Sub topic 3
CLR(1) and LALR(1) Parsers
This chapter assumes you know LR(0) items, SLR(1) tables, FIRST and FOLLOW sets, parse trees, and the basic idea of syntax-directed definitions.
CLR(1) and LALR(1)
CLR(1) is canonical LR(1) parsing. Each item carries a lookahead terminal, written [A -> α.β, a]. A reduce action is placed only on lookaheads that can really follow in that context. This removes many SLR(1) conflicts, but the table has many states.
LALR(1) is lookahead LR(1). It merges CLR(1) states that have the same LR(0) core. The result has as few states as SLR(1), with most of the lookahead precision of CLR(1).
| Parser | Lookahead source | State count | Typical conflicts |
|---|---|---|---|
| LR(0) | none | fewest | shift-reduce, reduce-reduce |
| SLR(1) | FOLLOW sets | same as LR(0) | fewer |
| LALR(1) | merged LR(1) lookaheads | same as LR(0) | reduce-reduce from merging |
| CLR(1) | exact LR(1) lookaheads | most | fewest |
::: callout note Merging can create reduce-reduce conflicts that CLR(1) does not have, so an LALR(1) table may be less accurate than a CLR(1) table for the same grammar. :::
Conflicts and the power hierarchy
A shift-reduce conflict occurs when a table cell allows both shift and reduce. A reduce-reduce conflict occurs when two productions can reduce on the same lookahead. A grammar belongs to a parser's class only if that parser's table has no conflicts.
Operator precedence parsing
Operator precedence parsing is a bottom-up method for operator grammars, where no production has two adjacent nonterminals. For each pair of adjacent terminals, it uses a relation (<. , =. , or .>) taken from precedence and associativity rules. Its tables are small, but it accepts fewer grammars than LR parsers.
Syntax error detection and recovery
An LR parser reports an error when its ACTION table entry is empty. LALR(1) may perform some extra reductions before it detects an error, but it never shifts invalid input. Panic-mode recovery pops states until one can act on a synchronizing token such as ";" or "}", then discards input up to that token.
Syntax-directed definitions
Each grammar symbol carries attributes. A synthesized attribute is computed from the node's children. An inherited attribute is passed down from the parent or from left siblings.
E -> E1 + T { E.val = E1.val + T.val } // E.val is synthesized
T -> id { T.val = id.lexval }Dependency graphs, S-attributed and L-attributed definitions
A dependency graph has one node for each attribute instance in a parse tree. An edge runs from each attribute to the attributes it uses. Evaluate attributes in topological order. A cycle means the values are undefined.
An S-attributed definition uses only synthesized attributes, so one bottom-up pass evaluates it. An L-attributed definition lets an inherited attribute depend only on the parent and on left siblings, so one left-to-right depth-first pass evaluates it.
Syntax-directed translation schemes and semantic action placement
A syntax-directed translation (SDT) scheme places actions in braces inside production bodies. In an S-attributed scheme, actions go at the right end of the body, so an LR parser runs them on each reduction. In an L-attributed scheme, actions may appear mid-body, which suits LL parsing and recursive descent.
E -> E + T { print('+') }
T -> T * F { print('*') }
F -> digit { print(digit.lexval) }With the other productions unchanged, the input 2 + 3 * 4 prints 2 3 4 * +.
Applications
Actions can evaluate an expression by computing values, produce postfix output by printing, or build a syntax tree by creating a node for each operator, such as mknode('+', E1, T).
Exercise
Exercise
A grammar has an SLR(1) table with no conflicts. Must its LALR(1) table also be conflict-free? Explain briefly.
Show answer
Yes. LALR(1) lookahead sets are subsets of the FOLLOW sets that SLR(1) uses, so every reduce entry in the LALR(1) table is also in the SLR(1) table. Without SLR(1) conflicts, the LALR(1) table has none either.