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:

enter image description here

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...

  • $\begingroup$ This post may help. $\endgroup$
    – fade2black
    Jan 2, 2018 at 12:21
  • $\begingroup$ Yes..what u r thinking is correct. Above grammar is L attributed and can be solved either way..top down parsing or bottom up parsing....I solved using both and got the same answer. $\endgroup$ May 19, 2018 at 12:39


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy