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?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2021 answer key
Solution
Trace how type attributes are assigned and verified for integer versus boolean identifiers.
Inspect the semantic rule associated with the identifier production :
Notice that this action unconditionally assigns to any variable reference, without looking up the declared type of in the symbol table.
If a boolean variable is declared (bool b;) and subsequently used in an expression such as !b, the rule sets . Then the rule checks whether , which fails because was forced to int! Thus boolean expressions cannot be correctly type-checked.
However, for integer variable declarations (int a, c;) and expressions (a + c), sets , and correctly checks . Thus, the actions can correctly type-check syntactically correct integer declarations and integer expressions (option B).
B
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology (CS, Set-1)
- Question
- 36 of 65, printed as CS Q.26
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.