Question

Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let X1,X2,X3,X4,X5,X_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 rule Semantic action
D→T  LD \to T \; L X1.type=X2.typeX_1.\text{type} = X_2.\text{type}
T→intT \to \text{int} T.type=intT.\text{type} = \text{int}
T→floatT \to \text{float} T.type=floatT.\text{type} = \text{float}
L→L1,idL \to L_1 , \text{id} X3.type=X4.type;addType(id.entry,X5.type)X_3.\text{type} = X_4.\text{type}; \quad \text{addType}(\text{id.entry}, X_5.\text{type})
L→idL \to \text{id} addType(id.entry,X6.type)\text{addType}(\text{id.entry}, X_6.\text{type})

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

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2019 answer key

Solution

Track how type information flows from production head to children in syntax-directed translation schemes.

In syntax-directed translation schemes for variable declarations:

  1. For the declaration rule D→T  LD \to T \; L:
    The non-terminal TT synthesizes a type attribute (T.type=intT.\text{type} = \text{int} or float\text{float}). This type must be inherited by the identifier list LL. Therefore, the semantic action is:
    L.type=T.typeL.\text{type} = T.\text{type}
    Comparing with X1.type=X2.typeX_1.\text{type} = X_2.\text{type}, we obtain X1=LX_1 = L and X2=TX_2 = T.
  1. For the identifier list rule L→L1,idL \to L_1 , \text{id}:
    The list L1L_1 must inherit the exact same type attribute that was passed down to LL. Therefore, the semantic action is:
    L1.type=L.typeL_1.\text{type} = L.\text{type}
    Comparing with X3.type=X4.typeX_3.\text{type} = X_4.\text{type}, we obtain X3=L1X_3 = L_1 and X4=LX_4 = L.

Thus, the appropriate choices are X1=L,X2=T,X3=L1,X4=LX_1 = L, X_2 = T, X_3 = L_1, X_4 = L (Option A).

Final answer

A

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2019 Computer Science and Information Technology (CS)
Question
46 of 65, printed as CS Q.36
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.