GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Propagate types through declaration lists

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

How these are solved

Both use a left-recursive identifier-list declaration grammar. Propagate the declared type from T through L and attach it to identifier symbol-table entries.

2 marks · MCQ
Report issue

Propagating declaration types

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

Descriptive
Report issue

Type assignment with end-only semantic actions

Semantic Analysis & Type Checking

Question 11(a), GATE 1992: Add syntax-directed semantic rules to the ends of productions, without changing the grammar, so each identifier gets its declared type in the symbol table:
D → T L ;
T → int | real
L → L , id | id
Tap to enlarge
Answer & solution

Answer: Collect identifier-entry references in synthesized L.entries; assign T.type to them at D → T L ;. (no official key was published)

A synthesized list of symbol-table references solves the end-only restriction without needing to pass an inherited type into L before parsing it. Give T a synthesized attribute type and L a synthesized attribute entries. The lexical token id.entry is a reference to that identifier's symbol-table entry (or the declaration entry created for it in the current scope).

Attach precisely these actions at production ends:
D → T L ; { for each p in L.entries: p.type := T.type }
T → int { T.type := integer }
T → real { T.type := real }
L₀ → L₁ , id { L₀.entries := append(L₁.entries, id.entry) }
L → id { L.entries := [id.entry] }

The subscripts distinguish the two occurrences of L; they are attribute notation, not new grammar symbols. append constructs or extends a list of references, not copies of symbol-table records. On reduction of L → id, the one-element list is available. Each reduction of the recursive production adds one further identifier. By induction on the list length, the final L.entries contains exactly the identifiers in this declaration. At the D reduction both T.type and the complete list are available, so every listed entry receives the same type.

For real x,y,z;, T.type becomes real, the L lists successively become [x], [x,y], [x,y,z], and the final action assigns real to x, y, and z. All actions are at the right ends, and the original productions are untouched. Redeclaration detection and nested-scope lookup are separate symbol-table concerns. With a tail pointer or linked-list concatenation, building and processing a k-identifier list costs O(k) time and O(k) references.

Original paper, page 8