Question: Consider the following grammar for reverse Polish arithmetic expressions: E E E op | id op + | - | * | /
Consider the following grammar for reverse Polish arithmetic expressions:
E → E E op | id
op → + | - | * | /
Assuming that each id has a synthesized attribute name of type string, and that each E and op has an attribute val of type string, write an attribute grammar that arranges for the val attribute of the root of the parse tree to contain a translation of the expression into conventional infix notation. For example, if the leaves of the tree, left to right, were “A A B - * C /,” then the val field of the root would be “( ( A * ( A - B ) ) / C ).” As an extra challenge, write a version of your attribute grammar that exploits the usual arithmetic precedence and associativity rules to use as few parentheses as possible.
Step by Step Solution
3.46 Rating (179 Votes )
There are 3 Steps involved in it
To minimize parentheses let us say that constants have precedence 3 p... View full answer
Get step-by-step solutions from verified subject matter experts
