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?
- A. 14 trees, 14 values
- B. 14 trees, 7 values
- C. 5 trees, 5 values
- 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
- A yacc-generated parser for E → E op E | num uses the precedence declarations %right '+' '-'; %left ''; later declarations bind tighter…
- How many distinct LR(0) items does the grammar E → E + T | T; T → T F | F; F → ( E ) | id have (do not augment it; an ε-production gives…
- int f(int n) { if (n <= 2) return 1; return f(n − 1) + f(n − 2) + f(n − 3); } How many calls to f (including the first) does f(8) make as…
- For S → A B; A → a A | ε; B → b B | ε (augmented), in how many states of the LR(0) automaton is there a conflict, and is the grammar SLR(1)?
- With the Sethi–Ullman labelling (a left leaf is 1, a right leaf is 0 because it can be a memory operand; an interior node is max(l1, l2)…
- For S → S ( S ) | ε (augmented with S′ → S), how many states do the LR(0) and canonical LR(1) automata have?