GATE 2019 CS – Question 46
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 rule | Semantic action |
|---|---|
| D → T L | $X_1$.type = $X_2$.type |
| T → int | T.type = int |
| T → float | T.type = float |
| L → $L_1$ , id | $X_3$.type = $X_4$.type; addType(id.entry, $X_5$.type) |
| L → id | addType(id.entry, $X_6$.type) |
Which one of the following are the appropriate choices for $X_1$, $X_2$, $X_3$ and $X_4$?
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$).