GATE / CS ARCHIVE

GATE CSE 2020 / QUESTION 33

GATE CSE 2020 Q33

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2020 paper

GATE 2020, Q33 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 33, GATE 2020: Productions A→PQ and A→XY have inherited i and synthesized s attributes on each nonterminal. Rule 1: P.i=A.i+2; Q.i=P.i+A.i; A.s=P.s+Q.s. Rule 2: X.i=A.i+Y.s; Y.i=X.s+A.i. Which are L-attributed? A both; B only Rule 1; C only Rule 2; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Worked explanation

An inherited attribute of a child in an L-attributed definition may depend on inherited attributes of the parent and attributes of siblings to its left. In A→PQ, P has no left sibling, and its inherited value depends only on A.i. Q can use P.i because P is left of Q. Synthesized A.s is evaluated after both children return. A valid evaluation order is A.i, P.i, process P to get P.s, Q.i, process Q to get Q.s, then A.s. In A→XY, X.i needs Y.s even though Y is on X’s right, violating the restriction. Y.i’s use of X.s would be allowed by itself; it does not rescue the first dependency.

Derivation and answer

Rule1 computes each inherited attribute from the parent’s inherited attribute and, for Q, a left sibling’s attribute; the parent synthesized attribute can then combine child results. This is L-attributed. Rule2 computes X.i using right sibling Y.s, violating the left-to-right dependency restriction. Only Rule1, B.

Original paper, page 11 · Answer key