Earley's algorithm computes exactly the valid items full
This proof shows: correctness.
We use the pseudocode of the page and its notation. The grammar is G=(V,Σ,P,S) with no restriction on its productions, and the input is w=a1⋯an. For 0≤i≤j≤n write wi,j=ai+1⋯aj, so wi,i=ε.
Valid items. An item [A→α∙β,i] is valid at j when
- (V1) α⇒∗wi,j, and
- (V2) S⇒∗a1⋯aiAγ for some γ∈(V∪Σ)∗.
We show that the final sets satisfy E[j]={items valid at j} for every j. Then line L13 is correct: some [S→α∙,0]∈E[n] if and only if some S→α has α⇒∗w (condition V2 holds with zero steps and γ=ε), that is, if and only if S⇒∗w.
Lemma 0 (line L1). The least fixed point of "A is nullable if some A→X1⋯Xk has all Xi nullable" is {A:A⇒∗ε}.
Proof. Each symbol added has a derivation of ε, by induction on the order of addition: if every Xi derives ε, so does A⇒X1⋯Xk. Conversely, if A⇒mε, the first step uses some A→X1⋯Xk, every Xi is a nonterminal, and by the splitting lemma each Xi⇒miε with mi<m. By induction on m every Xi is in the fixed point, and then so is A. □
Lemma 1 (soundness). Every item that the algorithm inserts into E[j] is valid at j.
Proof. By induction on the order of insertion; each line uses only items inserted before.
- Line L3 inserts [S→∙α,0] into E[0]: (V1) is ε⇒∗ε, and (V2) holds with zero steps.
- Line L9, from [A→α∙Xβ′,i]∈E[j], inserts [X→∙η,j]. (V1) is trivial. For (V2), S⇒∗a1⋯aiAγ⇒a1⋯aiαXβ′γ⇒∗a1⋯ajXβ′γ, using α⇒∗wi,j.
- Line L10 inserts [A→αX∙β′,i] with X nullable. (V1) holds because αX⇒∗wi,jε by Lemma 0, and (V2) is the same as for the source item.
- Line L12 inserts [A→αaj+1∙β′,i] into E[j+1]: αaj+1⇒∗wi,jaj+1=wi,j+1, and (V2) is unchanged.
- Line L7 uses [A→η∙,i]∈E[j] and [B→γ∙Aδ,k]∈E[i], and inserts [B→γA∙δ,k] into E[j]. By (V1) of the two items, γA⇒∗wk,iA⇒wk,iη⇒∗wk,iwi,j=wk,j. (V2) is that of the second item. □
The deduction rules. Let C be the least set of pairs (item, position) that contains ([S→∙α,0],0) for each S→α and is closed under:
- predict: from ([A→α∙Xβ,i],j) with X∈V, derive ([X→∙η,j],j) for each X→η;
- scan: from ([A→α∙aj+1β,i],j) with j<n, derive ([A→αaj+1∙β,i],j+1);
- complete: from ([A→η∙,i],j) and ([B→γ∙Aδ,k],i), derive ([B→γA∙δ,k],j).
Lemma 2. Every item valid at j is in C at j.
Proof. Let I=[A→α∙β,i] be valid at j, with a derivation S⇒ma1⋯aiAγ for (V2) and α⇒swi,j for (V1). Call (m+1+s, s, ∣α∣) the measure of this pair of witnesses. We prove I∈C by induction on the measure, ordered lexicographically.
Case α=ε. Then i=j. If m=0, then A=S, i=0 and I is an initial item of C. If m≥1, look at the partial derivation tree of S⇒ma1⋯aiAγ: its frontier is a1⋯aiAγ, the leaf A is not the root, and it has a parent B expanded by some B→μAν. All leaves left of A are terminals, so the subtree of B covers ak+1⋯aiA followed by the frontier of ν, for some k≤i, and μ⇒s′wk,i, where s′ is the number of expanded nodes below the children of B in μ. Removing every node below B gives a partial tree with frontier a1⋯akBγ′ and m′ expanded nodes, where m′+1+s′≤m, because B and the nodes below μ were removed. So the item I′=[B→μ∙Aν,k] is valid at i with measure (m′+1+s′,s′,∣μ∣), whose first component is at most m<m+1. By induction I′∈C at i, and predict gives I∈C at i=j.
Case α=α′a with a∈Σ. Then j≥1, a=aj, and α′⇒swi,j−1 by the splitting lemma, with the same s. The item [A→α′∙ajβ,i] is valid at j−1 with measure (m+1+s,s,∣α∣−1), which is smaller. By induction it is in C, and scan gives I.
Case α=α′X with X∈V. By the splitting lemma, α′⇒s′wi,k and X⇒sXwk,j for some k with s′+sX=s and sX≥1, since X is not a terminal. Let the first step of the derivation of X be X→η, so η⇒sX−1wk,j.
- The item I1=[A→α′∙Xβ,i] is valid at k with measure (m+1+s′,s′,∣α∣−1). Its first component is smaller than m+1+s because sX≥1.
- The item I2=[X→η∙,k] is valid at j: (V1) holds by η⇒sX−1wk,j, and (V2) by S⇒ma1⋯aiAγ⇒a1⋯aiα′Xβγ⇒s′a1⋯akXβγ, of m+1+s′ steps. Its measure is (m+1+s′+1+sX−1, sX−1, ∣η∣)=(m+1+s, sX−1, ∣η∣): the first component is equal, and the second is sX−1<s.
By induction I1∈C at k and I2∈C at j, and complete gives I. □
With Lemma 1 applied to the rules (each rule is one of the cases of Lemma 1, where complete is the case of line L7 for any i≤j), every member of C is valid. So C is exactly the set of valid items.
Lemma 3. When the loop of line L4 ends, the sets E[0],…,E[n] are closed under the three rules and contain the initial items.
Proof. Line L3 inserts the initial items. During iteration j of line L4, items are added only to E[j] (lines L7, L9, L10) and to E[j+1] (line L12). So E[j] does not change after iteration j, and every item of E[j] is visited by the loop of line L5, which runs until no unvisited item is left.
- Predict. When [A→α∙Xβ,i]∈E[j] is visited, either X is predicted now or was predicted before in E[j]; in both cases line L9 has inserted [X→∙η,j] for every X→η.
- Scan. When [A→α∙aj+1β,i]∈E[j] is visited, line L12 inserts the scanned item into E[j+1].
- Complete, with [A→η∙,i]∈E[j] and [B→γ∙Aδ,k]∈E[i]. If i<j: when the first item of E[j] of the form [A→η′∙,i] was visited, line L7 read all items of E[i] that wait for A, and E[i] was already final, so [B→γA∙δ,k] was inserted. Skipping the later items with the same pair (A,i) loses nothing, because line L7 inserts the same items for each of them. If i=j: the item [A→η∙,j] is valid by Lemma 1, so η⇒∗wj,j=ε and A is nullable by Lemma 0. The item [B→γ∙Aδ,k]∈E[j] is visited at some point, and then line L10 inserts [B→γA∙δ,k]. □
Theorem. For every j, E[j] is the set of items valid at j, and EARLEY returns true if and only if w∈L(G).
Proof. Lemma 3 says the sets contain the initial items and are closed under the rules, so they contain the least such set C, which by Lemma 2 contains all valid items. Lemma 1 says every inserted item is valid. So E[j] is exactly the set of valid items at j, and the remark after the definition of validity gives the answer of line L13. □
The parse tree. Every item with the dot after position 0 records the reason for its first insertion: the item it came from, plus a scanned terminal, a completed item, or a nullable nonterminal. These reasons point to items inserted strictly earlier, so following them from [S→α∙,0]∈E[n] ends. Walking from a completed item [A→X1⋯Xk∙,i]∈E[j] back to its dot-zero item gives, in reverse order, one record for each Xt: the terminal ap for a scan from E[p−1] to E[p], a completed item [Xt→η∙,p]∈E[q] for a completion, or a nullable Xt with an empty span. The spans of the records are consecutive and cover wi,j, by the positions in the pointers. Each completed child is again a valid completed item, and for a nullable child the fixed tree built in the order of Lemma 0 derives ε with finitely many nodes. So the result is a finite tree whose root is A, whose internal nodes are productions, and whose leaves spell wi,j. For the root item this is a parse tree of w. ■