Class TreeBuilder
java.lang.Object
org.libxmq.imp.ixml.earley.TreeBuilder
Compose the parse tree from a chart that accepted the input.
The chart only says which items were alive where. The tree is found by
working backwards from the complete start item in the last set:
A rule A matched the input from..to if the set at to has the complete item
(A → x y z ·, from). To find its children, take the symbols from the last
one backwards. The last symbol z ends at to. If z is a terminal it starts at
to-1. If z is a rule it starts at some mid, where z matched mid..to, and the
symbols before it, x y, matched from..mid. That is true exactly when the set
at mid has the item (A → x y · z, from). Then continue with y, ending at mid.
When there is more than one way, a production or a split point, the one with
the lowest cost wins. The cost of a tree is the sum of the costs of its
rules (ixml rule =< ...). If two ways have the same lowest cost the tree is
ambiguous, and the first one found is used: productions in grammar order
and, for a split, the shortest match of the earlier symbols.
A grammar with a cycle, eg A = A | 'a', has infinitely many trees. A rule
that is already being composed over the same span is skipped, so the trees
without the cycle remain.
The results are remembered, each rule over each span is composed once.
The steps record the order in which the nodes are added: top-down, from the
start rule, and the last symbol of a production first. See steps().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA node added to the tree: a production over a span of the input. -
Constructor Summary
ConstructorsConstructorDescriptionTreeBuilder(Chart chart, Grammar grammar) Create a builder for an accepted chart. -
Method Summary
-
Constructor Details
-
TreeBuilder
-
-
Method Details
-
build
Compose the tree of the whole input.- Returns:
- the tree of the start rule, or null if there is none
-
steps
The nodes in the order they were added, numbered 1, 2, ... Every production that was tried and composed a tree is a node, also one that lost on cost, like the alternatives that yaep composes. A rule over a span is only composed once, so a node that is used again is not added again.- Returns:
- the steps, call build() first
-