I came across following problem:
Here $id$ is a token that represents an integer and $id.value$ represents the corresponding integer value. What does this translation scheme prints for an input "$2∗3+4$"?
Here are things I am able to understand:
- Since semantic actions are placed in between, but not at the end of production, this is L attributed grammar.
- L attributed grammar is parsed left to right in depth first manner.
- I know that semantic actions associated with synthesized attributes should be evaluated when the corresponding node is last visited during traversal (that is in postfix order) since all its children need to be evaluated first in order for parent node to synthesize its value from children.
- The semantic actions associated with inherited attributes should be evaluated at first time their corresponding node is visited.
I am able to prepare parse tree for the given string:
But I am not sure how should I be evaluating semantic actions. Here, no attribute derives its value from other (all are print statements). Thus, it seems that this is both synthetic and inherited (or should I say L attribute?). Since this is also synthetic, I felt I should evaluate semantic actions when I visited the corresponding nodes at last time, i.e. in postfix order yielding "$234+*$". Is this correct? I feel I miss very basic major concept here...