Consider the SDT. S1: P → Q + R {P.val= Q.val + R.val} S2: P → Q.R {P.val =…
Consider the SDT.
S1: P → Q + R {P.val= Q.val + R.val}
S2: P → Q.R {P.val = Q.val * R.val and Q.val = R.val}
Which of the given option is correct w.r.t the above SDT?
Answer: A. S1 is L attributed but S2 is not L-attributed. — Conclusion: S1 is L-attributed but S2 is not L-attributed. Reasoning: S1: For the production P → Q R with semantic rule P.val = Q.val + R.val, only a…
- A.
S1 is L attributed but S2 is not L-attributed.
- B.
S1 is S attributed and S2 is L-attributed.
- C.
Both S1 and S2 are S attributed.
- D.
None of the above
Attempted by 63 students.
Show answer & explanation
Correct answer: A
Conclusion: S1 is L-attributed but S2 is not L-attributed.
Reasoning:
S1: For the production P → Q R with semantic rule P.val = Q.val + R.val, only a synthesized attribute (P.val) is computed from attributes of its children Q and R. This fits the definition of an S-attributed grammar (only synthesized attributes), and such a grammar also satisfies the L-attributed requirements (L-attributed allows synthesized attributes). Therefore S1 is both S-attributed and L-attributed.
S2: For the production P → Q R with rules P.val = Q.val * R.val and Q.val = R.val, the rule Q.val = R.val makes Q depend on R, which is to the right of Q. This creates a dependency of a right-hand symbol on a symbol to its right, violating the L-attributed restriction that an inherited attribute of a symbol on the right-hand side may depend only on symbols to its left and on the left-hand side. It also prevents S2 from being S-attributed, since synthesized-only definitions should not require sibling-to-sibling dependencies. Therefore S2 is neither S-attributed nor L-attributed.