Two syntax-directed type declarations
Semantic Analysis & Type Checking

Answer & solution
Official answer: A, B, D
Givens and goal
G1 has D→TV, T→int|float, V→V₁ id|id; G2 has D→D₁ id|T id, T→int|float. In SDD1, T.type is set by its keyword, D.type=T.type, V.type=T.type, V₁.type=V.type, and each id is entered with V.type. In SDD2, D.type comes from D₁.type or T.type and each id is entered with that child’s type. Decide A same language, B SDD2 S-attributed, C SDD1 L-attributed with only inherited attributes, D same symbol-table entries.
Approach
Compare generated token sequences separately from how attribute values flow through each parse tree.
Step-by-step solution
1. Both grammars generate a type keyword, either int or float, followed by one or more identifier tokens. G1 builds an identifier list through V; G2 builds the same sequence by extending D. Therefore their languages are identical, establishing A.
2. In SDD2, T.type comes from a type keyword and D.type comes from a child T or D. Every attribute value flows upward from children to the left-hand-side symbol, or is a constant. All attributes are synthesized, so SDD2 is S-attributed and B is true.
3. SDD1 passes the type inherited by V down through the identifier list. But it also contains synthesized attributes, including T.type and D.type. Its inherited dependencies are L-attributed, yet the stronger wording that it contains only inherited attributes is false. Thus C is false.
4. In either definition, every identifier receives the same type supplied by the leading keyword. Thus the same identifier/type entries are recorded, establishing D. The ways in which the values flow through the parse tree differ; the resulting entries do not.
Common error: L-attributed does not mean exclusively inherited. An L-attributed definition can contain synthesized attributes as well.
Example: for the token sequence int id₁ id₂, SDD1 obtains int from T and passes it down through V to both identifiers. SDD2 first constructs the declaration for int id₁, synthesizes its type upward, and uses that type when extending the declaration with id₂. Both call put with (id₁,int) and (id₂,int). In SDD1, the existence of the inherited V.type does not erase the synthesized T.type and D.type; that is the exact overstatement in C.

![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.](../../../assets/questions/CS-2025-S2-Q22-1.webp)





























