Algorithm Search⌘K

Proofs: Left-corner parsing

These are the human proofs of Left-corner parsing, at full length.

The left-corner chart recognizer accepts exactly the words of the grammar full

This proof shows: correctness.

Notation. G=(V,Σ,P,S)G = (V, \Sigma, P, S) is any context-free grammar and w=a1⋯anw = a_1 \cdots a_n. For 0≤i≤k≤n0 \le i \le k \le n, wi,k=ai+1⋯akw_{i,k} = a_{i+1} \cdots a_k. L(C)L(C) is the set of B∈VB \in V such that B=CB = C or there are productions C→B1γ1,B1→B2γ2,…,Bm−1→BγmC \to B_1 \gamma_1, B_1 \to B_2 \gamma_2, \ldots, B_{m-1} \to B \gamma_m in PP, m≥1m \ge 1, with Bm=BB_m = B. Note that if B∈L(C)B \in L(C) and B→Dγ∈PB \to D \gamma \in P with D∈VD \in V, then D∈L(C)D \in L(C). A pair (C,i)(C, i) is a goal once START(C,i)(C, i) has been called. "Add" puts an item in a set only if it is not there.

Lemma 1 (soundness). Every item [C,i,B→α∙β]∈E[k][C, i, B \to \alpha \bullet \beta] \in E[k] satisfies: (C,i)(C, i) is a goal, i≤ki \le k, B∈L(C)B \in L(C), α⇒∗wi,k\alpha \Rightarrow^* w_{i,k}, and α≠ε\alpha \ne \varepsilon unless β=ε\beta = \varepsilon. Every (C,i,k)∈DONE(C, i, k) \in \mathrm{DONE} satisfies: (C,i)(C, i) is a goal and C⇒∗wi,kC \Rightarrow^* w_{i,k}.

Proof. By induction on the order of the additions. Line L14 adds [C,k,D→ak+1∙γ][C, k, D \to a_{k+1} \bullet \gamma] to E[k+1]E[k + 1] with D∈L(C)D \in L(C), and ak+1⇒∗wk,k+1a_{k+1} \Rightarrow^* w_{k,k+1}. Line L15 adds [C,k,D→∙][C, k, D \to \bullet] with D∈L(C)D \in L(C) and β=ε\beta = \varepsilon. Line L6 adds (C,i,k)(C, i, k) for a complete item [C,i,C→α∙][C, i, C \to \alpha \bullet], and then C⇒α⇒∗wi,kC \Rightarrow \alpha \Rightarrow^* w_{i,k}. It also moves an item [C′,i′,D→γ∙Cδ]∈E[i][C', i', D \to \gamma \bullet C \delta] \in E[i] that waits on (C,i)(C, i) to E[k]E[k]; by induction γ⇒∗wi′,i\gamma \Rightarrow^* w_{i',i}, so γC⇒∗wi′,k\gamma C \Rightarrow^* w_{i',k}. Line L7 adds [C,i,D→B∙γ][C, i, D \to B \bullet \gamma] for a complete item [C,i,B→α∙]∈E[k][C, i, B \to \alpha \bullet] \in E[k], with D∈L(C)D \in L(C); by induction B⇒α⇒∗wi,kB \Rightarrow \alpha \Rightarrow^* w_{i,k}. Line L11 is the same as the move of line L6, for an entry (Y,k,k′)∈DONE(Y, k, k') \in \mathrm{DONE}, with Y⇒∗wk,k′Y \Rightarrow^* w_{k,k'} by induction. Line L12 extends α\alpha by ak+1a_{k+1}. In each case the new item keeps its goal, which is a goal by induction, and has a nonempty α\alpha unless it comes from line L15. □\square

