GATE Computer ScienceHardGATE CS (PYQ pattern)

For the grammar E → E + E | E * E | a (no precedence or associativity rules), the string a+a*a+a*a has 14 parse trees. In how many of them is the operator at the ROOT a *?

  1. A. 4
  2. B. 5
  3. C. 10
  4. D. 7

Correct answer: D. 7

Choose the root operator and multiply the tree counts of the two sides (Catalan numbers by operand count). Root = 1st *: left a+a (1 tree) × right a+a*a (2 trees) = 2. Root = 2nd *: left a+a*a+a (4 operands, 5 trees) × right a (1) = 5. Total 7; the other 7 trees have a + at the root.

More Theory of Computation questions

Exams that ask this topic