Chapter 5Forests, etc.

5.1ParseForest

The org.nineml.coffeegrinder.parser.ParseForest class.

The SPPF is a graph representation of all the (possibly infinite) parses that can be used to recognize the input sequence as a sentence in the grammar.

5.1.1Field summary

Public fields for the org.nineml.coffeegrinder.parser.ParseForest class.

public static final String logcategory = "Forest"

5.1.2Constructor summary

Public constructors for the org.nineml.coffeegrinder.parser.ParseForest class.

public ParseForest(ParserOptions options)

5.1.3Method summary

Public methods on the org.nineml.coffeegrinder.parser.ParseForest class.

public List<ForestNode> getAmbiguousNodes()

public List<ForestNode> getNodes()

Get the nodes in the graph.

public ParserOptions getOptions()

Get the options for this forest.

public int getParseTreeCount()

How many parse trees are there in this forest?

In an infinitely ambiguous graph, there are an infinite number of parse trees. However, CoffeeGrinder will never follow the same edge twice when constructing a tree, it won’t loop. So the number of available trees is always a finite number.

public ForestNode getRoot()

public boolean isAmbiguous()

Is the grammar represented by this graph ambiguous?

A grammar is ambiguous if there are more than two parses that will recognize the input.

public boolean isInfinitelyAmbiguous()

Is the grammar represented by this graph infinitely ambiguous?

If the answer is “true”, then the graph is infinitely ambiguous. If the graph is ambiguous and the anwer is “false”, then all that can be said is the single parse explored to check ambiguity did not encounter infinite ambiguity. It is not an assertion that no unexplored part of the graph contains a loop.

public String serialize()

Serialize the graph as XML.

public void serialize(PrintStream stream)

Serialize the graph as XML.

public void serialize(String filename)
throws ForestException if a error occurs attempt to write to the file

Serialize the graph as XML.

This method attempts to write the XML to a file.

public int size()

How big is the graph?

5.2Arborist

The org.nineml.coffeegrinder.trees.Arborist class.

5.2.1Field summary

Public fields for the org.nineml.coffeegrinder.trees.Arborist class.

public final ParseForest forest = (runtime initializer)

5.2.2Method summary

Public methods on the org.nineml.coffeegrinder.trees.Arborist class.

public boolean closesLoop(ForestNode node)

public Arborist getArborist(ParseForest forest)

public Arborist getArborist(ParseForest forest Axe axe)

public Set<Integer> getSelectedNodes()

public List<TreeSelection> getSelectedTrees()

public void getTree(TreeBuilder builder)

public boolean hasMoreTrees()

public boolean isAbsolutelyAmbiguous()

public boolean isAmbiguous()

public void reset()

5.3Lumberjack

The org.nineml.coffeegrinder.trees.Lumberjack class.

This class extends org.nineml.coffeegrinder.trees.Arborist.

The lumberjac extracts trees sequentially from a forest.

5.3.1Method summary

Public methods on the org.nineml.coffeegrinder.trees.Lumberjack class.

public boolean closesLoop(ForestNode node)
Inherited from org.nineml.coffeegrinder.trees.Arborist

public Arborist getArborist(ParseForest forest)
Inherited from org.nineml.coffeegrinder.trees.Arborist

public Arborist getArborist(ParseForest forest Axe axe)
Inherited from org.nineml.coffeegrinder.trees.Arborist

public Set<Integer> getSelectedNodes()
Inherited from org.nineml.coffeegrinder.trees.Arborist

public List<TreeSelection> getSelectedTrees()
Inherited from org.nineml.coffeegrinder.trees.Arborist

public void getTree(TreeBuilder builder)

public boolean hasMoreTrees()

public boolean isAbsolutelyAmbiguous()

public boolean isAmbiguous()

public void reset()

5.4TreeSurgeon

The org.nineml.coffeegrinder.trees.TreeSurgeon class.

This class extends org.nineml.coffeegrinder.trees.Arborist.

The tree surgeon makes no attempt to return trees in sequential order and treats every request as distinct.

5.4.1Method summary

Public methods on the org.nineml.coffeegrinder.trees.TreeSurgeon class.

public boolean closesLoop(ForestNode node)
Inherited from org.nineml.coffeegrinder.trees.Arborist

public Arborist getArborist(ParseForest forest)
Inherited from org.nineml.coffeegrinder.trees.Arborist