Lemma 2 (processing order). Every entry (Y,i,k′)(Y, i, k') of DONE is added while E[k′]E[k'] is processed. When line L11 runs for an item of E[k]E[k], every entry (Y,k,k′)(Y, k, k') in DONE has k′=kk' = k. Every item ever added to E[k]E[k] is processed by the loop of line L4 for this kk.

Proof. Line L6 adds (C,i,k)(C, i, k) while it processes a complete item of E[k]E[k]. So when E[k]E[k] is processed, the entries of DONE have ends at most kk, and an entry (Y,k,k′)(Y, k, k') has k′≥kk' \ge k by Lemma 1, so k′=kk' = k. Hence items are added to E[k]E[k] only by START(⋅,k)(\cdot, k) (line L15), by START(⋅,k−1)(\cdot, k - 1) (line L14), by lines L6, L7 and L11 while E[k]E[k] is processed, or by line L12 while E[k−1]E[k - 1] is processed. START(Y,k)(Y, k) is called at line L2 with k=0k = 0 before the loop, or at line L9 while E[k]E[k] is processed. So no item is added to E[k]E[k] after its loop ends, and the loop visits every item in the order of insertion, including the items added during the loop. □\square

Lemma 3 (closure). When the loop of line L3 ends, the chart is closed under the rules of lines L6, L7, L9 to L12, L14 and L15: for every item, the items and goals that the line for it would add are present, and for every goal (C,i)(C, i), the items of START(C,i)(C, i) are present.

Proof. Every item is processed (Lemma 2), and START runs for every goal. Processing an item adds what lines L7, L9, L10, L11 and L12 require for it. For a complete item [C,i,B→α∙]∈E[k][C, i, B \to \alpha \bullet] \in E[k], the first such item for the key (C,i,B,k)(C, i, B, k) is processed fully, and the effects of lines L6 and L7 depend only on this key, so they hold for the other items with the same key. It remains to show the rule of line L6 for an item JJ that waits on (C,i)(C, i) and an entry (C,i,k)∈DONE(C, i, k) \in \mathrm{DONE}: the moved item must be in E[k]E[k]. If JJ was recorded as waiting (line L10) before (C,i,k)(C, i, k) was added, then line L6 moved it when it added the entry. Otherwise the entry was in DONE when line L11 processed JJ, and line L11 moved it. □\square

Lemma 4 (completeness). Let (C,i)(C, i) be a goal, and suppose C⇒∗wi,kC \Rightarrow^* w_{i,k}. Then (C,i,k)∈DONE(C, i, k) \in \mathrm{DONE} when the loop ends.

Proof. By induction on the height of a parse tree TT of CC with yield wi,kw_{i,k}. Let C=B0,B1,…,BmC = B_0, B_1, \ldots, B_m be the nodes on the left edge of TT: Br+1B_{r+1} is the first child of BrB_r, and BmB_m is the last internal node on this edge. Each BrB_r is in L(C)L(C), since each production Br→Br+1⋯B_r \to B_{r+1} \cdots starts with Br+1B_{r+1}. Let Br→Xr,1⋯Xr,qrB_r \to X_{r,1} \cdots X_{r,q_r} be the production at BrB_r, where Xr,1=Br+1X_{r,1} = B_{r+1} for r<mr < m, and let i=lr,0≤lr,1≤⋯≤lr,qri = l_{r,0} \le l_{r,1} \le \cdots \le l_{r,q_r} be the boundaries of the yields of its children. All nodes on the left edge start at ii, and BrB_r ends at lr,qrl_{r, q_r}.

We show by downward induction on rr, from r=mr = m to r=0r = 0, that the complete item [C,i,Br→Xr,1⋯Xr,qr∙][C, i, B_r \to X_{r,1} \cdots X_{r,q_r} \bullet] is in E[lr,qr]E[l_{r,q_r}].

The first item at BrB_r. If r=mr = m and qm=0q_m = 0, START(C,i)(C, i) adds [C,i,Bm→∙][C, i, B_m \to \bullet] to E[i]E[i] (line L15). If r=mr = m and qm>0q_m > 0, then Xm,1X_{m,1} is a terminal, equal to ai+1a_{i+1}, so i<ni < n and START(C,i)(C, i) adds [C,i,Bm→ai+1∙Xm,2⋯ ][C, i, B_m \to a_{i+1} \bullet X_{m,2} \cdots] to E[i+1]E[i + 1] (line L14). If r<mr < m, the complete item for Br+1B_{r+1} is in E[lr,1]E[l_{r,1}] by the inner induction, and line L7 adds [C,i,Br→Br+1∙Xr,2⋯ ][C, i, B_r \to B_{r+1} \bullet X_{r,2} \cdots] to E[lr,1]E[l_{r,1}], because Br∈L(C)B_r \in L(C) (Lemma 3).

The other children. Suppose [C,i,Br→Xr,1⋯Xr,t−1∙Xr,t⋯ ]∈E[lr,t−1][C, i, B_r \to X_{r,1} \cdots X_{r,t-1} \bullet X_{r,t} \cdots] \in E[l_{r,t-1}] for some 2≤t≤qr2 \le t \le q_r. If Xr,tX_{r,t} is a terminal, it is alr,t−1+1a_{l_{r,t-1}+1}, and line L12 moves the dot into E[lr,t]E[l_{r,t}]. If Xr,t=YX_{r,t} = Y is a nonterminal, then by Lemma 3 the pair (Y,lr,t−1)(Y, l_{r,t-1}) is a goal (line L9) and the item waits on it (line L10). The subtree of YY is a proper subtree of TT, of smaller height, with yield wlr,t−1,lr,tw_{l_{r,t-1}, l_{r,t}}, so by the outer induction (Y,lr,t−1,lr,t)∈DONE(Y, l_{r,t-1}, l_{r,t}) \in \mathrm{DONE}. By Lemma 3 (the rule of line L6), the item with the dot after YY is in E[lr,t]E[l_{r,t}]. After t=qrt = q_r, the complete item for BrB_r is in E[lr,qr]E[l_{r,q_r}].

For r=0r = 0, the complete item [C,i,C→⋯∙]∈E[k][C, i, C \to \cdots \bullet] \in E[k] has B=CB = C, and the rule of line L6 puts (C,i,k)(C, i, k) in DONE. □\square

Termination. There are at most ∣V∣(n+1)|V| (n + 1) goals, and for each set E[k]E[k] at most ∣V∣(k+1)|V| (k + 1) goals times ∣G∣|G| dotted productions, so finitely many items. Each item is processed once, and each step of the processing does a finite amount of work, so the loop ends.

Theorem. LEFT-CORNER(G,w)(G, w) terminates and returns true if and only if S⇒∗wS \Rightarrow^* w.

Proof. Termination is shown above. Line L2 makes (S,0)(S, 0) a goal. If line L13 returns true, then (S,0,n)∈DONE(S, 0, n) \in \mathrm{DONE} and S⇒∗w0,n=wS \Rightarrow^* w_{0,n} = w by Lemma 1. If S⇒∗wS \Rightarrow^* w, then (S,0,n)∈DONE(S, 0, n) \in \mathrm{DONE} by Lemma 4, and line L13 returns true. ■\blacksquare

Back to Left-corner parsing