



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Today ...
Basics
Dealing with left-associative operators
Example:
e → val ( ÷ val )∗
Expression* expr() { Expression* v1 = val(); advance(); while(tok.type() == DIV) { advance(); Expression* v2 = val(); v1 = new Div(v1, v2); } return v1; }
Operator precedence
2 + 3 / 4 ≡ 2 + (3 / 4) 2 / 3 + 4 ≡ (2 / 3) + 4
One solution: Encode precedence in the grammar
e → t ( ‘+’ t )∗
t → num ( ’/’ num )∗
Q: What is the parse tree for: 2 + 3 / 4?