IAE Academy

GATE 2020 CS Q.33: solution and answer

GATE 2020 · CSQ.33 · 2 marks · MCQ

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.

See the question in the paper

Answer and solution

ANSWER

Only Rule 1 is L-attributed, which is option (B).

Official answer: BSource: Official answer key, GATE 2020 CS (IIT Delhi)

  1. A definition is L-attributed if, for every production , each inherited attribute of depends only on (i) inherited attributes of and (ii) attributes (inherited or synthesized) of , the symbols to its left. Synthesized attributes of may depend on any attribute in the body.

  2. Rule 1, production : uses only the inherited attribute of the head, so it is allowed.

  3. uses , which is to the left of , and the head's inherited attribute , so it is allowed.

  4. defines a synthesized attribute of the head, which may use any child. So Rule 1 is L-attributed.

  5. Rule 2, production : makes the inherited attribute of depend on , an attribute of , which is to the right of . This violates the left-to-right condition, so Rule 2 is not L-attributed.

  6. alone would be allowed, because is to the left of . One violation is enough to fail the definition. Together, needs and needs , so the dependencies are also circular.

  7. Hence only Rule 1 is L-attributed: option (B).

KEY IDEA

In an L-attributed definition, an inherited attribute of a body symbol may use only the head's inherited attributes and attributes of symbols to its left. Never an attribute of a symbol to its right.

WATCH OUT FOR

Checking only , which is legal, and ignoring that looks to the right. Another slip is to think a synthesized attribute like is restricted in the same way.

From the GATE 2020 CS paper. Every question of this paper · Download the full paper · All 7 Syntax-directed translation questions, with notes

More Syntax-directed translation questions