Algorithm Search⌘K

Proofs: Incremental parsing

These are the human proofs of Incremental parsing, at full length.

Incremental Earley reparsing returns the Earley chart of the edited string full

This proof shows: correctness.

Notation. G=(V,Σ,P,S)G = (V, \Sigma, P, S) is any context-free grammar, w=a1⋯anw = a_1 \cdots a_n is the old string and E[0],…,E[n]E[0], \ldots, E[n] is its Earley chart. The edit replaces ap+1⋯aqa_{p+1} \cdots a_q by vv, with 0≤p≤q≤n0 \le p \le q \le n and t=∣v∣t = |v|. The new string is w′=a1⋯ap v aq+1⋯an=a1′⋯an′′w' = a_1 \cdots a_p\, v\, a_{q+1} \cdots a_n = a'_1 \cdots a'_{n'}, with n′=n+δn' = n + \delta, δ=t−(q−p)\delta = t - (q - p) and q′=p+tq' = p + t. So ak′=aka'_k = a_k for k≤pk \le p and ak+δ′=aka'_{k + \delta} = a_k for k>qk > q. E′[0],…,E′[n′]E'[0], \ldots, E'[n'] is the true Earley chart of w′w'; we show that REPARSE returns it. The shift of an old position ii is σ(i)=i\sigma(i) = i if i<qi < q and σ(i)=i+δ\sigma(i) = i + \delta if i≥qi \ge q. For an item, σ[A→α∙β,i]=[A→α∙β,σ(i)]\sigma[A \to \alpha \bullet \beta, i] = [A \to \alpha \bullet \beta, \sigma(i)], and σ(X)={σ(I):I∈X}\sigma(X) = \{\sigma(I) : I \in X\} for a set XX. Note that σ(i)≤σ(i2)\sigma(i) \le \sigma(i_2) when i≤pi \le p and q≤i2q \le i_2, and σ(K)=K+δ\sigma(K) = K + \delta for K≥qK \ge q.

Fact 1 (Earley sets as least sets). For a string u=u1⋯umu = u_1 \cdots u_m, write Scan(X,a)\mathrm{Scan}(X, a) for the set of items [A→αa∙β,i][A \to \alpha a \bullet \beta, i] with [A→α∙aβ,i]∈X[A \to \alpha \bullet a \beta, i] \in X. Given the sets Eu[0],…,Eu[j−1]E_u[0], \ldots, E_u[j - 1] and a seed XX, let Clj(X)\mathrm{Cl}_j(X) be the least set Y⊇XY \supseteq X such that (P) if [A→α∙Bβ,i]∈Y[A \to \alpha \bullet B \beta, i] \in Y with B∈VB \in V, then [B→∙η,j]∈Y[B \to \bullet \eta, j] \in Y for every B→η∈PB \to \eta \in P; and (C) if [B→γ∙,i]∈Y[B \to \gamma \bullet, i] \in Y and [A→α∙Bβ,o][A \to \alpha \bullet B \beta, o] is in Eu[i]E_u[i] (for i<ji < j) or in YY (for i=ji = j), then [A→αB∙β,o]∈Y[A \to \alpha B \bullet \beta, o] \in Y. Then Eu[0]=Cl0({[S→∙α,0]:S→α∈P})E_u[0] = \mathrm{Cl}_0(\{[S \to \bullet \alpha, 0] : S \to \alpha \in P\}) and Eu[j]=Clj(Scan(Eu[j−1],uj))E_u[j] = \mathrm{Cl}_j(\mathrm{Scan}(E_u[j - 1], u_j)) for j≥1j \ge 1. This is the set that Earley's algorithm computes for position jj, with the nullable rule making one pass enough (see the Earley parser), and it is the set computed by EARLEY-SET at line L4. In particular Eu[j]E_u[j] depends only on u1⋯uju_1 \cdots u_j, and every origin in Eu[j]E_u[j] is at most jj.

Definition (good positions). An old position ii is good when either (a) i<qi < q and i≤pi \le p, or (b) i≥qi \ge q, E′[i+δ]=σ(E[i])E'[i + \delta] = \sigma(E[i]), and every origin o<io < i of an item of E[i]E[i] is good. This is well founded, since o<io < i. For a good position of kind (a), E′[i]=E[i]E'[i] = E[i] by Fact 1, because ww and w′w' agree on their first p≥ip \ge i symbols. Every origin oo of E[i]E[i] has o≤io \le i, so o<qo < q and σ(o)=o\sigma(o) = o; hence E′[σ(i)]=σ(E[i])E'[\sigma(i)] = \sigma(E[i]) for every good ii, of either kind, and every origin of an item of E[i]E[i] is good or equal to ii.

Lemma 1 (the test). When line L7 sets ANCH[k]\mathrm{ANCH}[k] for k≥q′k \ge q', the set new[k][k] equals E′[k]E'[k], and ANCH[k]\mathrm{ANCH}[k] is true exactly when K=k−δK = k - \delta is good.

Proof. By induction on kk. Line L2 copies E[0],…,E[p]E[0], \ldots, E[p], which equal E′[0],…,E′[p]E'[0], \ldots, E'[p] by Fact 1. Each set computed at line L4 is Clk(Scan(E′[k−1],ak′))\mathrm{Cl}_k(\mathrm{Scan}(E'[k - 1], a'_k)) from correct earlier sets, so it is E′[k]E'[k] by Fact 1. Now k≥q′k \ge q' gives K≥qK \ge q. Line L6 accepts an origin ii of E[K]E[K] when i<qi < q and i≤pi \le p, which is kind (a); when i=Ki = K; or when q≤i<Kq \le i < K and ANCH[i+δ]\mathrm{ANCH}[i + \delta] is true. In the last case q′≤i+δ<kq' \le i + \delta < k, so ANCH[i+δ]\mathrm{ANCH}[i + \delta] was set earlier, and by induction it is true exactly when ii is good. Line L6 rejects every origin ii with p<i<qp < i < q, and such an ii is not good. Line L7 adds the test E′[k]=σ(E[K])E'[k] = \sigma(E[K]). Together, these are the conditions of kind (b) for KK. □\square

Lemma 2 (good positions propagate). If K≥qK \ge q is good and K<nK < n, then K+1K + 1 is good.

Proof. Let j=K+1j = K + 1 and j′=j+δ=σ(j)j' = j + \delta = \sigma(j), and let Γ\Gamma be the set of good positions at most KK, together with jj. Since j>qj > q, aj′′=aja'_{j'} = a_j.

Claim A: every origin of an item of E[j]E[j] is in Γ\Gamma. The items of E[j]E[j] with an origin in Γ\Gamma form a set that contains Scan(E[K],aj)\mathrm{Scan}(E[K], a_j), since the origins of E[K]E[K] are good or equal to KK, and KK is good. It is closed under (P), which adds origin jj. It is closed under (C): for [B→γ∙,i][B \to \gamma \bullet, i] with i∈Γi \in \Gamma, either i=ji = j and the new item takes its origin from an item of the set, or i≤Ki \le K is good and the new item takes its origin from an item of E[i]E[i], which is good or equal to ii. By Fact 1, E[j]E[j] is the least such set, so all its items are in this set.

Claim B: σ(E[j])⊆E′[j′]\sigma(E[j]) \subseteq E'[j']. Let ZZ be the set of items I∈E[j]I \in E[j] with σ(I)∈E′[j′]\sigma(I) \in E'[j']. ZZ contains the seed: an item of Scan(E[K],aj)\mathrm{Scan}(E[K], a_j) comes from J∈E[K]J \in E[K], and σ(J)∈σ(E[K])=E′[K+δ]\sigma(J) \in \sigma(E[K]) = E'[K + \delta] scans aj′′=aja'_{j'} = a_j into E′[j′]E'[j']. ZZ is closed under (P), because the predicted item [B→∙η,j][B \to \bullet \eta, j] maps to [B→∙η,j′][B \to \bullet \eta, j'], which (P) puts in E′[j′]E'[j']. ZZ is closed under (C). Take [B→γ∙,i]∈Z[B \to \gamma \bullet, i] \in Z, so i∈Γi \in \Gamma by Claim A. If i=ji = j and the waiting item JJ is also in ZZ, then (C) in E′[j′]E'[j'] with origin j′j' applies to σ(J)\sigma(J) and gives the image of the new item. If i≤Ki \le K and J∈E[i]J \in E[i], then σ(i)≤σ(K)<j′\sigma(i) \le \sigma(K) < j', and (C) in E′[j′]E'[j'] reads E′[σ(i)]=σ(E[i])E'[\sigma(i)] = \sigma(E[i]), which contains σ(J)\sigma(J), so it gives the image of the new item. By the leastness in Fact 1, Z=E[j]Z = E[j].

Claim C: E′[j′]⊆σ(E[j])E'[j'] \subseteq \sigma(E[j]). Let W=σ(E[j])W = \sigma(E[j]). WW contains the new seed Scan(E′[K+δ],aj)=Scan(σ(E[K]),aj)=σ(Scan(E[K],aj))\mathrm{Scan}(E'[K + \delta], a_j) = \mathrm{Scan}(\sigma(E[K]), a_j) = \sigma(\mathrm{Scan}(E[K], a_j)). WW is closed under (P) at j′j', since a prediction from σ(J)\sigma(J) is the image of the prediction from JJ. WW is closed under (C) at j′j'. Take σ[B→γ∙,i]∈W\sigma[B \to \gamma \bullet, i] \in W with [B→γ∙,i]∈E[j][B \to \gamma \bullet, i] \in E[j], so i∈Γi \in \Gamma. If σ(i)=j′\sigma(i) = j', then i=ji = j, because σ(i)≤σ(K)<j′\sigma(i) \le \sigma(K) < j' for the other elements of Γ\Gamma; the waiting items of WW are images of waiting items of E[j]E[j], and (C) in E[j]E[j] gives the preimage of the new item. If σ(i)<j′\sigma(i) < j', then i≤Ki \le K is good, and (C) reads E′[σ(i)]=σ(E[i])E'[\sigma(i)] = \sigma(E[i]); its waiting items are images of waiting items of E[i]E[i], and (C) in E[j]E[j] again gives the preimage. By the leastness in Fact 1, E′[j′]⊆WE'[j'] \subseteq W.

So E′[j+δ]=σ(E[j])E'[j + \delta] = \sigma(E[j]), j≥qj \ge q, and every origin o<jo < j of E[j]E[j] is in Γ∖{j}\Gamma \setminus \{j\}, so it is good. Hence jj is good. □\square

Theorem. REPARSE(G,w,E,p,q,v)(G, w, E, p, q, v) terminates and returns w′w' and the Earley chart E′[0],…,E′[n′]E'[0], \ldots, E'[n'] of w′w'. Hence line L12 returns true if and only if S⇒∗w′S \Rightarrow^* w'.

Proof. The loop of line L3 runs at most n′−p+1n' - p + 1 times, and each step ends, so the call terminates. Line L1 builds w′w'. By Lemma 1, the sets that lines L2 and L4 build are E′[0],…,E′[k]E'[0], \ldots, E'[k] when the loop reaches kk. If line L8 never finds an anchored set, the loop computes every set at line L4, and line L11 returns E′E'. Otherwise line L8 stops at some k≥q′k \ge q' with ANCH[k]\mathrm{ANCH}[k] true, so K=k−δK = k - \delta is good by Lemma 1. By Lemma 2 and induction, every position jj with K≤j≤nK \le j \le n is good, so E′[j+δ]=σ(E[j])E'[j + \delta] = \sigma(E[j]). These are the sets that line L9 adds for the positions k+1,…,n′k + 1, \ldots, n', and line L10 returns E′E'. The last claim follows from the correctness of Earley's algorithm: S⇒∗w′S \Rightarrow^* w' if and only if some [S→α∙,0][S \to \alpha \bullet, 0] is in E′[n′]E'[n']. ■\blacksquare

Back to Incremental parsing