Class Bnf

java.lang.Object
io.jenetics.ext.grammar.Bnf

public final class Bnf extends Object
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 Details

    • parse

      public static Cfg<String> parse(String grammar)
      Parses the given BNF grammar string to a Cfg object. The following example show 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 BNF grammar string
      Returns:
      the parsed BNF object
      Throws:
      ParsingException - if the given grammar is invalid
      NullPointerException - it the given grammar string is null
    • format

      public static String format(Cfg<?> grammar)
      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 give grammar is null