GATE / CS ARCHIVE

GATE CSE 2021 SET 1 / QUESTION 26

GATE CSE 2021 Set 1 Q26

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2021 Set 1 paper

GATE 2021 Set 1, Q26 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 26, GATE 2021: A grammar and SDT are: P→D* E*; D→int ID records the identifier as int; D→bool ID records it as bool; E→E1+E2 checks E1.type=E2.type=int and sets E.type=int; E→!E1 checks E1.type=bool and sets E.type=bool; E→ID unconditionally sets E.type=int. Which claim holds? A correctly type-checks every syntactically correct program; B type-checks syntactically correct integer declarations and integer expressions; C type-checks syntactically correct boolean declarations and boolean expressions; D leads to an infinite loop.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The ID expression action unconditionally sets its type to int instead of consulting the declarations. Consequently integer-only, properly declared expressions using + receive the intended types, but a declared boolean is still labeled int and a valid negation can be rejected. The finite semantic actions do not themselves loop. Thus B. As usual, the limited claim assumes the mentioned integer identifiers have valid declarations; the displayed action does not implement undeclared-variable checking.

Trace a valid boolean declaration/expression: bool flag; !flag. The declaration records bool, but E→ID labels flag as int; the negation action then demands bool and rejects a valid expression. Conversely int a; int b; a+b labels both operands int and passes. A corrected ID action would look up ID.lexeme in the symbol table, reject undeclared identifiers, and copy the declared type. Thus the exact defect is the unconditional int assignment, not the grammar's use of repetition.

Original paper, page 23 · Answer key