GATE CSE 2021 Set 1 Q36: What an SDT for int/bool declarations and expressions can type-check
- Compiler Design
- 2 marks
- MCQ
- −2/3 if wrong
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:
With respect to the above grammar, which one of the following choices is correct?
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}Try it: multiple choice: exactly one correct option
Show answer and explanation
Answer: B (official GATE 2021 final answer key)
Concept used: An SDT type-checks correctly only if every rule assigns the right type; here always says int.
First step: Take a declared bool variable b and the expression !b: what type does give b, and does the check in pass?
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2021 Set 1 CS question paper and final answer key, organised by IIT Bombay. Question paper · Answer key
More Compiler Design PYQs
- 2026 Session 1 · Q27Classify errors in three C string declarations
- 2026 Session 1 · Q28True statements about LL(1) parsing
- 2026 Session 1 · Q42Redundant (common) subexpressions in blocks B4 and B5 of a CFG
- 2026 Session 1 · Q53Compare two SDDs for type declarations
- 2026 Session 2 · Q35Count tokens produced by a lexer for a given string
- 2026 Session 2 · Q41Shift-reduce conflicts in the LR(0) table of a grammar