GATE Computer ScienceVery hardGATE CS (PYQ pattern)
An LR parser for S → ( L ) | x; L → S | L , S parses "( ( x , x ) , x , ( x ) )". How many reductions does it perform (accept not counted), and what is the largest number of grammar symbols on its stack at any moment?
- A. 13 reductions, 8 symbols
- B. 11 reductions, 6 symbols
- C. 7 reductions, 5 symbols
- D. 13 reductions, 6 symbols
Correct answer: D. 13 reductions, 6 symbols
Each reduction builds one interior node of the parse tree: 7 S-nodes (4 for the x's, 2 for inner parentheses, the root) and 6 L-nodes (2 for (x,x), 1 for (x), 3 for the outer list) = 13. Thanks to left recursion each list is reduced as it grows, so the stack peaks just before the inner (x) is reduced: ( L , ( L ) — 6 symbols.
More Compiler Design questions
- Sethi–Ullman labelling: a leaf is 1 as a left child and 0 as a right child (a right operand may be a memory operand); an interior node is…
- Build the LL(1) table for E → T R; R → + T R | − T R | ε; T → F P; P → ^ T | ε; F → ( E ) | id (^ is right-associative exponent). How many…
- An LR parser for E → id | E [ E ] | ( E ) parses "id [ ( id [ id ] ) ]". What is the largest number of grammar symbols on its stack at any…
- S → ( S ) S | ε is both LL(1) and SLR(1). Both parsers process "( ( ) ( ) )". What is the largest number of grammar symbols on the LL(1)…
- Augment S → ( L ) | x; L → S | L , S with S′ → S and build the canonical collection of LR(0) item sets. How many states does it have, and…
- The grammar S → A B; A → a | ε; B → A b | c is not LL(1). Each change below is applied to it on its own. Which of them make the grammar…