Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation actions, given as pseudo-code
With respect to the above grammar, which one of the following choices is correct?
View worked solution
Answer: B
To determine the correctness of the given Syntax-Directed Translation (SDT) actions, let us analyze the semantic rules associated with the production rules.
1. Analysis of the Identifier Rule
Consider the production rule for reducing an identifier to an expression:
This rule unconditionally sets the type of the expression to , completely ignoring the actual type recorded for the identifier in the symbol table during the declaration phase ( or ).
2. Evaluating Boolean Expressions
Suppose we have a syntactically correct program with a boolean declaration and a boolean expression:
bool x
!x
- During declaration, the rule correctly records that
xis of type . - During expression evaluation, the identifier
xis reduced to using . This action unconditionally sets . - Next, the expression
!xis evaluated using . The semantic action checks if . Since was set to , this check fails.
Thus, the SDT cannot correctly type-check valid boolean expressions.
3. Evaluating Integer Expressions
Suppose we have an integer declaration and an integer expression:
int y
y + y
- The identifier
yis reduced to using , setting . - The expression
y + yis evaluated using . The semantic action checks if , which is satisfied. It then sets .
Since the default fallback type assigned by is , all integer declarations and expressions are type-checked correctly.
Conclusion
The actions can only be used to correctly type-check syntactically correct integer variable declarations and integer expressions.
Therefore, the correct option is B.
Free practice · Your answers on this page are not saved.
