GATE Computer ScienceVery hardGATE CS (PYQ pattern)

For the grammar S → SaS | SS | b, how many distinct parse trees does the string bbabb have?

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

Correct answer: B. 5

If the root uses S → SaS, the single a splits bbabb into bb and bb, each with exactly 1 tree (S → SS): 1 tree. If the root uses S → SS, split into nonempty parts both derivable: b | babb (1 × 2) and bbab | b (2 × 1); bb | abb and bba | bb fail since a part cannot start or end with a. babb has 2 trees (b·abb impossible, so SaS with b|bb, or SS with bab|b). Total 1 + 2 + 2 = 5.

More Theory of Computation questions

Exams that ask this topic