GATE Computer ScienceModerateGATE CS (PYQ pattern)
An LL(1) parser for S → A B C; A → a A | ε; B → b B | ε; C → c | ε reads the input "a b a c". At the moment it first detects the syntax error, what is on top of its stack and what has been matched?
- A. C on top, after matching a b
- B. A on top, after matching a
- C. $ on top, after matching a b a
- D. B on top, after matching a b
Correct answer: D. B on top, after matching a b
S → A B C, A → a A, match a. With lookahead b, M[A, b] = A → ε (b ∈ FOLLOW(A)). M[B, b] = B → b B, match b. Now B is on top with lookahead a: FOLLOW(B) = {c, $} and FIRST(B) = {b}, so M[B, a] is blank: the error is found with B on top after a b were matched.
More Compiler Design questions
- 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…
- 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)…