Question
Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals and terminals :
Using the above SDTS, the output printed by a bottom-up parser, for the input aab is:
Official answer and solution
- Official answer
- C
- From
- Official GATE 2016 answer key
Solution
Trace the shift-reduce steps of a bottom-up parser on input aab using the reverse of the rightmost derivation.
The SDTS productions with semantic actions are:
To find the bottom-up parsing sequence for input string aab, determine the rightmost derivation in reverse:
In reverse, this corresponds to the following sequence of reductions:
- The second
ais reduced to using rule . - The handle is reduced to using rule .
- The handle is reduced to using rule .
Step-by-step bottom-up parser actions:
- Shift
a. (Stack:a, Remaining:ab) - Shift
a. (Stack:aa, Remaining:b) - Reduce top
ausing . This executes{ print 2 }. (Stack:a S, Remaining:b) - Shift
b. (Stack:a S b, Remaining:$) - Reduce
Sbusing . This executes{ print 3 }. (Stack:a A, Remaining:$) - Reduce
aAusing . This executes{ print 1 }. (Stack:S, Remaining:$) - Accept.
Concatenating the printed values yields: 2 3 1. This corresponds to option C.
C
Prematurely reducing the very first a to , which leads to stack S before shifting the second a, creating a configuration from which aab cannot be reduced.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 1
- Question
- 56 of 65, printed as CS Q.46
- 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.