Algorithm Search⌘K

Proofs: LALR(1) parsing

These are the human proofs of LALR(1) parsing, at full length.

The LALR(1) table is the LR(1) table merged by core, and its driver parses exactly L(G) full

This proof shows: correctness.

We use the pseudocode of the page. G=(V,Σ,P,S)G = (V, \Sigma, P, S) is augmented with production 0, S′→SS' \to S, where S′S' occurs on no right side. We assume that every nonterminal derives some terminal string. Then FIRST(γ)\mathrm{FIRST}(\gamma) is not empty for every γ≠ε\gamma \ne \varepsilon, and FIRST(βa)≠∅\mathrm{FIRST}(\beta a) \ne \emptyset for every β\beta and every terminal or end marker aa.

LR(1) facts we use. The canonical LR(1) collection has the start state I0=CLOSURE1({[S′→∙S,$]})I_0 = \mathrm{CLOSURE}_1(\{[S' \to \bullet S, \$]\}) and the transitions GOTO1(J,X)=CLOSURE1({[A→αX∙β,b]:[A→α∙Xβ,b]∈J})\mathrm{GOTO}_1(J, X) = \mathrm{CLOSURE}_1(\{[A \to \alpha X \bullet \beta, b] : [A \to \alpha \bullet X \beta, b] \in J\}). We use two standard results of Knuth (1965), also in Section 4.7 of the Dragon book.

  • (K1) Validity. [A→α∙β,a]∈GOTO1(I0,γ)[A \to \alpha \bullet \beta, a] \in \mathrm{GOTO}_1(I_0, \gamma) if and only if there is a rightmost derivation S′⇒rm∗δAax⇒rmδαβaxS' \Rightarrow^*_{rm} \delta A a x \Rightarrow_{rm} \delta \alpha \beta a x with γ=δα\gamma = \delta \alpha (where axa x is read as $\$ when a=$a = \$).
  • (K2) The canonical parser. If the canonical LR(1) table has no conflict, the LR(1) driver (lines L12 to L17 with the canonical table) accepts every w∈L(G)w \in L(G) and never meets an empty entry on it. It follows the rightmost derivation of ww backwards: for each right sentential form γy\gamma y of that derivation, with y∈Σ∗y \in \Sigma^* and the handle ending at the end of γ\gamma, there is a moment at which its stack spells γ\gamma and the rest of the input is yy.

The core of a set of LR(1) items is the set of its LR(0) items.

Lemma 1 (cores). For every set KK of LR(1) items, core(CLOSURE1(K))=CLOSURE0(core(K))\mathrm{core}(\mathrm{CLOSURE}_1(K)) = \mathrm{CLOSURE}_0(\mathrm{core}(K)). Hence core(GOTO1(J,X))=GOTO0(core(J),X)\mathrm{core}(\mathrm{GOTO}_1(J, X)) = \mathrm{GOTO}_0(\mathrm{core}(J), X), and every canonical LR(1) state reachable from I0I_0 by γ\gamma has as its core the closure of the LR(0) state reachable from state 0 by γ\gamma.

Proof. Both closures add B→∙ηB \to \bullet \eta for the same items A→α∙BβA \to \alpha \bullet B \beta; the LR(1) closure adds it with the lookaheads FIRST(βa)\mathrm{FIRST}(\beta a), and this set is not empty by our assumption. So an induction on the number of closure steps shows that the two sides contain the same LR(0) items. The other two claims follow by induction on ∣γ∣|\gamma|. □\square

For an LR(0) state qq and an item ii in its closure, let LA∗(q,i)={a:[i,a]∈J\mathrm{LA}^*(q, i) = \{a : [i, a] \in J for some canonical LR(1) state JJ with core(J)=CLOSURE0(q)}\mathrm{core}(J) = \mathrm{CLOSURE}_0(q)\}. Write CqC_q for the set CLOSURE1 of lines L5 and L8, computed from the final sets LA.

Lemma 2 (no extra lookaheads). At every moment, LA[q,i]⊆LA∗(q,i)\mathrm{LA}[q, i] \subseteq \mathrm{LA}^*(q, i) for every kernel item ii of qq. Hence [i,b]∈Cq[i, b] \in C_q implies b∈LA∗(q,i)b \in \mathrm{LA}^*(q, i).

Proof. Line L2 is correct because [S′→∙S,$]∈I0[S' \to \bullet S, \$] \in I_0 and the only state with the kernel item S′→∙SS' \to \bullet S is state 0. Suppose the claim holds before line L6 adds bb to LA[GOTO(q,X),A→αX∙β]\mathrm{LA}[\mathrm{GOTO}(q, X), A \to \alpha X \bullet \beta] because of [A→α∙Xβ,b]∈C[A \to \alpha \bullet X \beta, b] \in C. The LR(1) closure has the property CLOSURE1(K)=⋃k∈KCLOSURE1({k})\mathrm{CLOSURE}_1(K) = \bigcup_{k \in K} \mathrm{CLOSURE}_1(\{k\}), because each closure step uses one item. Each item [i,a][i, a] of line L5 lies in some canonical state JJ with core qq, by the hypothesis, and JJ is closed, so CLOSURE1({[i,a]})⊆J\mathrm{CLOSURE}_1(\{[i, a]\}) \subseteq J. So [A→α∙Xβ,b][A \to \alpha \bullet X \beta, b] lies in such a JJ. Then [A→αX∙β,b]∈GOTO1(J,X)[A \to \alpha X \bullet \beta, b] \in \mathrm{GOTO}_1(J, X), whose core is GOTO0(q,X)\mathrm{GOTO}_0(q, X) by Lemma 1, so b∈LA∗(GOTO(q,X),A→αX∙β)b \in \mathrm{LA}^*(\mathrm{GOTO}(q, X), A \to \alpha X \bullet \beta). The second claim follows by the same argument for CqC_q. □\square

Lemma 3 (no missing lookaheads). After line L7, every canonical LR(1) state JJ with core qq satisfies J⊆CqJ \subseteq C_q. Hence LA∗(q,i)={b:[i,b]∈Cq}\mathrm{LA}^*(q, i) = \{b : [i, b] \in C_q\}.

Proof. By induction on the length of a shortest path from I0I_0 to JJ. The kernel of I0I_0 is [S′→∙S,$][S' \to \bullet S, \$], and $∈LA[0,S′→∙S]\$ \in \mathrm{LA}[0, S' \to \bullet S] by line L2; CLOSURE1\mathrm{CLOSURE}_1 is monotone, so I0⊆C0I_0 \subseteq C_0. Let J=GOTO1(J′,X)J = \mathrm{GOTO}_1(J', X) with J′⊆Cq′J' \subseteq C_{q'} by induction, where q′=core(J′)q' = \mathrm{core}(J'). Each kernel item of JJ is [A→αX∙β,b][A \to \alpha X \bullet \beta, b] with [A→α∙Xβ,b]∈J′⊆Cq′[A \to \alpha \bullet X \beta, b] \in J' \subseteq C_{q'}. When line L7 ends, a full pass has changed nothing, so the pass of line L6 for q′q' found bb already in LA[GOTO(q′,X),A→αX∙β]\mathrm{LA}[\mathrm{GOTO}(q', X), A \to \alpha X \bullet \beta], and GOTO(q′,X)=q\mathrm{GOTO}(q', X) = q by Lemma 1. So the kernel of JJ is in the set given to CLOSURE1 for qq, and by monotonicity J⊆CqJ \subseteq C_q. The equality then follows from Lemma 2. □\square

The loop of lines L3 to L7 ends, because each pass that does not end it adds at least one element to a set LA, and these sets are subsets of a finite set.

Corollary 4. For every state qq and terminal or end marker aa, the entry ACTION[q,a]\mathrm{ACTION}[q, a] of lines L8 to L10 is the union of the entries ACTION1[J,a]\mathrm{ACTION}_1[J, a] of the canonical LR(1) table over the states JJ with core qq, where a shift to J′′J'' is read as a shift to core(J′′)\mathrm{core}(J''). In particular, if the LALR table has no conflict, neither has the canonical table, and GG is LR(1).

Proof. The canonical table puts shift on aa in JJ when an item with ∙a\bullet a is in JJ, and reduce or accept on aa when a completed item with lookahead aa is in JJ. By Lemmas 2 and 3, CqC_q is the union of these JJ, and lines L9 and L10 read the same items. Shift targets agree by Lemma 1. Two different actions in some ACTION1[J,a]\mathrm{ACTION}_1[J, a] would appear in ACTION[q,a]\mathrm{ACTION}[q, a]. □\square

From now on, line L11 has passed, so every entry has at most one action, and by Corollary 4 the grammar is LR(1). LR(1) grammars are unambiguous (Knuth 1965).

Lemma 5 (soundness). At every moment of the driver, let the stack hold the states q0q1⋯qmq_0 q_1 \cdots q_m, with symbols X1,…,XmX_1, \ldots, X_m, where XkX_k is the symbol on the transition into qkq_k. Then q0=0q_0 = 0, qk=GOTO(qk−1,Xk)q_k = \mathrm{GOTO}(q_{k-1}, X_k), and the value stack holds trees T1,…,TmT_1, \ldots, T_m, where TkT_k is a derivation tree with root XkX_k, and the leaves of T1,…,TmT_1, \ldots, T_m in order spell the consumed input a1⋯ai−1a_1 \cdots a_{i-1}. If the driver returns a tree at line L16, it is a parse tree of ww.

Proof. Line L12 makes the claim true. Line L14 pushes GOTO(qm,a)\mathrm{GOTO}(q_m, a) with the leaf aa. At line L15, the reduction by A→βA \to \beta is in ACTION[qm,a]\mathrm{ACTION}[q_m, a] because the completed item A→β∙A \to \beta \bullet is in the closure of qmq_m. In the LR(0) automaton, an item A→β′∙β′′A \to \beta' \bullet \beta'' in the closure of GOTO(q,X)\mathrm{GOTO}(q, X) with β′≠ε\beta' \ne \varepsilon is a kernel item that came from A→β′′′∙Xβ′′A \to \beta''' \bullet X \beta'' in the closure of qq with β′=β′′′X\beta' = \beta''' X. So, going down the stack, the top ∣β∣|\beta| symbols are β\beta, and there are at least ∣β∣|\beta| of them above q0q_0. The new tree has root AA, its children are the popped trees in order, and it uses the production A→βA \to \beta; the leaves keep their order. The new state is GOTO\mathrm{GOTO} of the exposed state on AA, which exists because the exposed state has A→∙βA \to \bullet \beta in its closure and so an item with ∙A\bullet A in its closure.

At line L16 the closure of qmq_m contains S′→S∙S' \to S \bullet and the lookahead is $\$, so all of ww is consumed. The item S′→S∙S' \to S \bullet comes only from S′→∙SS' \to \bullet S, which is only in state 0, because S′S' is on no right side. No transition enters state 0, because every non-start kernel item has its dot after position 0. So the stack is 0 q10\, q_1 with X1=SX_1 = S, and T1T_1 is a parse tree of ww. □\square

Lemma 6 (completeness). If w∈L(G)w \in L(G), the driver accepts ww.

Proof. Run the canonical LR(1) driver on ww next to the LALR driver. We show by induction on the number of moves that, before each move, the LALR stack is core(J0)⋯core(Jm)\mathrm{core}(J_0) \cdots \mathrm{core}(J_m) when the LR(1) stack is J0⋯JmJ_0 \cdots J_m, and both have read the same input. This holds at the start. By (K2) the LR(1) driver has an action tt in ACTION1[Jm,a]\mathrm{ACTION}_1[J_m, a]. By Corollary 4, tt (with shift targets mapped to their cores) is in ACTION[core(Jm),a]\mathrm{ACTION}[\mathrm{core}(J_m), a], which has no other action, so the LALR driver makes the same move. A shift pushes core(GOTO1(Jm,a))=GOTO(core(Jm),a)\mathrm{core}(\mathrm{GOTO}_1(J_m, a)) = \mathrm{GOTO}(\mathrm{core}(J_m), a) by Lemma 1. A reduction pops the same number of states from both stacks and pushes GOTO1(Jk,A)\mathrm{GOTO}_1(J_k, A) and GOTO(core(Jk),A)\mathrm{GOTO}(\mathrm{core}(J_k), A), which correspond by Lemma 1. By (K2) the LR(1) driver accepts, so the LALR driver accepts at the same move. □\square

Lemma 7 (termination). The driver stops on every input.

Proof. There are at most n+1n + 1 shifts, so it is enough to show that no run of reductions without a shift is infinite. Suppose that one is, with lookahead aa from time t0t_0 on. Each reduction pops the top ∣β∣|\beta| entries and then pushes one; call the stack length after the pops the low point of the move. Let b(t)b(t) be the least low point of the moves at or after tt. It never decreases.

Claim T. Let t≥t0t \ge t_0 and let ee be the top entry at time tt. Within a bounded number of moves, the driver pops ee. Let qq be its state. The driver makes a reduction at time tt, so some completed item [A→β∙][A \to \beta \bullet] in the closure of qq has a∈LA∗(q,A→β∙)a \in \mathrm{LA}^*(q, A \to \beta \bullet) by Lemma 2: there is a canonical state JJ with core qq that contains [A→β∙,a][A \to \beta \bullet, a]. Let JJ be reached by γ\gamma. By (K1), S′⇒rm∗δAax⇒rmγaxS' \Rightarrow^*_{rm} \delta A a x \Rightarrow_{rm} \gamma a x. Each nonterminal derives a terminal string, so γax\gamma a x derives a sentence z=uax′z = u a x' with γ⇒∗u\gamma \Rightarrow^* u, through a rightmost derivation in which γax\gamma a x occurs. The canonical driver on zz follows this rightmost derivation backwards, so at some moment its stack spells γ\gamma with top state JJ and lookahead aa. From that moment, compare it with the LALR driver from time tt, restricted to the entries above ee and above JJ. As in Lemma 6, by (K2) and Corollary 4 the two drivers make the same moves, with the LALR states the cores of the LR(1) states, until one of them pops its base entry (JJ or ee), and then the other pops its base entry at the same move, or until the LR(1) driver shifts aa, and then the LALR driver shifts aa. The LR(1) driver accepts zz in finitely many moves, so one of these events happens within a bound B(q,a)B(q, a) that does not depend on tt. A shift is excluded, so ee is popped.

Case 1: b(t)b(t) grows without bound. Let k=b(t0)k = b(t_0), and let tkt_k be the last move at or after t0t_0 with low point kk; it exists because some move has low point kk and b(t)>kb(t) > k from some time on. Every later move has a low point above kk, so the entry at position k+1k + 1, which the move at tkt_k pushed, is never popped. It is the top entry right after tkt_k, and this contradicts Claim T.

Case 2: b(t)=Mb(t) = M from some time t1t_1 on. The entries at positions 1,…,M1, \ldots, M are never popped after t1t_1, and infinitely many moves have low point MM and push an entry at position M+1M + 1. Each such entry has the symbol of the nonterminal it reduces to, so two of these moves, at times t2<t3t_2 < t_3, push trees T2T_2 and T3T_3 with the same root AA. The consumed input does not change, so the trees above position MM span the same input before and after. Trees are never destroyed: a popped tree becomes a child of the new tree. Between t2t_2 and t3t_3 no move has a low point below MM, so T2T_2 stays inside some tree above position MM, and the move at t3t_3 pops all of these trees into T3T_3. So T2T_2 is a subtree of T3T_3. T3≠T2T_3 \ne T_2, and both span the same input. So A⇒+μAνA \Rightarrow^+ \mu A \nu with μν⇒∗ε\mu \nu \Rightarrow^* \varepsilon. Since AA derives a terminal string and occurs in a sentential form (it is the root of a tree in a viable stack, by Lemma 5 and Lemma 1), repeating this derivation gives infinitely many parse trees for one sentence, so GG is ambiguous. This contradicts the remark after Corollary 4. □\square

Theorem. If line L11 finds no conflict, LALR-PARSE returns a parse tree of ww if w∈L(G)w \in L(G) and "reject" otherwise. If it finds a conflict, GG has no conflict-free LALR(1) table and the parser reports this.

Proof. By Lemma 7 the driver stops, at line L16 or line L17. By Lemma 6 it stops at line L16 when w∈L(G)w \in L(G), and by Lemma 5 it stops at line L16 only when w∈L(G)w \in L(G), with a parse tree. The table of lines L1 to L10 is determined by GG, so a conflict means that GG is not LALR(1). ■\blacksquare

Back to LALR(1) parsing