Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation (SDT) actions, given as pseudo-code: $P \rightarrow D^* E^*$ $D \rightarrow$ int ID {record that ID.lexeme is of type int} $D \rightarrow$ bool ID {record that ID.lexeme is of type bool} $E \rightarrow E_1 + E_2$ {check that $E_1$.type = $E_2$.type = int; set $E$.type := int} $E \rightarrow !E_1$ {check that $E_1$.type = bool; set $E$.type := bool} $E \rightarrow$ ID {set $E$.type := int} With respect to the above grammar, which one of the following choices is correct?
The actions can be used to correctly type-check any syntactically correct program.
The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
The actions can be used to type-check syntactically correct boolean variable declarations and boolean expressions.
Correct answer: (B) The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
Explanation
The rule E -> ID always sets type int, ignoring the recorded declaration, so boolean variables cannot be type-checked. Only integer declarations and expressions are handled correctly.
More Syntax-Directed Translation questions
GATE 2020 CS Q43– Consider the productions A → PQ and A → XY. Each of the five non-terminals A, P, Q, X, and…
GATE 2019 CS Q46– Consider the following grammar and the semantic actions to support the inherited type…
GATE 2016 CS (CS1) Q56– Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals \S,…
GATE 2021 CS (CS2) Q13– Which phase of a seven-phase ANSI C compiler reports the error? Assume Integer has not…
GATE 2026 CS (CS1) Q53– Consider the following two syntax-directed definitions SDD1 and SDD2 for type…
GATE 2025 CS (CS2) Q22– Given the following syntax directed translation rules: Rule 1: R AB\ \B.i = R.i - 1;\ A.i…