Record Class Cfg<T>

java.lang.Object
java.lang.Record
io.jenetics.ext.grammar.Cfg<T>
Type Parameters:
T - the terminal symbol value type

public record Cfg<T>(List<Cfg.NonTerminal<T>> nonTerminals, List<Cfg.Terminal<T>> terminals, List<Cfg.Rule<T>> rules, Cfg.NonTerminal<T> start) extends Record
Represents a context-free grammar (CFG).

Formal definition

A context-free grammar G is defined by the 4-tuple G = (N, T, R, S), where

  • N is a finite set; each element n ∈ N is called a non-terminal (Cfg.NonTerminal) character or a variable. Each variable represents a different type of phrase or clause in the sentence. Variables are also sometimes called syntactic categories. Each variable defines a sub-language of the language defined by G.
  • T is a finite set of terminals (Cfg.Terminal) disjoint from N, which make up the actual content of the sentence. The set of terminals is the alphabet of the language defined by the grammar G.
  • R is a finite relation in N × (N ∪ T)∗, where the asterisk represents the Kleene star operation. The members of R are called the (rewrite) rules (Cfg.Rule) or productions of the grammar.
  • S is the start variable (or start symbol), used to represent the whole sentence (or program). It must be an element of N (Cfg.NonTerminal) .
You can easily create a Cfg object from a given BNF grammar.
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 """ );
It is also possible to create the grammar above programmatically.
final Cfg<String> grammar = Cfg.of( R("expr", E(NT("num")), E(NT("var")), E(T("("), NT("expr"), NT("op"), NT("expr"), T(")")) ), R("op", E(T("+")), E(T("-")), E(T("*")), E(T("/"))), R("var", E(T("x")), E(T("y"))), R("num", E(T("0")), E(T("1")), E(T("2")), E(T("3")), E(T("4")), E(T("5")), E(T("6")), E(T("7")), E(T("8")), E(T("9")) ) );
Since:
7.1
Version:
7.1
See Also:
  • Constructor Details

  • Method Details

    • rule

      public Optional<Cfg.Rule<T>> rule(Cfg.NonTerminal<?> start)
      Return the rule for the given start symbol.
      Parameters:
      start - the start symbol of the rule
      Returns:
      the rule for the given start symbol
      Throws:
      NullPointerException - if the given start symbol is null
    • map

      public <A> Cfg<A> map(Function<? super Cfg.Terminal<T>,? extends A> mapper)
      Maps the values of the terminal symbols from type T to type A.
      Type Parameters:
      A - the new value type of the terminal symbols
      Parameters:
      mapper - the mapper function
      Returns:
      the mapped grammar
      Throws:
      NullPointerException - if the given mapper is null
    • of

      public static <T> Cfg<T> of(List<Cfg.Rule<T>> rules)
      Create a grammar object with the given rules. Duplicated rules are merged into one rule. The start symbol of the first rule is chosen as the start symbol of the created CFG
      Parameters:
      rules - the rules the grammar consists of
      Throws:
      IllegalArgumentException - if the list of rules is empty
      NullPointerException - if the list of rules is null
    • of

      @SafeVarargs public static <T> Cfg<T> of(Cfg.Rule<T>... rules)
      Create a grammar object with the given rules. Duplicated rules are merged into one rule. The start symbol of the first rule is chosen as the start symbol of the created CFG
      Parameters:
      rules - the rules the grammar consists of
      Throws:
      IllegalArgumentException - if the list of rules is empty
      NullPointerException - if the list of rules is null
    • T

      public static <T> Cfg.Terminal<T> T(String name, T value)
      Factory method for creating a terminal symbol with the given name and value.
      Type Parameters:
      T - the terminal symbol value type
      Parameters:
      name - the name of the terminal symbol
      value - the value of the terminal symbol
      Returns:
      a new terminal symbol
    • T

      public static Cfg.Terminal<String> T(String name)
      Factory method for creating a terminal symbol with the given name.
      Parameters:
      name - the name of the terminal symbol
      Returns:
      a new terminal symbol
    • N

      public static <T> Cfg.NonTerminal<T> N(String name)
      Factory method for creating non-terminal symbols.
      Type Parameters:
      T - the terminal symbol value type
      Parameters:
      name - the name of the symbol.
      Returns:
      a new non-terminal symbol
    • E

      @SafeVarargs public static <T> Cfg.Expression<T> E(Cfg.Symbol<T>... symbols)
      Factory method for creating an expression with the given symbols.
      Type Parameters:
      T - the terminal symbol value type
      Parameters:
      symbols - the list of symbols of the expression
      Returns:
      a new expression
      Throws:
      IllegalArgumentException - if the list of symbols is empty
    • R

      @SafeVarargs public static <T> Cfg.Rule<T> R(String name, Cfg.Expression<T>... alternatives)
      Factory method for creating a new rule.
      Type Parameters:
      T - the terminal symbol value type
      Parameters:
      name - the name of start symbol of the rule
      alternatives - the list af alternative rule expressions
      Returns:
      a new rule
      Throws:
      IllegalArgumentException - if the given list of alternatives is empty
      NullPointerException - if one of the arguments is null
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • nonTerminals

      Returns the value of the nonTerminals record component.
      Returns:
      the value of the nonTerminals record component
    • terminals

      Returns the value of the terminals record component.
      Returns:
      the value of the terminals record component
    • rules

      public List<Cfg.Rule<T>> rules()
      Returns the value of the rules record component.
      Returns:
      the value of the rules record component
    • start

      public Cfg.NonTerminal<T> start()
      Returns the value of the start record component.
      Returns:
      the value of the start record component