Consider the productions \(A → PQ\) and \(A → XY\). Each of the five…

2020

Consider the productions \(A → PQ\) and \(A → XY\). Each of the five non-terminals \(A,P,Q,X\), and \(Y\) has two attributes: \(s\) is a synthesized attribute, and \(i\) is an inherited attribute. Consider the following rules.

Rule 1 : \( P . i = A.i + 2, \: Q.i = P. i + A.i,\) and \(A.s = P.s +Q. s\)

Rule 2 : \( X.i = A.i + Y.s\), and \(Y. i = X. s +A .i\)

Which one of the following is TRUE ?

  1. A.

    Both Rule 1 and Rule 2 are \(L - attributed\)

  2. B.

    Only Rule 1 is \(L - attributed\)

  3. C.

    Only Rule 2 is \(L - attributed\)

  4. D.

    Neither Rule 1 nor Rule 2 is \(L - attributed\)

Attempted by 101 students.

Show answer & explanation

Correct answer: B

Key insight: an inherited attribute of a symbol may depend only on the parent's inherited attributes and attributes of symbols to its left; it must not depend on attributes of symbols to its right.

  • For A → P Q (Rule 1): P.i = A.i + 2 uses only the parent's inherited attribute (allowed). Q.i = P.i + A.i uses P's attribute (left sibling) and A.i (parent) (allowed). A.s = P.s + Q.s uses children synthesized attributes to form the parent's synthesized attribute (allowed). Therefore Rule 1 is L-attributed.

  • For A → X Y (Rule 2): X.i = A.i + Y.s makes an inherited attribute of X depend on Y.s, a synthesized attribute of the right sibling Y. That requires information from the right sibling and so violates the left-to-right restriction. Therefore Rule 2 is not L-attributed.

Conclusion: Only the first rule (the A → P Q rule with the given attribute equations) is L-attributed.

Explore the full course: Gate Guidance By Sanchit Sir