Derivation trees of a context-free grammar #
A derivation tree, or parse tree, of a context-free grammar is a rose tree over its symbols,
RoseTree (Symbol T N). A grammar licenses a tree when every terminal node is a leaf and every
nonterminal node, read with the symbols of its children, is a rule: the trees a grammar licenses
are the local set of its rules (RoseTree.Licensed). The yield of a tree is the list of terminals
at its leaves, left to right.
Main definitions #
RoseTree.yield: the terminal frontier.ContextFreeGrammar.Licenses g: the local trees the grammarglicenses.RoseTree.ruleCount,RoseTree.corpusRuleCount: the number of applications of a rule in a tree and in a corpus of trees.RoseTree.ruleAt?: the rule applied at a Gorn address.
Main results #
RoseTree.Licensed.derives: a licensed tree derives its yield from its root symbol.ContextFreeGrammar.exists_licensed_tree: every word of the language has a licensed derivation tree from the start symbol.RoseTree.Licensed.replaceAt,RoseTree.Licensed.exists_repeat: replacing a subtree by one with the same root symbol keeps the tree licensed, and a long enough path in a licensed tree passes two nodes with the same nonterminal; together withRoseTree.numNodes_replaceAt_ltthese are the ingredients of the pumping lemma.
The yield #
The terminal frontier of a tree lists the terminals among its leaves, left to right.
Equations
- t.yield = List.filterMap Symbol.terminal? t.leafList
Instances For
Replacing inside the tree splits the yield into the terminals left of the address, the yield of the subtree there, and the terminals to its right.
Licensing #
A grammar licenses a local tree when it is a terminal without children, or a nonterminal whose children's symbols are the right-hand side of one of its rules.
Equations
Instances For
Induction over the trees a grammar licenses, by the two shapes it licenses.
Rule counts #
ruleCount r t counts the applications of the rule r in t, the nonterminal nodes whose
symbol and children's symbols spell out r.
Equations
- RoseTree.ruleCount r t = List.count (Symbol.nonterminal r.input, r.output) t.localTrees
Instances For
The number of applications of the rule r in a corpus of trees.
Equations
- RoseTree.corpusRuleCount r D = (Multiset.map (RoseTree.ruleCount r) D).sum
Instances For
The rule at an address #
The rule applied at a Gorn address is the nonterminal there with the symbols of its children,
and none off the tree or at a terminal.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Along an address into a licensed tree, every proper prefix ends at a nonterminal node, and so does the address itself when the subtree there has children.
By pigeonhole, a path of at least g.rules.card steps through a licensed tree, ending at a
node with children, passes two nodes with the same nonterminal.
Among the licensed trees with a given yield and root symbol there is one of least size.
Soundness and completeness #
Completeness. Every word of the language has a licensed derivation tree from the start symbol.