Question:

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 → D* E*
D → int ID {record that ID.lexeme is of type int}
D → bool ID {record that ID.lexeme is of type bool}
E → E1 + E2 {check that E1.type = E2.type = int; set E.type := int}
E → !E1 {check that E1.type = bool; set E.type := bool}
E → ID {set E.type := int}

With respect to the above grammar, which one of the following choices is correct?

Show Hint

Always ensure that identifier type lookup in expressions refers to the symbol table rather than assigning a fixed type.
Updated On: Jan 2, 2026
  • 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.
  • The actions will lead to an infinite loop.
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Examine type assignment for identifiers.
In the production \(E \rightarrow ID\), the action always sets \(E.\text{type} := \text{int}\), regardless of whether the identifier was declared as \texttt{int} or \texttt{bool}. This ignores boolean declarations when identifiers are used in expressions.

Step 2: Analyze integer expressions.
The production \(E \rightarrow E_1 + E_2\) correctly checks that both operands are of type \texttt{int} and assigns type \texttt{int} to the result. Hence, integer expressions are type-checked correctly.

Step 3: Analyze boolean expressions.
Although boolean declarations are recorded, boolean identifiers used in expressions are incorrectly typed as \texttt{int}. Therefore, expressions involving boolean variables cannot be reliably type-checked.

Step 4: Eliminate incorrect options.
Option (A) is incorrect because boolean expressions are not correctly handled.
Option (C) is incorrect for the same reason.
Option (D) is incorrect since the grammar does not contain left-recursive or cyclic productions that cause infinite looping in SDT actions.

Step 5: Conclusion.
The given actions correctly type-check only syntactically correct integer variable declarations and integer expressions.

Final Answer: (B)

Was this answer helpful?
0
0