NT - is an Enum type with one value for every nonterminal in the grammarpublic interface Parser<NT extends Enum<NT>>
Parsers are constructed by calling compile() with a grammar, which might be stored in a string, in a file, or read from a stream.
Once constructed, a Parser object is used by calling parse() on a sequence of characters (represented as a string or file or stream). Its result is a ParseTree showing how that string matches the grammar.
The type parameter NT should be an Enum type with the same (case-insensitive) names as
the nonterminals in the grammar. This allows nonterminals to be referred to by your Java code with static checking
and type safety. For example, if your grammar is:
String sumGrammar = "expression ::= number '+' number ; number ::= [0-9]+;"then you should create a nonterminal enum like this:
enum SumGrammar { EXPRESSION, NUMBER };
and then use:
Parser<SumGrammar>.compile(sumGrammar, SumGrammar.EXPRESSION)to compile it into a parser.
The grammar of a grammar is as follows.
@skip whitespaceAndComments {
grammar ::= ( production | skipBlock )+
production ::= nonterminal '::=' union ';'
skipBlock ::= '@skip' nonterminal '{' production* '}'
union :: = concatenation ('|' concatenation)*
concatenation ::= repetition+
repetition ::= unit repeatOperator
repeatOperator ::= [*+?]
unit ::= nonterminal | terminal | '(' union ')'
}
nonterminal ::= [a-zA-Z_][a-zA-Z_0-9]*
terminal ::= quotedString | characterSet | anyChar | characterClass
quotedString ::= "'" ([^'\r\n\\] | '\\' . )+ "'" // e.g. 'hello', '\'', '\r\n\t'
| '"' ([^"\r\n\\] | '\\' . )+ '"' // e.g. "world", "\"", "\r\n\t"
characterSet ::= '[' ([^\]\r\n\\] | '\\' . )+ ']' // e.g. [abc], [a-z], [^a-z], [\]], [\r\n\t]
anyChar ::= '.'
characterClass ::= '\\' [dsw] // e.g. \d, \s, \w
whitespaceAndComments ::= (whitespace | oneLineComment | blockComment)*
whitespace ::= [ \t\r\n]*
oneLineComment ::= '//' [^\r\n]* [\r\n]+
blockComment ::= '/*' [^*]* '*' ([^/]* '*')* '/'
| Modifier and Type | Method and Description |
|---|---|
static <NT extends Enum<NT>> |
compile(File in,
NT rootNonterminal)
Compile a Parser from a grammar stored in a file.
|
static <NT extends Enum<NT>> |
compile(InputStream in,
NT rootNonterminal)
Compile a Parser from a grammar represented as an InputStream.
|
static <NT extends Enum<NT>> |
compile(Reader in,
NT rootNonterminal)
Compile a Parser from a grammar represented as a Reader stream.
|
static <NT extends Enum<NT>> |
compile(String grammar,
NT rootNonterminal)
Compile a Parser from a grammar represented as a string.
|
ParseTree<NT> |
parse(File f)
Parses a file based on the grammar internally represented by the parser.
|
ParseTree<NT> |
parse(InputStream stream)
Parses a stream based on the grammar internally represented by the parser.
|
ParseTree<NT> |
parse(Reader in)
Parses a stream based on the grammar internally represented by the parser.
|
ParseTree<NT> |
parse(String string)
Parses a string based on the grammar internally represented by the parser.
|
static final String VERSION
static <NT extends Enum<NT>> Parser<NT> compile(String grammar, NT rootNonterminal) throws UnableToParseException
NT - an Enum type that contains one value for every nonterminal in the grammar.grammar - the grammar to userootNonterminal - the desired root nonterminal in the grammarUnableToParseException - if the grammar has a syntax errorstatic <NT extends Enum<NT>> Parser<NT> compile(Reader in, NT rootNonterminal) throws UnableToParseException, IOException
NT - an Enum type that contains one value for every nonterminal in the grammar.in - contains the grammarrootNonterminal - the desired root nonterminal in the grammarUnableToParseException - if the grammar has a syntax errorIOException - if the stream has an I/O errorstatic <NT extends Enum<NT>> Parser<NT> compile(File in, NT rootNonterminal) throws UnableToParseException, IOException
NT - an Enum type that contains one value for every nonterminal in the grammar.in - contains the grammarrootNonterminal - the desired root nonterminal in the grammarUnableToParseException - if the grammar has a syntax errorIOException - if the file is missing or has an I/O errorstatic <NT extends Enum<NT>> Parser<NT> compile(InputStream in, NT rootNonterminal) throws UnableToParseException, IOException
NT - an Enum type that contains one value for every nonterminal in the grammar.in - contains the grammarrootNonterminal - the desired root nonterminal in the grammarUnableToParseException - if the grammar has a syntax errorIOException - if the stream has an I/O errorParseTree<NT> parse(String string) throws UnableToParseException
string - string to parseParseTree representing a successful parse of the stringUnableToParseException - if string cannot be parsed, describing approximately where the parsing error occurredParseTree<NT> parse(File f) throws UnableToParseException, IOException
f - File containing the text to be parsedParseTree representing a successful parse of the content of the fileUnableToParseException - if the file cannot be parsed, describing approximately where the parsing error occurredIOException - if the file has an I/O error.ParseTree<NT> parse(Reader in) throws UnableToParseException, IOException
in - stream from which to read the text to be parsed.ParseTree representing a successful parse of the content of the streamUnableToParseException - if the stream cannot be parsed, describing approximately where the parsing error occurredIOException - if the stream has an I/O error.ParseTree<NT> parse(InputStream stream) throws UnableToParseException, IOException
stream - stream from which to read the text to be parsed.ParseTree representing a successful parse of the content of the streamUnableToParseException - if the stream cannot be parsed, describing approximately where the parsing error occurredIOException - if the stream has an I/O error.