Sub topic 14
Conflicts and the Power Hierarchy of Parsers
This chapter assumes you know context-free grammars, FIRST and FOLLOW sets, stacks, and the basic shift-reduce process. A parser conflict means that its parsing table gives more than one valid action for the same parser state and next input token. Conflicts usually indicate that the parser has insufficient information, or that the grammar is ambiguous.
The main kinds of conflicts
A shift-reduce conflict occurs when the parser can either shift the next token or reduce a completed production. For example, after reading id + id, an expression parser may need to choose between reducing id + id or shifting * in a longer expression.
A reduce-reduce conflict occurs when two completed productions can both be reduced. This usually signals overlapping grammar rules or ambiguity and is more serious than a shift-reduce conflict.
| Conflict | Competing actions | Typical cause |
|---|---|---|
| Shift-reduce | Shift or reduce | Missing precedence or lookahead |
| Reduce-reduce | Reduce by rule 1 or rule 2 | Ambiguous or poorly separated grammar |
In LR parsing, a conflict is reported for a particular state and lookahead token. In LL(1) parsing, a conflict appears when two productions would be placed in the same table cell.
def conflict(actions):
"""Return the conflict type for the actions in one table cell."""
kinds = {action[0] for action in actions}
if len(actions) <= 1:
return "no conflict"
if kinds == {"s", "r"}:
return "shift-reduce conflict"
if kinds == {"r"}:
return "reduce-reduce conflict"
return "other conflict"
print(conflict(["s5", "r3"]))
print(conflict(["r2", "r4"]))Parser power hierarchy
The common LR hierarchy adds more context to parsing decisions:
- LR(0) uses no lookahead when deciding reductions.
- SLR(1) uses FOLLOW sets to restrict LR(0) reductions.
- LALR(1) merges compatible LR(1) states with the same LR(0) core.
- CLR(1), also called canonical LR(1), keeps separate one-token lookahead information.
More information usually removes conflicts, but it also increases table size.
The usual grammar-class relationship is:
LR(0) ⊂ SLR(1) ⊆ LALR(1) ⊆ CLR(1)
The inclusion means every grammar handled by a weaker class is also handled by the stronger class, although the reverse is not guaranteed. LL(1) is a separate top-down class. It is often simpler to implement, but it cannot use the same broad range of left-recursive grammars that LR parsers handle naturally.
> callout warning > A parser generator may resolve a shift-reduce conflict using default rules, such as shifting. This can produce a parser that runs while silently applying the wrong precedence or associativity. Treat unexplained conflicts as grammar defects.
Choosing a parser
Use LL(1) or recursive descent when the grammar has clear predictive choices and readable hand-written code matters. Use LALR(1) when you want compact generated tables and the grammar resembles those used by practical programming languages. Use CLR(1) when correctness requires precise lookahead and table size is acceptable.
Exercise
Which parser class should you try when an SLR(1) table has a conflict but the grammar's LR(1) states have different lookaheads?
Show answer
Try CLR(1), or LALR(1) if merging states with the same LR(0) core does not recreate the conflict.