java.lang.Object
io.jenetics.ext.grammar.Bnf
This class contains methods for parsing and formatting context-free
grammars in
BNF
format.
final Cfg<String> grammar = Bnf.parse("""
<expr> ::= <num> | <var> | '(' <expr> <op> <expr> ')'
<op> ::= + | - | * | /
<var> ::= x | y
<num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
"""
);
- Since:
- 7.1
- Version:
- 7.1
-
Method Summary
-
Method Details
-
parse
Parses the given BNFgrammar
string to aCfg
object. The following example shows the grammar of a simple arithmetic expression.<expr> ::= <num> | <var> | '(' <expr> <op> <expr> ')' <op> ::= + | - | * | / <var> ::= x | y <num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
- Parameters:
grammar
- the BNFgrammar
string- Returns:
- the parsed
BNF
object - Throws:
ParsingException
- if the given grammar is invalidNullPointerException
- it the givengrammar
string isnull
-
format
Formats the given CFG as BNF grammar string.- Parameters:
grammar
- the CFG to format as BNF- Returns:
- the BNF formatted grammar string
- Throws:
NullPointerException
- if the givegrammar
isnull
-