
In bottom-up parsing (especially LR parsing) , a shift-reduce conflict is one of the most important problems asked in exams.
In bottom-up parsing (especially LR parsing), a shift-reduce conflict is one of the most important problems asked in exams.
A shift-reduce conflict occurs in parsing when the parser is unable to decide whether to:
π This happens for the same state and input symbol in the parsing table.
π The parser is confused:
βShould I read more input (shift) or reduce what I already have?β
E β E + E
E β E * E
E β id
id + id * id
At a certain parser state:
Stack contains:
E + E
Next input symbol:
Now parser faces:
* (to handle multiplication first)E + E β Eπ β Conflict arises
Example:
E β E + E | E * E | id
π No clear precedence or associativity
Parser doesnβt know:
* has higher priority than +LR(0) has no lookahead, so it cannot decide correctly.
π Shift-reduce conflict means:
βShould I complete a rule now OR wait for more input?β
| State | Input | Action |
|---|---|---|
| i | + | Shift / Reduce β |
π Both actions are possible β conflict
Caused by poor grammar design
Can be fixed using:
Rewrite grammar:
E β E + E | E * E | id
E β E + T | T
T β T * F | F
F β id
* has higher priority than +a - b - c β (a - b) - c
| Situation | Action |
|---|---|
| Input should continue | Shift |
| Rule complete | Reduce |
| Unclear grammar | Conflict β |
π Imagine reading a sentence:
Conflict =
βShould I continue reading or interpret what I already have?β
π Very important:
| Aspect | Shift | Reduce | Conflict |
|---|---|---|---|
| Meaning | Move input to stack | Apply grammar rule | Decision problem |
| Purpose | Read input | Simplify stack | Parsing issue |
| Problem | None alone | None alone | Happens in LR parsing |
| Cause | N/A | N/A | Ambiguity / LR(0) |
| Solution | N/A | N/A | Grammar fix / LR(1) |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.