Question
Consider the following grammar along with translation rules.
Here # and % are operators and id is a token that represents an integer and id.val represents the corresponding integer value. The set of non-terminals is and a subscripted non-terminal indicates an instance of the non-terminal.
Using this translation scheme, the computed value of S.val for root of the parse tree for the expression is _____________.
Official answer and solution
- Official answer
- 80
- From
- Official GATE 2022 answer key
Solution
Construct the parse tree according to the grammar rules and compute synthesized attribute values bottom-up.
The grammar is:
The grammar specifies that:
- Operator
#is left-associative at the -level, performing multiplication (). - Operator
%is left-associative at the -level, performing division (), with higher precedence than#.
The expression is .
Evaluating each -term:
- Term 1:
- Term 2:
- Term 3: :
- By left-associativity:
Evaluating the -level productions:
Thus, the computed value of at the root is 80.
80
Evaluating % from right to left as , violating the left-associative production .
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2022 Computer Science and Information Technology (CS)
- Question
- 65 of 65
- 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.