GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Classify synthesized and L-attributed dependencies

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Inspect each semantic assignment target and right-hand-side dependency; check child-to-parent synthesis and inherited left-to-right restrictions. Different rule sets; cyclic/right-sibling dependencies preserved.

1 mark · MCQ
Report issue

Semantic Analysis & Type Checking

Question 22, GATE 2025: Classify these syntax-directed attribute rules:
1. R→AB with B.i=R.i−1, A.i=B.i, R.i=A.i+1.
2. P→CD with P.i=C.i+D.i, D.i=C.i+2.
3. Q→EF with Q.i=E.i+F.i.
Options list the classification of rules 1,2,3 respectively:
A. [S and L], [S not L], [neither].
B. [neither], [S and L], [S and L].
C. [neither], [L not S], [S and L].
D. [S not L], [L not S], [S and L].
Here S means S-attributed and L means L-attributed.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

An S-attributed definition has only synthesized attributes: the parent obtains its values from children. An L-attributed definition additionally permits a child's inherited attributes to depend on the parent's inherited attributes and on attributes of siblings to that child's left, so a left-to-right evaluation is possible.

Rule 1 assigns attributes on both children, so it is not S-attributed. In particular A.i depends on B.i, an attribute of A's right sibling. That violates the L-attributed direction restriction. The listed dependencies also form the cycle R.i→B.i→A.i→R.i; they do not provide a straightforward acyclic evaluation. Thus rule 1 is neither.

In rule 2, P.i is synthesized, but D.i is inherited from its left sibling C. This is permitted in an L-attributed definition and disallowed in an S-attributed one. Rule 3 only synthesizes the parent's Q.i from E.i and F.i, so it is S-attributed, and every S-attributed definition is also L-attributed. The sequence is [neither], [L not S], [S and L], which is C. The letters used to name an attribute, such as i, do not determine its classification; its dependency does.

Original paper, page 19 · Answer key

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