Practice free →
HomeGATE CSEcomputerscienceCompiler Design › A SHIFT/REDUCE CONFLICT in an LR parser occurs w…

A SHIFT/REDUCE CONFLICT in an LR parser occurs when

Aparser cannot pick shift or reduce in a state
Btwo different tokens overlap
Cthe input file has shifted
Dthe grammar has too many rules
Answer & Solution
Correct answer: A. parser cannot pick shift or reduce in a state
1. At each step, an LR parser must decide: SHIFT (consume more input) or REDUCE (apply a grammar rule)? 2. SHIFT/REDUCE CONFLICT: in some state with some lookahead, the parser cannot decide between shifting and reducing — the grammar is AMBIGUOUS at that point. 3. CLASSIC EXAMPLE: the 'dangling else' problem. `if (x) if (y) a; else b;` Should the else match the outer if or the inner if? 4. yacc/bison resolve by preferring SHIFT (matches the inner if — the common convention). 5. Some grammars can be rewritten to remove conflicts; others (truly ambiguous) cannot. 6. Other options misstate the conflict. _Source: Bob Nystrom, "Crafting Interpreters", Ch 6 — referenced + standard compiler theory._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions