GATE Computer ScienceHardGATE CS (PYQ pattern)
A predictive translator uses E → T R; R → + { print('+') } T R | − { print('−') } T R | ε; T → num { print(num.val) }. (The print action in R comes before T.) What does it print for the input 9 − 5 + 2?
- A. 9 5 − 2 +
- B. 9 − 5 + 2
- C. − + 9 5 2
- D. 9 5 2 + −
Correct answer: B. 9 − 5 + 2
Actions run in the order they appear in the right-hand sides during the left-to-right derivation. T prints 9; R sees − and prints − before expanding T, which prints 5; then + is printed before 2. The operator is emitted between its operands, so the output is infix. (With the action after T, R → + T { print('+') } R, the familiar postfix 9 5 − 2 + appears.)
More Compiler Design questions
- The grammar S → X Y Z; X → a X | ε; Y → b Y | X; Z → c | ε is not LL(1). How many distinct parse trees does the string "a a a c" have?
- Construct the SLR(1) parsing table of S → a S b S | b S a S | ε (augmented). In how many states of the SLR(1) automaton does the ACTION…
- G1: S → A B; A → a A | c; B → a B | c. G2: S → A B A; A → a A | c; B → b B | c. For each, how many more states does the canonical LR(1)…
- The grammar S → ( S ) S | ε is both LL(1) and SLR(1). On the input "( ( ) ) ( )", how many times does the LL(1) parser expand S → ε, and…
- Integer code: a = 4; b = a 2; c = b + n; d = c 0; e = d + b; f = e - 8; g = n + f; h = g 1; r = h. Only r is live at the end. The…
- For S → A | x b; A → a A b | B; B → x (augmented), how many states does the canonical LR(1) automaton have, and in how many states of the…