GLR accepts exactly the words of the grammar and returns a parse tree full
This proof shows: correctness.
Notation. G=(V,Σ,P,S) is any context-free grammar, augmented with a new start symbol S′ and the production S′→S, and w=a1⋯an. For 0≤i≤j≤n, write wi,j=ai+1⋯aj. Items, CLOSURE, GOTO, the states Q and q0 are as on the page. For a string γ=X1⋯Xk, δ(q,γ)=GOTO(⋯GOTO(q,X1)⋯,Xk) when every step is defined, and δ(q,ε)=q. A node v of the GSS has a state state(v) and a level level(v). A path of m edges from u is a sequence u=y0,y1,…,ym with an edge yr−1→yr for each r; nodes may repeat.
Facts about the LR(0) automaton
Fact 1 (dot positions). CLOSURE adds only items with the dot at the start. So every item with the dot after the start in GOTO(q,X) has the form A→βX∙γ, with A→β∙Xγ∈q. Such an item exists, since GOTO is defined only for a nonempty kernel. Hence every state s=GOTO(q,X) determines X, its accessing symbol X(s), and q0, which has no item with the dot after the start, is never a value of GOTO.
Fact 2 (items move back to the start). If A→β1∙β2∈δ(t,γ) and ∣γ∣=∣β1∣, then γ=β1 and A→∙β1β2∈t. In particular, if A→α∙∈δ(t,γ) and ∣γ∣=∣α∣, then γ=α and A→∙α∈t.
Proof. By induction on d=∣β1∣. For d=0, γ=ε and δ(t,γ)=t. For d>0, write γ=γ′X. The item has the dot after the start and is in GOTO(δ(t,γ′),X), so by Fact 1, β1=β1′X and A→β1′∙Xβ2∈δ(t,γ′), with ∣γ′∣=∣β1′∣. By induction, γ′=β1′ and A→∙β1′Xβ2∈t. So γ=β1. □
Fact 3 (valid items are present). If S′⇒rm∗δAz⇒rmδβ1β2z is a rightmost derivation, then δ(q0,δβ1) is defined and contains A→β1∙β2.
Proof. By the definition of GOTO, if B→η1∙Xη2∈q, then GOTO(q,X) is defined and contains B→η1X∙η2. Applying this ∣β1∣ times, it suffices to show A→∙β1β2∈δ(q0,δ). We use induction on the length of the derivation S′⇒rm∗δAz. If it has zero steps, then A=S′, δ=ε, and S′→∙S∈q0. Otherwise, consider the step that introduced this occurrence of A: S′⇒rm∗δ′Bz′⇒rmδ′η1Aη2z′⇒rm∗δAz. In a rightmost derivation, no symbol to the left of a nonterminal that is not yet rewritten is ever rewritten, so the steps after the introduction rewrite only η2, and δ=δ′η1. The derivation of δ′Bz′ is shorter, so by induction B→∙η1Aη2∈δ(q0,δ′). Moving the dot over η1 gives B→η1∙Aη2∈δ(q0,δ), and the closure then contains A→∙β1β2. □
Soundness
Lemma 1. For every edge v→x with v=(s,j) and x=(t,i): i≤j, s=GOTO(t,X(s)), and the tree on the edge is a parse tree of X(s) with yield wi,j (for a terminal, the leaf itself).
Proof. By induction on the order in which edges are made. A shift edge (line L12) goes from (s,j+1) to u∈Uj, with s=GOTO(state(u),aj+1). By Fact 1, X(s)=aj+1, and its leaf has yield wj,j+1.
A reduction edge (line L10) comes from an entry (u,A→α,e) with u∈Uj and A→α∙∈state(u), and from a path u=y0,…,ym=x with m=∣α∣, all of whose edges were made earlier. By induction, state(yr−1)=GOTO(state(yr),Xr) with Xr=X(state(yr−1)), and level(yr)≤level(yr−1). So state(u)=δ(state(x),γ) with γ=Xm⋯X1, and by Fact 2, γ=α and A→∙α∈state(x). Hence s=GOTO(state(x),A) is defined, and X(s)=A by Fact 1. The levels give i=level(x)≤level(u)=j. The trees on the edges ym−1→ym,…,y0→y1 are parse trees of Xm,…,X1 with yields wlevel(ym),level(ym−1),…,wlevel(y1),level(y0), which concatenate to wi,j. With these children, bottom first, in the order of α, the new tree is a parse tree of A with yield wi,j. □
Corollary. If line L13 returns true, then the edge from (GOTO(q0,S),n) to the root carries a parse tree of X(GOTO(q0,S))=S with yield w0,n=w, so S⇒∗w.
Termination
Each level has at most ∣Q∣ nodes, and the levels are 0,…,n, so there are finitely many nodes and edges. In phase j, line L4 queues finitely many entries, line L8 queues entries only when it makes a node, and line L9 only when it makes an edge. So finitely many entries are queued. For each entry, line L7 enumerates finitely many paths, since they have ∣α∣ edges and each node has finitely many out-edges; the set of paths is fixed when the entry is removed. So every phase ends.
Completeness
Edges are made only in two places. Line L10 in phase j makes edges whose source is at level j; line L12 in phase j makes edges whose source is at level j+1. A path from a node of level j never goes up a level (Lemma 1), so it uses only edges with a source at level at most j. Hence after the loop of lines L5 to L10 in phase j ends, no edge on a path from a node of level j is ever made again, and Uj never changes again.
Lemma 2 (saturation). When the loop of lines L5 to L10 of phase j ends, the following holds. For every u∈Uj, every A→α∈R(state(u)), and every path P of m=∣α∣ edges from u to a node x, the node v=(GOTO(state(x),A),j) exists and has the edge v→x.
Proof. It suffices to show that some entry (u,A→α,e) is removed at line L6 after all edges of P exist, with e none or an edge of P. Then line L7 enumerates P, and lines L8 and L10 make v and the edge if they are missing. (GOTO is defined here by the proof of Lemma 1, which shows A→∙α∈state(x).) The phase ends only when the queue is empty, so every queued entry is removed.
If m=0, then P has no edges and x=u. The node u is in Uj either at the start of the phase, and then line L4 queues (u,A→α,none), or it is made by line L8, which queues the same entry. Now let m≥1, and let f be the edge of P that was made last.
Case (a): f existed before the loop of phase j began. Then all edges of P existed, so did their sources, and u∈Uj at line L4, which queued (u,A→α,none).
Case (b): f was made in the loop, at line L10, in the same step in which line L8 made its source v′. Let fr be the first occurrence of f in P. If r>1, the edge fr−1 ends at v′ and is different from f, because f ends at a node that existed before v′, while fr−1 ends at v′. So fr−1 was made before f, but after v′ was made, and nothing is made between line L8 and line L10 of one step. This is impossible, so r=1 and u=v′. When line L8 made u, it queued (u,A→α,none), which is removed after this step, when all edges of P exist.
Case (c): f was made in the loop, at line L10, from a source v′ that already existed. Then the edge was new, and line L9 of the same step queued (u′,r,f) for every u′∈Uj and every r∈R(state(u′)) with a nonempty right side. The node u was in Uj at that time: it is v′ if f is the first edge of P, and otherwise it is the source of the first edge of P, which was made before f. Since m≥1, the entry (u,A→α,f) was queued, and it is removed after f is made, when all edges of P exist. The path P uses f. □
Lemma 3 (the stacks of a rightmost derivation). Let S=φ0⇒rmφ1⇒rm⋯⇒rmφt=w, with t≥1, where step k rewrites φk=δkAkzk (zk∈Σ∗) by Ak→αk. Consider the sequence of configurations (γ,i) of a shift-reduce parser that starts at (ε,0) and, for k=t−1 down to 0, shifts input symbols until (γ,i)=(δkαk,n−∣zk∣), then reduces to (δkAk,n−∣zk∣). Then this sequence is well defined, ends at (S,n), and γai+1⋯an is a right sentential form for each configuration. Also, δ(q0,γ) is defined for every γ in it.
Proof. We show by downward induction on k that before the shifts for step k, the configuration (γ,i) has γai+1⋯an=φk+1 and γ a prefix of δkαk. Then shifting brings the stack to δkαk, since φk+1=δkαkzk, and the reduction gives δkAk with δkAkzk=φk. For k=t−1, the configuration is (ε,0) and φt=w. For the step from k to k−1: φk=δk−1αk−1zk−1, and Ak is the rightmost nonterminal of φk. Since zk−1 has no nonterminal, Ak lies in δk−1αk−1, so δkAk is a prefix of δk−1αk−1. At the end, φ0=S gives δ0=ε, A0=S, z0=ε, and the configuration (S,n).
Each stack γ of the sequence is S or a prefix of some δkαk. The derivation S′⇒rmS⇒rm∗δkAkzk⇒rmδkαkzk and Fact 3 show that δ(q0,δkαk) is defined, so it is defined on all prefixes. Also GOTO(q0,S) is defined, since S′→∙S∈q0. □
For a configuration (X1⋯Xk,i) of this sequence, let lr be the input position of the configuration just after Xr was pushed, so l1≤⋯≤lk≤i, and let l0=0. We say the configuration is in the GSS if, for vr=(δ(q0,X1⋯Xr),lr), the nodes v0,…,vk and the edges vr→vr−1 exist for 1≤r≤k, where v0 is the root.
Lemma 4. Every configuration (γ,i) of the sequence of Lemma 3 is in the GSS, with all its nodes and edges made by the end of the loop of lines L5 to L10 of phase i.
Proof. By induction along the sequence. The first configuration (ε,0) is the root, made at line L2. Note that every node of γ at level i and every edge with a source at level i is made in phase i−1 at line L12 or in phase i at line L10, so it already exists at the end of that loop if it exists at all.
Shift from (γ,i) to (γai+1,i+1). By induction, the top node u=(δ(q0,γ),i) is in Ui when line L11 of phase i runs. The state s=GOTO(state(u),ai+1)=δ(q0,γai+1) is defined by Lemma 3, so line L12 makes the node (s,i+1) and the edge to u. This happens before phase i+1 starts.
Reduction from (δα,i) to (δA,i) by A→α, with m=∣α∣. By induction, the path v0,…,v∣δ∣+m exists at the end of the loop of phase i, and its top node u has level i. The last m edges of it form a path of m edges from u to x=v∣δ∣=(δ(q0,δ),l∣δ∣). The item A→α∙ is valid for δα (Lemma 3 with Fact 3), so it is in state(u)=δ(q0,δα), and A→α∈R(state(u)). By Lemma 2, the node (GOTO(state(x),A),i)=(δ(q0,δA),i) exists, with an edge to x. The symbol A was pushed at position i, so this is the node v∣δ∣+1 of the new configuration, and the nodes below it are unchanged. □
Theorem. GLR terminates, returns true if and only if S⇒∗w, and when it returns true, the tree it returns is a parse tree of w from S.
Proof. Termination is shown above. If GLR returns true, then S⇒∗w and the tree is a parse tree, by the Corollary of Lemma 1. Conversely, if S⇒∗w, then since w has no nonterminal, there is a rightmost derivation with t≥1 steps. By Lemma 3 its last configuration is (S,n), and by Lemma 4 the node (δ(q0,S),n)=(GOTO(q0,S),n) exists with an edge to the root. Line L13 runs after phase n, so it finds them and returns true. ■