Proofs: GLL parsing
These are the human proofs of GLL parsing, at full length.
Correctness of the GLL recognizer full
This proof shows: correctness.
Setting. is any context-free grammar and the input; for . A GSS node other than the root is a pair with ; we say that calls at . The root calls at 0. A configuration is visited if it is a descriptor taken from at line L2, or is obtained from a visited configuration by one match of line L3. Every descriptor added to is also put in , and the loop runs until is empty, so every descriptor in is eventually taken, and every visited configuration is followed by lines L3 to L5 from it.
Soundness
Invariants. At every moment of the run:
- (I1) If is visited or in , then calls at some and .
- (I2) If the edge exists with , then the configuration has been visited.
- (I3) If and calls at , then ; if was recorded at line L10, then .
Proof. All three hold at the start, when everything is empty. We check every step that adds to one of the sets.
- Line L1 adds : calls at 0 and .
- Line L3 turns a visited with into ; by I1, .
- Line L4 processes a visited , with calling at and . CREATE makes , which calls at . A new edge satisfies I2 because the configuration just processed is the one I2 asks for. A replay at line L8 adds for ; by I3, , so , and calls at , which is I1. The descriptors satisfy I1 since calls at .
- Line L5 processes a visited , so is a production and, with calling at , . If , then and , and line L10 records correctly. Otherwise line L11 may add to , which satisfies I3. For each edge , with , invariant I2 says was visited, so by I1 calls at some with . The added descriptor then has , which is I1.
No other step changes , the edges or .
In particular, if line L10 records then , so every accepted input is in , and every span reported from is derived.
Termination
A slot is a production and a dot position, so there are of them. GSS nodes are pairs of a slot and a position in , so there are at most of them with the root, and descriptors are triples of a slot, a node and a position, so is finite. Line L12 puts each descriptor into at most once, so the main loop takes at most descriptors. Processing one is finite: line L3 advances the dot at most times, line L4 loops over the finitely many productions of , and the loops of lines L8 and L11 run over the finite sets and the edges of one node. So the run terminates on every grammar and input, including grammars with cycles .
Completeness
For a derivation tree, its size is the number of its internal nodes (nodes labelled by nonterminals). For a sequence and a derivation , its size is the sum of the sizes of the trees of , where a terminal has a tree of size 0.
Lemma A. Let be visited, and let by a tree of size ( when is a terminal). Then is visited.
Lemma B. Let be visited, and let with size . Then is visited.
Proof of both, by strong induction on . We prove Lemma A for size assuming Lemma B for sizes less than , and Lemma B for size assuming Lemma A for sizes at most .
Lemma A, a terminal. Then , and , so line L3 matches and is visited by definition.
Lemma A, a nonterminal. The root of the tree uses a production and with size . Processing the visited configuration reaches line L4, which makes the node , ensures the edge , and adds unless it is already in ; either way it is in , hence taken from and visited. By Lemma B for size , is visited, and processing it calls POP at line L5. Since , after that call . Now compare the moments , when the edge was added, and , when was added to ; they are different steps. If , then at line L11 runs over the edges of , which include , and adds . If , then at line L8 runs over the entries of for , which include , and adds the same descriptor. In both cases it is in , hence visited.
Lemma B. Write and split as with , , and by a tree of size . If the claim is the hypothesis. Otherwise apply Lemma A times: from the visited it gives the visited , and after steps .
Theorem. The recognizer accepts if and only if . Moreover, for every GSS node that calls at and every with , the pair is in .
Proof. If the input is accepted, line L10 recorded , and by invariant I3 . Conversely, let . Line L1 adds , which is then visited, and Lemma B gives that is visited; processing it calls POP at line L5, so line L10 records and line L6 accepts. For the second claim, the node was created at line L4 while processing a visited , and the argument in the proof of Lemma A, with , shows that for every derivation .
Remark on the order. The proofs never use the order in which descriptors are taken from . Read the steps as rules: line L4 turns a configuration into a node, an edge and call descriptors; line L5 turns a configuration and an edge, or a configuration at the root, into a descriptor or a recorded end; and so on. Every member of the final sets was produced by a rule from members present earlier, and the final sets are closed under every rule, because each rule fires when the last of its premises appears (this is what lines L8 and L11 ensure for the pair of an edge and an entry of ). So the final sets are the least sets closed under the rules, whatever the order. ∎