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
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
