The GATE Grind

GATE 2019 CS – Question 46

Compiler Design · Syntax-Directed Translation · 2 marks · Multiple choice

Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let $X_1$, $X_2$, $X_3$, $X_4$, $X_5$, and $X_6$ be the placeholders for the non-terminals D, T, L or $L_1$ in the following table:

Production ruleSemantic action
D → T L$X_1$.type = $X_2$.type
T → intT.type = int
T → floatT.type = float
L → $L_1$ , id$X_3$.type = $X_4$.type; addType(id.entry, $X_5$.type)
L → idaddType(id.entry, $X_6$.type)

Which one of the following are the appropriate choices for $X_1$, $X_2$, $X_3$ and $X_4$?

  1. $X_1=L,\ X_2=T,\ X_3=L_1,\ X_4=L$
  2. $X_1=T,\ X_2=L,\ X_3=L_1,\ X_4=T$
  3. $X_1=L,\ X_2=L,\ X_3=L_1,\ X_4=L$
  4. $X_1=T,\ X_2=L,\ X_3=T,\ X_4=L_1$

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) $X_1=L,\ X_2=T,\ X_3=L_1,\ X_4=L$

Explanation

The type is inherited from $T$ to $L$ in $D\to TL$, so $L.type=T.type$ ($X_1=L$, $X_2=T$). In $L\to L_1,\ id$ the type is passed down to the child, $L_1.type=L.type$ ($X_3=L_1$, $X_4=L$).