GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A yacc-generated parser for E → E op E | num uses the precedence declarations %left '*'; %right '-'; %left '+' (in this order, so + binds tightest); later declarations bind tighter. The operators +, - and * have their usual arithmetic meaning. What does the parser compute for "2 * 8 - 3 - 1 + 4 * 2"?

  1. A. 20
  2. B. 32
  3. C. 28
  4. D. 40

Correct answer: D. 40

Apply the declared levels and associativity (not school precedence): the parenthesised grouping implied by the declarations evaluates to 40; school precedence would give 20.

More Compiler Design questions

Exams that ask this topic