Question

Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals {S,A}\{S, A\} and terminals {a,b}\{a, b\}:

S⟶aA{print 1}S⟶a{print 2}A⟶Sb{print 3}\begin{aligned} S &\longrightarrow aA \quad \{ \text{print } 1 \} \\ S &\longrightarrow a \quad \{ \text{print } 2 \} \\ A &\longrightarrow Sb \quad \{ \text{print } 3 \} \end{aligned}

Using the above SDTS, the output printed by a bottom-up parser, for the input aab is:

Choose one option
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:

  1. S→aA{print 1}S \to aA \quad \{ \text{print } 1 \}
  2. S→a{print 2}S \to a \quad \{ \text{print } 2 \}
  3. A→Sb{print 3}A \to Sb \quad \{ \text{print } 3 \}

To find the bottom-up parsing sequence for input string aab, determine the rightmost derivation in reverse:

S  ⟹  aA  ⟹  aSb  ⟹  aabS \implies aA \implies aSb \implies aab

In reverse, this corresponds to the following sequence of reductions:

  1. The second a is reduced to SS using rule S→aS \to a.
  2. The handle SbSb is reduced to AA using rule A→SbA \to Sb.
  3. The handle aAaA is reduced to SS using rule S→aAS \to aA.

Step-by-step bottom-up parser actions:

  1. Shift a. (Stack: a, Remaining: ab)
  2. Shift a. (Stack: aa, Remaining: b)
  3. Reduce top a using S→aS \to a. This executes { print 2 }. (Stack: a S, Remaining: b)
  4. Shift b. (Stack: a S b, Remaining: $)
  5. Reduce Sb using A→SbA \to Sb. This executes { print 3 }. (Stack: a A, Remaining: $)
  6. Reduce aA using S→aAS \to aA. This executes { print 1 }. (Stack: S, Remaining: $)
  7. Accept.

Concatenating the printed values yields: 2 3 1. This corresponds to option C.

Final answer

C

Common trap

Prematurely reducing the very first a to SS, 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.