public Arborist getArborist(ParseForest forest Axe axe)
Inherited from org.nineml.coffeegrinder.trees.Arborist

public Set<Integer> getSelectedNodes()
Inherited from org.nineml.coffeegrinder.trees.Arborist

public List<TreeSelection> getSelectedTrees()
Inherited from org.nineml.coffeegrinder.trees.Arborist

public void getTree(TreeBuilder builder)

public boolean hasMoreTrees()

public boolean isAbsolutelyAmbiguous()

public boolean isAmbiguous()

public void reset()

5.5Axe

The org.nineml.coffeegrinder.trees.Axe interface.

5.5.1Method summary

Public methods on the Axe interface.

public void forArborist(Arborist arborist)

Who’s using this axe?

Do not pass the same axe to more than one Arborist.

public boolean isSpecialist()

Is this a specialist axe?

A specialist axe can consider every tree in the forest differently every time it is used. A non-specialist axe considers every tree at most once and returns all the trees that are selected.

public List<Family> select(ParseTree tree ForestNode forestNode int count List<Family> choices)
throws NullPointerException if null is returned
throws org.nineml.coffeegrinder.exceptions.TreeWalkerException if an invalid choice is made

Select a branch from a list of branches.

In an ambiguous forest, some nodes will have more than one possible choice. In any given tree, only one choice may be selected. This function is called to make the selection.

There will always be at least one element in the choices list when the method is called. The method must return at least one.

The first node in the list returned is the choice selected for the tree currently under construction. If only one choice is returned, the node becomes unambiguous on subsequent parses, the same selection will always be used. If additional choices are returned, they will be considered on subsequent parses. Note that if you want the selected choice to be considered on future parses, it must appear in the list twice. It is the only node that may appear twice.

public boolean wasAmbiguousSelection()

Was the previous selection ambiguous?

This method asks if the previous selection was ambiguous. If the axe ever indicates that an ambiguous selection was made, the resulting parse is considered ambiguous. For example, the SequentialAxe always returns true because it treats all choices as equivalent. The PriorityAxe only considers a selection ambiguous if there wasn’t a uniquely highest priority selection.

5.6SequentialAxe

The org.nineml.coffeegrinder.trees.SequentialAxe class.

5.6.1Constructor summary

Public constructors for the org.nineml.coffeegrinder.trees.SequentialAxe class.

public SequentialAxe()

public SequentialAxe(boolean avoidLoops)

5.6.2Method summary

Public methods on the org.nineml.coffeegrinder.trees.SequentialAxe class.

public void forArborist(Arborist arborist)

Who’s using this axe?

Do not pass the same axe to more than one Arborist.

public boolean isSpecialist()

Is this a specialist axe?

A specialist axe can consider every tree in the forest differently every time it is used. A non-specialist axe considers every tree at most once and returns all the trees that are selected.

public List<Family> select(ParseTree tree ForestNode forestNode int count List<Family> choices)
throws NullPointerException if null is returned
throws org.nineml.coffeegrinder.exceptions.TreeWalkerException if an invalid choice is made

Select a branch from a list of branches.

In an ambiguous forest, some nodes will have more than one possible choice. In any given tree, only one choice may be selected. This function is called to make the selection.

There will always be at least one element in the choices list when the method is called. The method must return at least one.

The first node in the list returned is the choice selected for the tree currently under construction. If only one choice is returned, the node becomes unambiguous on subsequent parses, the same selection will always be used. If additional choices are returned, they will be considered on subsequent parses. Note that if you want the selected choice to be considered on future parses, it must appear in the list twice. It is the only node that may appear twice.

public boolean wasAmbiguousSelection()

Was the previous selection ambiguous?

This method asks if the previous selection was ambiguous. If the axe ever indicates that an ambiguous selection was made, the resulting parse is considered ambiguous. For example, the SequentialAxe always returns true because it treats all choices as equivalent. The PriorityAxe only considers a selection ambiguous if there wasn’t a uniquely highest priority selection.

5.7PriorityAxe

The org.nineml.coffeegrinder.trees.PriorityAxe class.

5.7.1Constructor summary

Public constructors for the org.nineml.coffeegrinder.trees.PriorityAxe class.

public PriorityAxe()

public PriorityAxe(boolean avoidLoops)

5.7.2Method summary

Public methods on the org.nineml.coffeegrinder.trees.PriorityAxe class.

public void forArborist(Arborist arborist)

Who’s using this axe?

