Explaining LR(1) item construction and lookahead propagation
LR(1) items are productions with a dot indicating the parse position plus a lookahead terminal; closure adds items for a nonterminal after the dot with lookaheads from FIRST of the following symbols plus current lookahead. This precise lookahead propagation keeps states distinct, unlike LALR(1) which merges states by core and can introduce conflicts, making LR(1) strictly more powerful.
An LR item records not just "how far through a production have I seen?", but also "under what next input symbols is it valid to reduce here?". In the one–symbol case an item is usually written in bracket notation as
[
[A \to \alpha;\cdot;\beta,;a]
]
which should be read as “I am in the middle of the production (A\to\alpha\beta), with (\alpha) on the top of the stack, (\beta) still to be seen, and it will be legal to reduce by this production when the next input symbol is (a)”. The extra lookahead component is what distinguishes LR(1) from LR(0).
The canonical collection of LR(1) items is built in the same graph–construction style as for LR(0) or SLR:
- Augment the grammar with a new start (S'\to S).
- Start from the set (I_0=\operatorname{closure}(,[S'\to\cdot S,\ $],)).
- For each item set (I) and each grammar symbol (X) that appears immediately after a dot in some item in (I), define
[
\operatorname{goto}(I,X) = \operatorname{closure}({[A\to\alpha X\cdot\beta,a] \mid [A\to\alpha\cdot X\beta,a]\in I}).
]
If (\operatorname{goto}(I,X)) is non‑empty and not already in the collection, add it and record a transition from (I) on (X).
The key difference from LR(0) lies in the closure operation. Given a set of items (I), (\operatorname{closure}(I)) is obtained by repeatedly adding new items until no more can be added. Whenever (I) contains an item ([A\to\alpha\cdot B\beta, a]) with a nonterminal (B) after the dot, closure adds, for every production (B\to\gamma), the item ([B\to\cdot\gamma, b]) for each terminal (b) in (\mathsf{FIRST}(\beta a)): the set of terminals that can begin a string derived from (\beta) followed by the lookahead (a). Intuitively, if you are about to expand (B) in order to eventually see (\beta) and then (a), any terminal that could begin (\beta) or, if (\beta) can be empty, (a) itself is a valid lookahead for the (B)‑productions. This is how lookahead information is propagated down into the items introduced by closure.
Carrying this one‑symbol lookahead all the way through the construction keeps states that are distinguishable only by their future input separate. The LALR(1) construction, by contrast, starts from the LR(1) items but then merges any two item sets that have the same LR(0) “core” ([A\to\alpha\cdot\beta]), unifying their lookahead sets. For many practical grammars this causes no harm and produces a much smaller table, but the merging can introduce spurious shift/reduce or reduce/reduce conflicts because distinctions based on lookahead have been lost. There are well‑known examples of grammars that have a conflict‑free canonical LR(1) automaton but whose core‑merged LALR(1) automaton has conflicts. That is what is meant by saying LR(1) is strictly more powerful: every LALR(1) grammar is LR(1), but there are LR(1) grammars that cannot be parsed by any LALR(1) parser.