Class TreeBuilder

java.lang.Object
org.libxmq.imp.ixml.earley.TreeBuilder

public final class TreeBuilder extends Object
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().
  • Constructor Details

    • TreeBuilder

      public TreeBuilder(Chart chart, Grammar grammar)
      Create a builder for an accepted chart.
      Parameters:
      chart - the chart, after EarleyParser.removeForbiddenByNots
      grammar - the grammar
  • Method Details

    • build

      public Tree build()
      Compose the tree of the whole input.
      Returns:
      the tree of the start rule, or null if there is none
    • steps

      public List<TreeBuilder.Step> 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