GATE Computer ScienceVery hardGATE CS (PYQ pattern)

The ambiguous grammar E → E − E | E * E | id derives "2 − 3 * 4 − 5 * 1" (each number is an id). How many distinct parse trees does the string have, and how many different values do those trees compute?

  1. A. 14 trees, 14 values
  2. B. 14 trees, 7 values
  3. C. 5 trees, 5 values
  4. D. 14 trees, 5 values

Correct answer: D. 14 trees, 5 values

With 4 binary operators the trees are the Catalan number C4 = 14 bracketings. Evaluating all 14 gives only the values −15, −9, −5, 1 and 5 (multiplication by 1 and several groupings coincide), so 5 different values.

More Compiler Design questions

Exams that ask this topic