GATE / CS ARCHIVE

GATE CSE 2019 / QUESTION 36

Propagating declaration types

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

GATE 2019, Q36 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 36, GATE 2019: For declarations use D→T L; T→int|float; L→L1 , id|id. T synthesizes the chosen type. Semantic actions include D→TL: X1.type=X2.type and L→L1,id: X3.type=X4.type. Find X1,X2,X3,X4: A (L,T,L1,L), B (T,L,L1,T), C (L,L,L1,T), D (T,L,T,L1).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

1. T reads the type keyword and synthesizes its type value: int for T→int or float for T→float. The identifier list must receive this value from its declaration context.
2. In D→T L, the value flows from sibling T to L. Therefore L.type=T.type, giving X1=L and X2=T. Reversing this assignment would try to obtain the declared type from an untyped identifier list.
3. In L→L1,id, the parent L already has an inherited type. The shorter list L1 must inherit that same type, so L1.type=L.type. Consequently X3=L1 and X4=L.
4. The last identifier also gets L.type through addType; in the base case L→id, the single identifier receives L.type. For example, int a,b passes int from T to the outer list, then to its inner list and to both identifier entries.
5. The requested tuple is (L,T,L1,L), option A. T need not appear directly in recursive list productions: its value has already been propagated into the inherited L attribute.

Original paper, page 12 · Answer key