in Compiler Design
2,854 views
0 votes
0 votes
Given an Operator Grammar as,

E -> E*F / F+E / F

F -> F-F / id

How to determine associativity in this case? Since Operator grammar can be ambiguous also.

Is the above question solved using associativity and precedence of operators we consider in C programming? or it is Different.
in Compiler Design
2.9k views

2 Answers

0 votes
0 votes
See recursive nature of the grammar.

If any production is left recursive then the associativity of operator will be left.

In case right recursive, the right associativity.

Also when parse tree is made, operator that comes at lower level has higher associativity like here - > * and  - > +.

If operands are same then generally +, *, - are left associative.
by
0 votes
0 votes
If the grammar isn't ambiguous we can directly get the the associativity and precedence from the grammar. But operator precedence grammar is ambiguous hence we mayn't always get these from it.

E -> E*F / F+E / F

* and + have equal precedence, + is right associative and * is left associative.

 F -> F-F / id

Here F can be both left and right associative.

So in case of ambiguous grammar we can sometimes get precedence and associativity.

Related questions