Skip to content
Advertisement

How can I create an abstract syntax tree considering ‘|’? (Ply / Yacc)

Considering the following grammar:

JavaScript

This is my code using ply:

JavaScript

How am I supposed to build an AST with the expression if I can’t access the operators? I could separate the functions like p_expr_plus, but in this case, I would eliminate operator precedence. The docs are not so helpful, since I’m a beginner and can’t solve this problem. The best material I’ve found on the subject is this, but it does not consider the complexity of operator precedence.

EDIT: I can’t access p2 or p[3], since I get an IndexError (It’s matching the term only). In the PDF I’ve linked, they explicitly put the operator inside the tuple, like: (‘+’, p1, p2), and thus, evincing my problem considering precedence (I can’t separate the functions, the expression is the expression, there should be a way to consider the pipes and access any operator).

Advertisement

Answer

As far as I can see, in p[0] = ("expr", p[1], p[2]), p[1] would be the left hand expression, p[2] would be the operator, and p[3] (that you aren’t using) would be the right hand term.

Just use p[2] to determine the operator, add p[3], since you will need it, and you should be good to go.

Also, you must verify how many items p has, since if the last rule, | term""" is matched, p will only have two items instead of four.

Take a look at a snippet from the GardenSnake example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement