GATE Computer ScienceHardGATE CS (PYQ pattern)

The grammar S → aSb | Sb | b generates { aⁿbᵐ : m > n ≥ 0 }. How many distinct parse trees does the string aabbbb have?

  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 6

Correct answer: C. 3

Every tree is a spine of 2 aSb steps and 1 Sb step (4 b's = 2 from aSb + 1 from Sb + the final b) ending in b. Since the Sb step adds its b at the right end of whatever lies below, the three steps can be ordered arbitrarily: C(3, 1) = 3 trees, so the grammar is ambiguous.

More Theory of Computation questions

Exams that ask this topic