GATE Computer ScienceHardGATE CS (PYQ pattern)

The grammar S → a S | S a | b is ambiguous. How many distinct parse trees does the string "a a a b a a" have?

  1. A. 6
  2. B. 32
  3. C. 5
  4. D. 10

Correct answer: D. 10

Every tree peels one a off the left (S → a S) or the right (S → S a) at each step until b remains. The three left a's must be peeled by a S steps and the two right a's by S a steps, in any interleaving: C(5, 2) = 10 orders, each a different tree.

More Compiler Design questions

Exams that ask this topic