Skip to content
Official PYQ · 2019

GATE CS 2019 Q36 — Syntax-directed Translation

Explain with AI

Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let X1,X2,X3,X4,X5X_1, X_2, X_3, X_4, X_5, and X6X_6 be the placeholders for the non-terminals D,T,LD, T, L or L1L_1 in the following table:

Production ruleSemantic actionD→TLX1.type=X2.typeT→intT.type=intT→floatT.type=floatL→L1,idX3.type=X4.typeaddType(id.entry,X5.type)L→idaddType(id.entry,X6.type)\begin{array}{|l|l|} \hline \text{Production rule} & \text{Semantic action} \\ \hline D \rightarrow T L & X_1.\text{type} = X_2.\text{type} \\ \hline T \rightarrow \text{int} & T.\text{type} = \text{int} \\ \hline T \rightarrow \text{float} & T.\text{type} = \text{float} \\ \hline L \rightarrow L_1, id & X_3.\text{type}= X_4.\text{type} \\ &\text{addType}(id. \text{entry}, X_5.\text{type})\\ \hline L \rightarrow id & \text{addType}(id. \text{entry}, X_6.\text{type}) \\ \hline \end{array}

Which one of the following are appropriate choices for X1,X2,X3X_1, X_2, X_3 and X4X_4?

Choose one option
View worked solution

Answer: A

To determine the correct placeholders, let us analyze how type information flows in a variable declaration grammar using Syntax-Directed Translation (SDT).

1. Analysis of D→TLD \rightarrow T L

  • The non-terminal TT synthesizes the type (either int or float) via the rules T→intT \rightarrow \text{int} and T→floatT \rightarrow \text{float}.
  • This type information must be passed down (inherited) to the list of identifiers LL.
  • Therefore, the semantic action must assign the type of TT to LL: L.type=T.typeL.\text{type} = T.\text{type}
  • Comparing this with the given action X1.type=X2.typeX_1.\text{type} = X_2.\text{type}, we get: X1=LandX2=TX_1 = L \quad \text{and} \quad X_2 = T

2. Analysis of L→L1,idL \rightarrow L_1, id

  • The parent list LL has already inherited the type attribute in L.typeL.\text{type}.
  • This type must be propagated down to the sublist L1L_1 so that the identifiers inside L1L_1 can also be declared with the same type.
  • Therefore, the inherited type of L1L_1 is obtained from LL: L1.type=L.typeL_1.\text{type} = L.\text{type}
  • Comparing this with the given action X3.type=X4.typeX_3.\text{type} = X_4.\text{type}, we get: X3=L1andX4=LX_3 = L_1 \quad \text{and} \quad X_4 = L

Conclusion

Matching our findings with the options:

  • X1=LX_1 = L
  • X2=TX_2 = T
  • X3=L1X_3 = L_1
  • X4=LX_4 = L

This corresponds exactly to Option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Syntax-directed Translation questions