GATE CS 2021 Set 1 | Question 36

Compiler Design › Parser · MCQ · 2 marks · Answer: B

  1. Question 36Compiler Designmcq2 marks
    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?
    • A.
      The actions can be used to correctly type-check any syntactically correct program.
    • B.
      The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
      ✓
    • C.
      The actions can be used to type-check syntactically correct boolean variable declarations and boolean expressions.
    • D.
      The actions will lead to an infinite loop.

    Answer: B

    Solution

    The rule unconditionally sets type int, ignoring a bool declaration, so boolean expressions are not correctly checked. The actions only reliably type-check integer declarations and integer expressions.

More GATE PYQ on Parser

All GATE CS Compiler Design PYQ →

Attempt GATE CS 2021 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →