Incremental Earley reparsing returns the Earley chart of the edited string full
This proof shows: correctness.
Notation. G=(V,Σ,P,S) is any context-free grammar, w=a1⋯an is the old string and E[0],…,E[n] is its Earley chart. The edit replaces ap+1⋯aq by v, with 0≤p≤q≤n and t=∣v∣. The new string is w′=a1⋯apvaq+1⋯an=a1′⋯an′′, with n′=n+δ, δ=t−(q−p) and q′=p+t. So ak′=ak for k≤p and ak+δ′=ak for k>q. E′[0],…,E′[n′] is the true Earley chart of w′; we show that REPARSE returns it. The shift of an old position i is σ(i)=i if i<q and σ(i)=i+δ if i≥q. For an item, σ[A→α∙β,i]=[A→α∙β,σ(i)], and σ(X)={σ(I):I∈X} for a set X. Note that σ(i)≤σ(i2) when i≤p and q≤i2, and σ(K)=K+δ for K≥q.
Fact 1 (Earley sets as least sets). For a string u=u1⋯um, write Scan(X,a) for the set of items [A→αa∙β,i] with [A→α∙aβ,i]∈X. Given the sets Eu[0],…,Eu[j−1] and a seed X, let Clj(X) be the least set Y⊇X such that (P) if [A→α∙Bβ,i]∈Y with B∈V, then [B→∙η,j]∈Y for every B→η∈P; and (C) if [B→γ∙,i]∈Y and [A→α∙Bβ,o] is in Eu[i] (for i<j) or in Y (for i=j), then [A→αB∙β,o]∈Y. Then Eu[0]=Cl0({[S→∙α,0]:S→α∈P}) and Eu[j]=Clj(Scan(Eu[j−1],uj)) for j≥1. This is the set that Earley's algorithm computes for position j, 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] depends only on u1⋯uj, and every origin in Eu[j] is at most j.
Definition (good positions). An old position i is good when either (a) i<q and i≤p, or (b) i≥q, E′[i+δ]=σ(E[i]), and every origin o<i of an item of E[i] is good. This is well founded, since o<i. For a good position of kind (a), E′[i]=E[i] by Fact 1, because w and w′ agree on their first p≥i symbols. Every origin o of E[i] has o≤i, so o<q and σ(o)=o; hence E′[σ(i)]=σ(E[i]) for every good i, of either kind, and every origin of an item of E[i] is good or equal to i.
Lemma 1 (the test). When line L7 sets ANCH[k] for k≥q′, the set new[k] equals E′[k], and ANCH[k] is true exactly when K=k−δ is good.
Proof. By induction on k. Line L2 copies E[0],…,E[p], which equal E′[0],…,E′[p] by Fact 1. Each set computed at line L4 is Clk(Scan(E′[k−1],ak′)) from correct earlier sets, so it is E′[k] by Fact 1. Now k≥q′ gives K≥q. Line L6 accepts an origin i of E[K] when i<q and i≤p, which is kind (a); when i=K; or when q≤i<K and ANCH[i+δ] is true. In the last case q′≤i+δ<k, so ANCH[i+δ] was set earlier, and by induction it is true exactly when i is good. Line L6 rejects every origin i with p<i<q, and such an i is not good. Line L7 adds the test E′[k]=σ(E[K]). Together, these are the conditions of kind (b) for K. □
Lemma 2 (good positions propagate). If K≥q is good and K<n, then K+1 is good.
Proof. Let j=K+1 and j′=j+δ=σ(j), and let Γ be the set of good positions at most K, together with j. Since j>q, aj′′=aj.
Claim A: every origin of an item of E[j] is in Γ. The items of E[j] with an origin in Γ form a set that contains Scan(E[K],aj), since the origins of E[K] are good or equal to K, and K is good. It is closed under (P), which adds origin j. It is closed under (C): for [B→γ∙,i] with i∈Γ, either i=j and the new item takes its origin from an item of the set, or i≤K is good and the new item takes its origin from an item of E[i], which is good or equal to i. By Fact 1, E[j] is the least such set, so all its items are in this set.
Claim B: σ(E[j])⊆E′[j′]. Let Z be the set of items I∈E[j] with σ(I)∈E′[j′]. Z contains the seed: an item of Scan(E[K],aj) comes from J∈E[K], and σ(J)∈σ(E[K])=E′[K+δ] scans aj′′=aj into E′[j′]. Z is closed under (P), because the predicted item [B→∙η,j] maps to [B→∙η,j′], which (P) puts in E′[j′]. Z is closed under (C). Take [B→γ∙,i]∈Z, so i∈Γ by Claim A. If i=j and the waiting item J is also in Z, then (C) in E′[j′] with origin j′ applies to σ(J) and gives the image of the new item. If i≤K and J∈E[i], then σ(i)≤σ(K)<j′, and (C) in E′[j′] reads E′[σ(i)]=σ(E[i]), which contains σ(J), so it gives the image of the new item. By the leastness in Fact 1, Z=E[j].
Claim C: E′[j′]⊆σ(E[j]). Let W=σ(E[j]). W contains the new seed Scan(E′[K+δ],aj)=Scan(σ(E[K]),aj)=σ(Scan(E[K],aj)). W is closed under (P) at j′, since a prediction from σ(J) is the image of the prediction from J. W is closed under (C) at j′. Take σ[B→γ∙,i]∈W with [B→γ∙,i]∈E[j], so i∈Γ. If σ(i)=j′, then i=j, because σ(i)≤σ(K)<j′ for the other elements of Γ; the waiting items of W are images of waiting items of E[j], and (C) in E[j] gives the preimage of the new item. If σ(i)<j′, then i≤K is good, and (C) reads E′[σ(i)]=σ(E[i]); its waiting items are images of waiting items of E[i], and (C) in E[j] again gives the preimage. By the leastness in Fact 1, E′[j′]⊆W.
So E′[j+δ]=σ(E[j]), j≥q, and every origin o<j of E[j] is in Γ∖{j}, so it is good. Hence j is good. □
Theorem. REPARSE(G,w,E,p,q,v) terminates and returns w′ and the Earley chart E′[0],…,E′[n′] of w′. Hence line L12 returns true if and only if S⇒∗w′.
Proof. The loop of line L3 runs at most n′−p+1 times, and each step ends, so the call terminates. Line L1 builds w′. By Lemma 1, the sets that lines L2 and L4 build are E′[0],…,E′[k] when the loop reaches k. If line L8 never finds an anchored set, the loop computes every set at line L4, and line L11 returns E′. Otherwise line L8 stops at some k≥q′ with ANCH[k] true, so K=k−δ is good by Lemma 1. By Lemma 2 and induction, every position j with K≤j≤n is good, so E′[j+δ]=σ(E[j]). These are the sets that line L9 adds for the positions k+1,…,n′, and line L10 returns E′. The last claim follows from the correctness of Earley's algorithm: S⇒∗w′ if and only if some [S→α∙,0] is in E′[n′]. ■