Algorithm Search⌘K

Proofs: Earley parser

These are the human proofs of Earley parser, at full length.

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)G = (V, \Sigma, P, S) with no restriction on its productions, and the input is w=a1⋯anw = a_1 \cdots a_n. For 0≤i≤j≤n0 \le i \le j \le n write wi,j=ai+1⋯ajw_{i,j} = a_{i+1} \cdots a_j, so wi,i=εw_{i,i} = \varepsilon.

Valid items. An item [A→α∙β,i][A \to \alpha \bullet \beta, i] is valid at jj when

  • (V1) α⇒∗wi,j\alpha \Rightarrow^* w_{i,j}, and
  • (V2) S⇒∗a1⋯ai A γS \Rightarrow^* a_1 \cdots a_i\, A\, \gamma for some γ∈(V∪Σ)∗\gamma \in (V \cup \Sigma)^*.

We show that the final sets satisfy E[j]={items valid at j}E[j] = \{\text{items valid at } j\} for every jj. Then line L13 is correct: some [S→α∙,0]∈E[n][S \to \alpha \bullet, 0] \in E[n] if and only if some S→αS \to \alpha has α⇒∗w\alpha \Rightarrow^* w (condition V2 holds with zero steps and γ=ε\gamma = \varepsilon), that is, if and only if S⇒∗wS \Rightarrow^* w.

Lemma 0 (line L1). The least fixed point of "AA is nullable if some A→X1⋯XkA \to X_1 \cdots X_k has all XiX_i nullable" is {A:A⇒∗ε}\{A : A \Rightarrow^* \varepsilon\}.

Proof. Each symbol added has a derivation of ε\varepsilon, by induction on the order of addition: if every XiX_i derives ε\varepsilon, so does A⇒X1⋯XkA \Rightarrow X_1 \cdots X_k. Conversely, if A⇒mεA \Rightarrow^m \varepsilon, the first step uses some A→X1⋯XkA \to X_1 \cdots X_k, every XiX_i is a nonterminal, and by the splitting lemma each Xi⇒miεX_i \Rightarrow^{m_i} \varepsilon with mi<mm_i < m. By induction on mm every XiX_i is in the fixed point, and then so is AA. □\square

Lemma 1 (soundness). Every item that the algorithm inserts into E[j]E[j] is valid at jj.

