GATE Computer ScienceHardGATE CS (PYQ pattern)

The grammar S → S S S | a is ambiguous. How many distinct parse trees do the strings a⁷ (seven a's) and a⁶ (six a's) have, respectively?

  1. A. 12 and 5
  2. B. 55 and 12
  3. C. 12 and 0
  4. D. 42 and 0

Correct answer: C. 12 and 0

Each use of S → S S S adds two leaves, so derivable strings have odd length: a⁶ has no tree. Parse trees of a⁷ are full ternary trees with 3 internal nodes, counted by the ternary Catalan number C(9, 3)/(2·3 + 1) = 84/7 = 12.

More Compiler Design questions

Exams that ask this topic