GATE Computer ScienceHardGATE CS (PYQ pattern)

The SLR(1) table of S → i E t S Z | a; Z → e S | ε; E → b has one shift-reduce conflict (shift e versus reduce Z → ε), resolved as yacc does by shifting. While parsing "i b t i b t a e a", what are the last four reductions, in order?

  1. A. Z → ε, S → i E t S Z, Z → e S, S → i E t S Z
  2. B. Z → e S, Z → ε, S → i E t S Z, S → i E t S Z
  3. C. Z → e S, S → i E t S Z, Z → ε, S → i E t S Z
  4. D. Z → ε, Z → e S, S → i E t S Z, S → i E t S Z

Correct answer: C. Z → e S, S → i E t S Z, Z → ε, S → i E t S Z

After the first a is reduced to S, the lookahead is e and the parser shifts it (the conflict choice), so the else is attached to the inner if: a → S, then Z → e S and the inner S → i E t S Z. Now the lookahead is $, so the outer if gets Z → ε, and finally the outer S → i E t S Z.

More Compiler Design questions

Exams that ask this topic