Proof. By induction on the order of insertion; each line uses only items inserted before.

  • Line L3 inserts [S→∙α,0][S \to \bullet \alpha, 0] into E[0]E[0]: (V1) is ε⇒∗ε\varepsilon \Rightarrow^* \varepsilon, and (V2) holds with zero steps.
  • Line L9, from [A→α∙Xβ′,i]∈E[j][A \to \alpha \bullet X \beta', i] \in E[j], inserts [X→∙η,j][X \to \bullet \eta, j]. (V1) is trivial. For (V2), S⇒∗a1⋯aiAγ⇒a1⋯ai αXβ′γ⇒∗a1⋯aj Xβ′γS \Rightarrow^* a_1 \cdots a_i A \gamma \Rightarrow a_1 \cdots a_i\, \alpha X \beta' \gamma \Rightarrow^* a_1 \cdots a_j\, X \beta' \gamma, using α⇒∗wi,j\alpha \Rightarrow^* w_{i,j}.
  • Line L10 inserts [A→αX∙β′,i][A \to \alpha X \bullet \beta', i] with XX nullable. (V1) holds because αX⇒∗wi,j ε\alpha X \Rightarrow^* w_{i,j}\, \varepsilon by Lemma 0, and (V2) is the same as for the source item.
  • Line L12 inserts [A→αaj+1∙β′,i][A \to \alpha a_{j+1} \bullet \beta', i] into E[j+1]E[j+1]: αaj+1⇒∗wi,j aj+1=wi,j+1\alpha a_{j+1} \Rightarrow^* w_{i,j}\, a_{j+1} = w_{i,j+1}, and (V2) is unchanged.
  • Line L7 uses [A→η∙,i]∈E[j][A \to \eta \bullet, i] \in E[j] and [B→γ∙Aδ,k]∈E[i][B \to \gamma \bullet A \delta, k] \in E[i], and inserts [B→γA∙δ,k][B \to \gamma A \bullet \delta, k] into E[j]E[j]. By (V1) of the two items, γA⇒∗wk,i A⇒wk,i η⇒∗wk,i wi,j=wk,j\gamma A \Rightarrow^* w_{k,i}\, A \Rightarrow w_{k,i}\, \eta \Rightarrow^* w_{k,i}\, w_{i,j} = w_{k,j}. (V2) is that of the second item. □\square

The deduction rules. Let CC be the least set of pairs (item, position) that contains ([S→∙α,0],0)([S \to \bullet \alpha, 0], 0) for each S→αS \to \alpha and is closed under:

  • predict: from ([A→α∙Xβ,i],j)([A \to \alpha \bullet X \beta, i], j) with X∈VX \in V, derive ([X→∙η,j],j)([X \to \bullet \eta, j], j) for each X→ηX \to \eta;
  • scan: from ([A→α∙aj+1β,i],j)([A \to \alpha \bullet a_{j+1} \beta, i], j) with j<nj < n, derive ([A→αaj+1∙β,i],j+1)([A \to \alpha a_{j+1} \bullet \beta, i], j + 1);
  • complete: from ([A→η∙,i],j)([A \to \eta \bullet, i], j) and ([B→γ∙Aδ,k],i)([B \to \gamma \bullet A \delta, k], i), derive ([B→γA∙δ,k],j)([B \to \gamma A \bullet \delta, k], j).

Lemma 2. Every item valid at jj is in CC at jj.

Proof. Let I=[A→α∙β,i]I = [A \to \alpha \bullet \beta, i] be valid at jj, with a derivation S⇒ma1⋯aiAγS \Rightarrow^{m} a_1 \cdots a_i A \gamma for (V2) and α⇒swi,j\alpha \Rightarrow^{s} w_{i,j} for (V1). Call (m+1+s, s, ∣α∣)(m + 1 + s,\ s,\ |\alpha|) the measure of this pair of witnesses. We prove I∈CI \in C by induction on the measure, ordered lexicographically.

Case α=ε\alpha = \varepsilon. Then i=ji = j. If m=0m = 0, then A=SA = S, i=0i = 0 and II is an initial item of CC. If m≥1m \ge 1, look at the partial derivation tree of S⇒ma1⋯aiAγS \Rightarrow^m a_1 \cdots a_i A \gamma: its frontier is a1⋯aiAγa_1 \cdots a_i A \gamma, the leaf AA is not the root, and it has a parent BB expanded by some B→μAνB \to \mu A \nu. All leaves left of AA are terminals, so the subtree of BB covers ak+1⋯ai Aa_{k+1} \cdots a_i\, A followed by the frontier of ν\nu, for some k≤ik \le i, and μ⇒s′wk,i\mu \Rightarrow^{s'} w_{k,i}, where s′s' is the number of expanded nodes below the children of BB in μ\mu. Removing every node below BB gives a partial tree with frontier a1⋯akBγ′a_1 \cdots a_k B \gamma' and m′m' expanded nodes, where m′+1+s′≤mm' + 1 + s' \le m, because BB and the nodes below μ\mu were removed. So the item I′=[B→μ∙Aν,k]I' = [B \to \mu \bullet A \nu, k] is valid at ii with measure (m′+1+s′,s′,∣μ∣)(m' + 1 + s', s', |\mu|), whose first component is at most m<m+1m < m + 1. By induction I′∈CI' \in C at ii, and predict gives I∈CI \in C at i=ji = j.

Case α=α′a\alpha = \alpha' a with a∈Σa \in \Sigma. Then j≥1j \ge 1, a=aja = a_j, and α′⇒swi,j−1\alpha' \Rightarrow^{s} w_{i,j-1} by the splitting lemma, with the same ss. The item [A→α′∙ajβ,i][A \to \alpha' \bullet a_j \beta, i] is valid at j−1j - 1 with measure (m+1+s,s,∣α∣−1)(m + 1 + s, s, |\alpha| - 1), which is smaller. By induction it is in CC, and scan gives II.

Case α=α′X\alpha = \alpha' X with X∈VX \in V. By the splitting lemma, α′⇒s′wi,k\alpha' \Rightarrow^{s'} w_{i,k} and X⇒sXwk,jX \Rightarrow^{s_X} w_{k,j} for some kk with s′+sX=ss' + s_X = s and sX≥1s_X \ge 1, since XX is not a terminal. Let the first step of the derivation of XX be X→ηX \to \eta, so η⇒sX−1wk,j\eta \Rightarrow^{s_X - 1} w_{k,j}.

  • The item I1=[A→α′∙Xβ,i]I_1 = [A \to \alpha' \bullet X \beta, i] is valid at kk with measure (m+1+s′,s′,∣α∣−1)(m + 1 + s', s', |\alpha| - 1). Its first component is smaller than m+1+sm + 1 + s because sX≥1s_X \ge 1.
  • The item I2=[X→η∙,k]I_2 = [X \to \eta \bullet, k] is valid at jj: (V1) holds by η⇒sX−1wk,j\eta \Rightarrow^{s_X - 1} w_{k,j}, and (V2) by S⇒ma1⋯aiAγ⇒a1⋯ai α′Xβγ⇒s′a1⋯akXβγS \Rightarrow^m a_1 \cdots a_i A \gamma \Rightarrow a_1 \cdots a_i\, \alpha' X \beta \gamma \Rightarrow^{s'} a_1 \cdots a_k X \beta \gamma, of m+1+s′m + 1 + s' steps. Its measure is (m+1+s′+1+sX−1, sX−1, ∣η∣)=(m+1+s, sX−1, ∣η∣)(m + 1 + s' + 1 + s_X - 1,\ s_X - 1,\ |\eta|) = (m + 1 + s,\ s_X - 1,\ |\eta|): the first component is equal, and the second is sX−1<ss_X - 1 < s.

By induction I1∈CI_1 \in C at kk and I2∈CI_2 \in C at jj, and complete gives II. □\square

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≤ji \le j), every member of CC is valid. So CC is exactly the set of valid items.

Lemma 3. When the loop of line L4 ends, the sets E[0],…,E[n]E[0], \ldots, E[n] are closed under the three rules and contain the initial items.

Proof. Line L3 inserts the initial items. During iteration jj of line L4, items are added only to E[j]E[j] (lines L7, L9, L10) and to E[j+1]E[j + 1] (line L12). So E[j]E[j] does not change after iteration jj, and every item of E[j]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][A \to \alpha \bullet X \beta, i] \in E[j] is visited, either XX is predicted now or was predicted before in E[j]E[j]; in both cases line L9 has inserted [X→∙η,j][X \to \bullet \eta, j] for every X→ηX \to \eta.
  • Scan. When [A→α∙aj+1β,i]∈E[j][A \to \alpha \bullet a_{j+1} \beta, i] \in E[j] is visited, line L12 inserts the scanned item into E[j+1]E[j + 1].
  • Complete, with [A→η∙,i]∈E[j][A \to \eta \bullet, i] \in E[j] and [B→γ∙Aδ,k]∈E[i][B \to \gamma \bullet A \delta, k] \in E[i]. If i<ji < j: when the first item of E[j]E[j] of the form [A→η′∙,i][A \to \eta' \bullet, i] was visited, line L7 read all items of E[i]E[i] that wait for AA, and E[i]E[i] was already final, so [B→γA∙δ,k][B \to \gamma A \bullet \delta, k] was inserted. Skipping the later items with the same pair (A,i)(A, i) loses nothing, because line L7 inserts the same items for each of them. If i=ji = j: the item [A→η∙,j][A \to \eta \bullet, j] is valid by Lemma 1, so η⇒∗wj,j=ε\eta \Rightarrow^* w_{j,j} = \varepsilon and AA is nullable by Lemma 0. The item [B→γ∙Aδ,k]∈E[j][B \to \gamma \bullet A \delta, k] \in E[j] is visited at some point, and then line L10 inserts [B→γA∙δ,k][B \to \gamma A \bullet \delta, k]. □\square

Theorem. For every jj, E[j]E[j] is the set of items valid at jj, and EARLEY returns true if and only if w∈L(G)w \in 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 CC, which by Lemma 2 contains all valid items. Lemma 1 says every inserted item is valid. So E[j]E[j] is exactly the set of valid items at jj, and the remark after the definition of validity gives the answer of line L13. □\square

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][S \to \alpha \bullet, 0] \in E[n] ends. Walking from a completed item [A→X1⋯Xk∙,i]∈E[j][A \to X_1 \cdots X_k \bullet, i] \in E[j] back to its dot-zero item gives, in reverse order, one record for each XtX_t: the terminal apa_p for a scan from E[p−1]E[p-1] to E[p]E[p], a completed item [Xt→η∙,p]∈E[q][X_t \to \eta \bullet, p] \in E[q] for a completion, or a nullable XtX_t with an empty span. The spans of the records are consecutive and cover wi,jw_{i,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 ε\varepsilon with finitely many nodes. So the result is a finite tree whose root is AA, whose internal nodes are productions, and whose leaves spell wi,jw_{i,j}. For the root item this is a parse tree of ww. ■\blacksquare

Back to Earley parser