Class SourceGrammar
A grammar is a list of rules. Each rule defines a non-terminal symbol as a sequence of zero or
more symbols (terminals or nonterminals).
A grammar can be used to create a parser. The parser will (successfully) parse inputs that match the rules in the grammar.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new grammar with default options.SourceGrammar(ParserOptions options) Create a new grammar with a specific set of options.SourceGrammar(SourceGrammar current) Create a new grammar from an existing grammar. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRule(NonterminalSymbol nonterminal, List<Symbol> rhs) Add a rule to the grammar.voidaddRule(NonterminalSymbol nonterminal, Symbol... rhs) Add a rule to the grammar.voidAdd a rule to the grammar.booleanDoes this grammar contain an equivalent rule?getCompiledGrammar(ParserType parserType, NonterminalSymbol seed) Get a hygiene report for this grammar.getNonterminal(String name) Return the nonterminal symbol identified by name.getNonterminal(String name, List<ParserAttribute> attributes) Return the nonterminal symbol identified by name.getNonterminal(String name, ParserAttribute attribute) Return the nonterminal symbol identified by name.getParser(ParserOptions options, String seed) getParser(ParserOptions options, NonterminalSymbol seed) Get a parser for this grammar.Gets the parser options.booleanisNullable(Symbol symbol) Is the symbol nullable?Resolve duplicate symbols in the grammar.voidsetMetadataProperty(String name, String value) Sets a metadata property.Methods inherited from class org.nineml.coffeegrinder.parser.Grammar
getMetadataProperies, getMetadataProperty, getRules, getRulesBySymbol, getRulesForSymbol, getSymbols
-
Field Details
-
logcategory
- See Also:
-
-
Constructor Details
-
SourceGrammar
public SourceGrammar()Create a new grammar with default options. -
SourceGrammar
Create a new grammar with a specific set of options.- Parameters:
options- The options.- Throws:
NullPointerException- if options is null.
-
SourceGrammar
Create a new grammar from an existing grammar.- Parameters:
current- the grammar to copy
-
-
Method Details
-
getNonterminal
Return the nonterminal symbol identified by name.Nonterminal symbols are uniquely identified by their name.
Any string can be used as a name.
- Parameters:
name- The name of this symbol.- Returns:
- The nonterminal for the name specified.
- Throws:
NullPointerException- if the name is null.
-
getNonterminal
Return the nonterminal symbol identified by name.Nonterminal symbols are uniquely identified by their name.
Any string can be used as a name.
- Parameters:
name- The name of this symbol.attribute- an attribute.- Returns:
- The nonterminal for the name specified.
- Throws:
NullPointerException- if the name is null or the attribute is null.
-
getNonterminal
Return the nonterminal symbol identified by name.Nonterminal symbols are uniquely identified by their name.
Any string can be used as a name.
- Parameters:
name- The name of this symbol.attributes- attributes to associate with this symbol, may be null- Returns:
- The nonterminal for the name specified.
- Throws:
NullPointerException- if the name is null.
-
addRule
Add a rule to the grammar.Multiple rules can exist for the same
NonterminalSymbol. There should be at least one rule for every nonterminal symbol that occurs on the "right hand side" of a rule. A symbol that is undefined but unreachable from the specified start symbol when parsing, isn't forbidden by CoffeeGrinder, but it is by CoffeeFilter.Once added, a rule can never be removed.
- Parameters:
rule- The rule to add- Throws:
GrammarException- if any nonterminal in the rule is not from this grammar, or if the grammar is closed
-
addRule
Add a rule to the grammar.This is a convenience method that will construct the
Rulefor you.Multiple rules can exist for the same
NonterminalSymbol. There must be at least one rule for every nonterminal symbol that occurs on the "right hand side" of a rule.- Parameters:
nonterminal- The nonterminal symbol defined by this rule.rhs- The list of symbols that define it- Throws:
GrammarException- if any nonterminal in the rule is not from this grammar or if the grammar is closed
-
addRule
Add a rule to the grammar.This is a convenience method that will construct the
Rulefor you.Multiple rules can exist for the same
NonterminalSymbol. There must be at least one rule for every nonterminal symbol that occurs on the "right hand side" of a rule.- Parameters:
nonterminal- The nonterminal symbol defined by this rule.rhs- The list of symbols that define it.- Throws:
GrammarException- if any nonterminal in the rule is not from this grammar or if the grammar is closed
-
isNullable
Description copied from class:GrammarIs the symbol nullable?A
TerminalSymbolis never nullable.For a
ParserGrammar, the answer is definitive. For anSourceGrammar, a symbol that isn't currently nullable could become nullable by the addition of more rules.- Specified by:
isNullablein classGrammar- Parameters:
symbol- The symbol.- Returns:
- true if the symbol is nullable
-
getParser
-
getCompiledGrammar
-
getCompiledGrammar
-
getParser
Get a parser for this grammar.Returns a parser that will parse an input against the rules that define this grammar.
- Parameters:
options- the options for this parser.seed- TheNonterminalSymbolthat your input is expected to match.- Returns:
- the parser
-
resolveDuplicates
Resolve duplicate symbols in the grammar.This method may return a different SourceGrammar. In this new grammar, all of the unique nonterminals will have unique names. (That is, if a nonterminal A occurs twice in the grammar with different parser attributes, it will have two different names in the resolved grammar.)
This method is public only so that it is possible to know what all of the nonterminals in the parse grammar will be.
- Returns:
- a grammar with duplicates resolved
-
getParserOptions
Gets the parser options.- Returns:
- the current options.
-
getHygieneReport
Get a hygiene report for this grammar.- Parameters:
seed- The seed rule for hygiene checking.- Returns:
- the report.
-
contains
Does this grammar contain an equivalent rule?Two rules are equivalent if they have the same symbol, the same list of right-hand-side symbols, and if the attributes and optionality of every symbol on the right-hand-side are the same in both rules.
- Parameters:
candidate- the candidate rule- Returns:
- true if the grammar contains an equivalent rule
-
setMetadataProperty
Sets a metadata property.Metadata properties exist solely for annotations by an application. They have no bearing on the function of the grammar.
- Parameters:
name- the name of the propertyvalue- the value of the property, or null to remove a property- Throws:
NullPointerException- if the name is null
-