GATE CS 2021, set 1 · Question 36 · Compiler Design · Multiple choice (MCQ) · 2 marks · −2/3 for a wrong answer

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, →, E₁ + E₂ {check that E₁.type = E₂.type = int; set E.type := int}; E, →, !E₁ {check that E₁.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?

  1. (A) The actions can be used to correctly type-check any syntactically correct program.
  2. (B) The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
  3. (C) The actions can be used to type-check syntactically correct boolean variable declarations and boolean expressions.
  4. (D) The actions will lead to an infinite loop.
Answer

Answer (B)

Answer from the official GATE CS 2021 answer key.

The step-by-step solution, where available, is part of GATE CS Pro. See what Pro includes