Skip to content
GATEiit

GATE CSE 2021 Set 1 Q36: What an SDT for int/bool declarations and expressions can type-check

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

Select one option
Wrong answer costs 2/3 mark in GATE.
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 E→IDE \to \text{ID} always says int.

First step: Take a declared bool variable b and the expression !b: what type does E→IDE \to \text{ID} give b, and does the check in E→ !E1E \to\, !E_1 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

WhatsAppDownload image

Source: official GATE 2021 Set 1 CS question paper and final answer key, organised by IIT Bombay. Question paper · Answer key