GATE Computer ScienceHardGATE CS (PYQ pattern)
An LR parser for E → id | E [ E ] | ( E ) parses "( id ) [ id ]". Each shift and each reduce is one move (the final accept is not a move). What is on the stack (grammar symbols, bottom to top) right after move 8, and how many moves are made in all?
- A. E [ E; 10 moves
- B. E [ E; 9 moves
- C. ( E ) [ E; 10 moves
- D. E [ id; 10 moves
Correct answer: A. E [ E; 10 moves
Moves: 1 shift (, 2 shift id, 3 reduce E → id, 4 shift ), 5 reduce E → ( E ) (the stack is now just E), 6 shift [, 7 shift id, 8 reduce E → id giving E [ E, 9 shift ], 10 reduce E → E [ E ]. Five shifts (one per token) and five reductions (one per E-node of the tree) make 10.
More Compiler Design questions
- SDD: S → ( S S ) | a. Inherited attribute S.d: the root has S.d = 0 and each child of a node gets parent.d + 1. Synthesised S.v: for a →…
- A Pascal-like scanner uses longest match (ties to the earlier rule) with rules: FLOAT = [0-9]+.[0-9]; INT = [0-9]+; RANGE = ..; DOT =…
- SDD: S → ( S S ) | a. S.d is inherited: root d = 0, children get parent.d + 1. For a → a: S.v = S.d if S.d is even, otherwise −S.d. For S…
- int i = 1; int A[1..4] = {4, 7, 2, 9}; void swap(x, y) { int t = x; x = y; y = t; } The call swap(i, A[i]) is made. The parameters are…
- For S′ → S; S → A | x b; A → a A b | B; B → x, what is the LALR(1) lookahead set of the item B → x · in the state reached from the start…
- int x = 1; int a() { return x; } int b(int n) { int x = n 10; if (n == 0) return a(); return b(n - 1) + a(); } What does b(2) return under…