Do not pass the same axe to more than one Arborist.

public boolean isSpecialist()

Is this a specialist axe?

A specialist axe can consider every tree in the forest differently every time it is used. A non-specialist axe considers every tree at most once and returns all the trees that are selected.

public List<Family> select(ParseTree tree ForestNode node int count List<Family> choices)
throws NullPointerException if null is returned
throws org.nineml.coffeegrinder.exceptions.TreeWalkerException if an invalid choice is made

Select a branch from a list of branches.

In an ambiguous forest, some nodes will have more than one possible choice. In any given tree, only one choice may be selected. This function is called to make the selection.

There will always be at least one element in the choices list when the method is called. The method must return at least one.

The first node in the list returned is the choice selected for the tree currently under construction. If only one choice is returned, the node becomes unambiguous on subsequent parses, the same selection will always be used. If additional choices are returned, they will be considered on subsequent parses. Note that if you want the selected choice to be considered on future parses, it must appear in the list twice. It is the only node that may appear twice.

public boolean wasAmbiguousSelection()

Was the previous selection ambiguous?

This method asks if the previous selection was ambiguous. If the axe ever indicates that an ambiguous selection was made, the resulting parse is considered ambiguous. For example, the SequentialAxe always returns true because it treats all choices as equivalent. The PriorityAxe only considers a selection ambiguous if there wasn’t a uniquely highest priority selection.

5.8RandomAxe

The org.nineml.coffeegrinder.trees.RandomAxe class.

Whenever this axe has to make a choice, it picks randomly. There is no guarantee that this will ever find a tree.

5.8.1Constructor summary

Public constructors for the org.nineml.coffeegrinder.trees.RandomAxe class.

public RandomAxe()

5.8.2Method summary

Public methods on the org.nineml.coffeegrinder.trees.RandomAxe class.

public void forArborist(Arborist arborist)

Who’s using this axe?

Do not pass the same axe to more than one Arborist.

public boolean isSpecialist()

Is this a specialist axe?

A specialist axe can consider every tree in the forest differently every time it is used. A non-specialist axe considers every tree at most once and returns all the trees that are selected.

public List<Family> select(ParseTree tree ForestNode forestNode int count List<Family> choices)
throws NullPointerException if null is returned
throws org.nineml.coffeegrinder.exceptions.TreeWalkerException if an invalid choice is made

Select a branch from a list of branches.

In an ambiguous forest, some nodes will have more than one possible choice. In any given tree, only one choice may be selected. This function is called to make the selection.

There will always be at least one element in the choices list when the method is called. The method must return at least one.

The first node in the list returned is the choice selected for the tree currently under construction. If only one choice is returned, the node becomes unambiguous on subsequent parses, the same selection will always be used. If additional choices are returned, they will be considered on subsequent parses. Note that if you want the selected choice to be considered on future parses, it must appear in the list twice. It is the only node that may appear twice.

public boolean wasAmbiguousSelection()

Was the previous selection ambiguous?

This method asks if the previous selection was ambiguous. If the axe ever indicates that an ambiguous selection was made, the resulting parse is considered ambiguous. For example, the SequentialAxe always returns true because it treats all choices as equivalent. The PriorityAxe only considers a selection ambiguous if there wasn’t a uniquely highest priority selection.

5.9TreeBuilder

The org.nineml.coffeegrinder.trees.TreeBuilder interface.

5.9.1Method summary

Public methods on the TreeBuilder interface.

public void endAmbiguity(int id int leftExtent int rightExtent)

Called at the end of an ambiguity that is not marked by a single nonterminal.

public void endNonterminal(NonterminalSymbol symbol Map<String, String> attributes int leftExtent int rightExtent)

Called when a nonterminal ends.

public void endTree(boolean ambiguous boolean absolutelyAmbiguous boolean infinitelyAmbiguous)

Called last, when construction finishes.

public void startAmbiguity(int id int leftExtent int rightExtent)

Called at the start of an ambiguity that is not marked by a single nonterminal.

The ambiguity id will distinguish this ambiguity from any other ambiguity. The numbers are not sequential or guaranteed stable across parses.

public void startNonterminal(NonterminalSymbol symbol Map<String, String> attributes int leftExtent int rightExtent)

Called when a new nonterminal begins.

public void startTree()

Called first, when construction begins.

public void token(Token token Map<String, String> attributes int leftExtent int rightExtent)

Called when a terminal occurs.