GATE Computer ScienceVery hardGATE CSE · UGC NET

The grammar S → SS | aS | a is used to parse the string a⁶ (six a's). How many different parse trees does a⁶ have?

  1. A. 42
  2. B. 394
  3. C. 364
  4. D. 494

Correct answer: B. 394

Let T(i,j) count trees for a substring: T = [length 1] + T(shorter by 1) + Σ_k T(i,k)·T(k,j). Evaluating for length 6 gives 394 (the Catalan number 42 counts only the S → SS shapes).

More Theory of Computation questions

Exams that ask this topic