GATE Computer ScienceHardGATE CS (PYQ pattern)
G1: L → L , x | x and G2: L → x , L | x generate the same language. A shift-reduce (LR) parser parses "x , x , x , x , x" (five x's) with each grammar. What is the largest number of grammar symbols on the stack at any moment with G1 and with G2?
- A. 9 with G1 and 3 with G2
- B. 3 with G1 and 5 with G2
- C. 5 with G1 and 9 with G2
- D. 3 with G1 and 9 with G2
Correct answer: D. 3 with G1 and 9 with G2
Left recursion lets G1 reduce as it goes: the stack never exceeds L , x (3 symbols). With right recursion nothing can be reduced until the last x is seen, so all 9 input symbols are on the stack together before the chain of L → x , L reductions begins.
More Compiler Design questions
- Productions of the grammar are numbered: (1) E → E + T; (2) E → T; (3) T → T F; (4) T → F; (5) F → ( E ); (6) F → id. An LR parser parses…
- In 1: i = 0; 2: L1: if i >= n goto L3; 3: if i > k goto L2; 4: t = a b; 5: x = t + 1; 6: L2: y = a + b; 7: i = i + 1; 8: goto L1; 9: L3…
- For the grammar S → X Y Z; X → a X | ε; Y → b Y | X; Z → c | ε, what is FIRST(S)? (ε counts as a member if S is nullable.)
- The statement r = (a + b) (b + a) − (a + b) / c is turned into a DAG (+ and commutative, − and / not), and three-address code is generated…
- For S′ → S; S → a S a | b S b | c, the canonical LR(1) states with identical cores are merged to obtain LALR(1). The LALR state whose only…
- Which of the following about intermediate representations is/are true? I. In triples, an operand that is a result of another statement is…