Sub topic 17
Conflicts and the power hierarchy of parsers
This chapter assumes you can build LR(0) item sets, compute FIRST and FOLLOW, and fill an SLR(1) table. It covers what a conflict is, why the four LR variants differ in how many conflicts they report, and how LL(1) relates to them. GATE asks for this in two forms: "is this grammar SLR(1), LALR(1), both, or neither?" and "which of these inclusions is true?"
What a conflict is
A parsing table cell must hold at most one action. A grammar fails a parser's test when some cell would hold two.
- A shift-reduce conflict means that in one state, on one lookahead terminal, the table could shift or reduce.
- A reduce-reduce conflict means that in one state, on one lookahead terminal, two different productions could be reduced.
- A shift-shift conflict cannot occur. The goto on a symbol leads to exactly one state.
The parsers differ only in which lookaheads they allow a reduction on.
| Parser | Reduces `A -> α.` on | Items carry lookahead |
|---|---|---|
| LR(0) | every terminal | no |
| SLR(1) | terminals in FOLLOW(A) | no |
| LALR(1) | the merged LR(1) lookahead set | yes, merged by core |
| CLR(1) | the exact LR(1) lookahead set | yes, per state |
A smaller reduce set means fewer chances to collide with a shift or with another reduction.
Spotting conflicts in an item set
For each state, apply these checks in order:
- If a state has a complete item
A -> α.and another itemB -> β.aγwith terminalaafter the dot, there is a shift-reduce conflict onawhen the parser's reduce set forA -> α.containsa. For LR(0) the reduce set is every terminal, so any such state conflicts. - If a state has two complete items
A -> α.andB -> β., there is a reduce-reduce conflict when their reduce sets intersect. For LR(0) they always intersect. - If neither check fires in any state, the grammar passes that parser's test.
A grammar that is LALR(1) but not SLR(1)
S -> L = R | R
L -> * R | id
R -> LOne LR(0) state contains these items:
S -> L . = R
R -> L .FOLLOW(R) contains =. This is because L -> * R puts FOLLOW(L) into FOLLOW(R), and FOLLOW(L) contains = from S -> L = R. SLR(1) therefore wants to reduce R -> L on = and also shift =, which is a shift-reduce conflict.
In the canonical LR(1) item set, the item R -> L . in this state has lookahead $ only. Reducing on = would never lead to a valid sentential form here. CLR(1) and LALR(1) see this, so the conflict disappears.
A grammar that is CLR(1) but not LALR(1)
S -> a A d | b B d | a B e | b A e
A -> c
B -> cCLR(1) builds two separate states after reading c:
after a c: A -> c . , d B -> c . , e
after b c: A -> c . , e B -> c . , dLALR(1) merges states with the same LR(0) core. The two states above have the same core {A -> c., B -> c.}, so they merge:
A -> c . , {d, e} B -> c . , {d, e}Both reductions are now allowed on d and on e. This is a reduce-reduce conflict that exists only because of merging.
The power hierarchy
The classes of grammars accepted satisfy a strict chain:
LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1) = LR(1)
Each parser tests a grammar with a stricter or looser reduce set, so every grammar passing a weaker test also passes the next one.
Some further facts to memorize:
- LR(0), SLR(1), and LALR(1) tables for the same grammar have the same number of states. CLR(1) usually has more.
- Every LL(1) grammar is LR(1), but an LL(1) grammar need not be LR(0), SLR(1), or LALR(1). For example,
S -> A a A b | B b B a,A -> ε,B -> εis LL(1) but not SLR(1). - LL(1) and LR(0) grammars are not comparable. A grammar can be in one and not the other.
- No ambiguous grammar is LR(1), and none is LL(1). Ambiguity always produces an unresolvable conflict.
- A grammar with left recursion is never LL(1). Left recursion does not hurt LR parsers.
- As language classes, LL(1) languages are properly contained in the deterministic context-free languages. Those are exactly the languages of LR(1) grammars, and also of LR(k) grammars for any k.
Resolving conflicts without changing the grammar
Real generators such as yacc accept ambiguous grammars and resolve conflicts with rules. In a shift-reduce conflict, the default is to shift. Precedence and associativity declarations for operators override that default. In a reduce-reduce conflict, the default is to reduce by the production listed first. This is how the dangling-else ambiguity is usually handled.
Common errors
- Using FOLLOW sets for LALR(1) or CLR(1). They need the item lookaheads, not FOLLOW.
- Declaring a conflict on a terminal that is not in the reduce set. Check the set before deciding.
- Forgetting that a state with a single complete item and no shifts is fine in SLR(1) but fails LR(0) only if another item exists in that state.
- Assuming that "LALR(1) fails" implies "CLR(1) fails". Only the reverse holds.
Exercise
A grammar has an LR(0) state with items X -> a . and Y -> b .. FOLLOW(X) = {c, $} and FOLLOW(Y) = {d, $}. Is the grammar SLR(1) at this state, and is it LR(0) at this state?
Show answer
It is not LR(0) here, because two complete items force a reduce-reduce conflict on every terminal. The SLR(1) check intersects the reduce sets {c, $} and {d, $}. The intersection is {$}, so there is a reduce-reduce conflict on $ and the grammar is not SLR(1) at this state.
Exercise
Which statement is true? (a) Every SLR(1) grammar is LR(0). (b) Every LALR(1) grammar is SLR(1). (c) Every SLR(1) grammar is CLR(1). (d) Every LL(1) grammar is LALR(1).
Show answer
(c). By the hierarchy LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1), so SLR(1) implies CLR(1). Statements (a) and (b) reverse the inclusions. Statement (d) is false because LL(1) implies LR(1) but not LALR(1).