Class AbstractAssemblyGrammar<NT extends AssemblyNonTerminal,P extends AbstractAssemblyProduction<NT>>
- Type Parameters:
NT
- the type of non-terminalsP
- the type of productions, which must have the same types of (non-)terminals.
- All Implemented Interfaces:
Iterable<P>
- Direct Known Subclasses:
AssemblyExtendedGrammar
,AssemblyGrammar
As in classic computer science, a CFG consists of productions of non-terminals and terminals. The left-hand side of the a production must be a single non-terminal, but the right-hand side may be any string of symbols. To avoid overloading the term "String," here we call it a "Sentential."
To define a grammar, simply construct an appropriate subclass (probably AssemblyGrammar
)
and call addProduction(AbstractAssemblyProduction)
or
addProduction(AssemblyNonTerminal, AssemblySentential)
.
By default, the start symbol is taken from the left-hand side of the first production added to the grammar.
-
Field Summary
Modifier and TypeFieldDescriptionprotected String
protected final Map
<String, AssemblySymbol> protected final Map
<String, AssemblyTerminal> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addProduction
(NT lhs, AssemblySentential<NT> rhs) Add a production to the grammarvoid
addProduction
(P prod) Add a production to the grammarvoid
combine
(AbstractAssemblyGrammar<NT, P> that) Add all the productions of a given grammar to this oneboolean
Check if the grammar contains any symbol with the given namegetNonTerminal
(String name) Get the named non-terminalgetStart()
Get the start symbol for the grammarGet the name of the start symbol for the grammargetTerminal
(String name) Get the named terminalprotected boolean
isPureRecursive
(P prod) Check if the given production is purely recursive, i.e., of the form I => Iiterator()
Traverse the productionsprotected abstract P
newProduction
(NT lhs, AssemblySentential<NT> rhs) Because a subclass may have a different type of production, it must provide a mechanism for constructing an appropriate production given just the LHS and RHS.Get the non-terminalsvoid
print
(PrintStream out) Print the productions of this grammar to the given streamGet all productions where the left-hand side is the given non-terminalproductionsOf
(String name) Get all productions where the left-hand side non-terminal has the given namevoid
Change the start symbol for the grammarvoid
setStartName
(String startName) Change the start symbol for the grammarGet the terminalsvoid
verify()
Check that the grammar is consistentMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
productions
protected final org.apache.commons.collections4.MultiValuedMap<String,P extends AbstractAssemblyProduction<NT>> productions -
prodList
-
nonterminals
-
terminals
-
symbols
-
startName
-
-
Constructor Details
-
AbstractAssemblyGrammar
public AbstractAssemblyGrammar()
-
-
Method Details
-
newProduction
Because a subclass may have a different type of production, it must provide a mechanism for constructing an appropriate production given just the LHS and RHS.- Parameters:
lhs
- the left-hand side of the productionrhs
- the right-hand side of the production- Returns:
- the constructed production
-
addProduction
Add a production to the grammar- Parameters:
lhs
- the left-hand siderhs
- the right-hand side
-
addProduction
Add a production to the grammar- Parameters:
prod
- the production
-
isPureRecursive
Check if the given production is purely recursive, i.e., of the form I => I- Parameters:
prod
- the production to check- Returns:
- true iff the production is purely recursive
-
setStart
Change the start symbol for the grammar- Parameters:
nt
- the new start symbol
-
setStartName
Change the start symbol for the grammar- Parameters:
startName
- the name of the new start symbol
-
getStart
Get the start symbol for the grammar- Returns:
- the start symbol
-
getStartName
Get the name of the start symbol for the grammar- Returns:
- the name of the start symbol
-
getNonTerminal
Get the named non-terminal- Parameters:
name
- the name of the desired non-terminal- Returns:
- the non-terminal, or null if it is not in this grammar
-
getTerminal
Get the named terminal- Parameters:
name
- the name of the desired terminal- Returns:
- the terminal, or null if it is not in this grammar
-
combine
Add all the productions of a given grammar to this one- Parameters:
that
- the grammar whose productions to add
-
print
Print the productions of this grammar to the given stream- Parameters:
out
- the stream
-
verify
Check that the grammar is consistentThe grammar is consistent if every non-terminal appearing in the grammar also appears as the left-hand side of some production. If not, such non-terminals are said to be undefined.
- Throws:
AssemblyGrammarException
- the grammar is inconsistent, i.e., contains undefined non-terminals.
-
iterator
Traverse the productions- Specified by:
iterator
in interfaceIterable<NT extends AssemblyNonTerminal>
-
nonTerminals
Get the non-terminals- Returns:
-
terminals
Get the terminals- Returns:
-
productionsOf
Get all productions where the left-hand side non-terminal has the given name- Parameters:
name
- the name of the non-terminal- Returns:
- all productions "defining" the named non-terminal
-
productionsOf
Get all productions where the left-hand side is the given non-terminal- Parameters:
nt
- the non-terminal whose defining productions to find- Returns:
- all productions "defining" the given non-terminal
-
contains
Check if the grammar contains any symbol with the given name- Parameters:
name
- the name to find- Returns:
- true iff a terminal or non-terminal has the given name
-