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).
It is also possible to create the grammar above programmatically.
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 elementn ∈ 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 byG
.T
is a finite set of terminals (Cfg.Terminal
) disjoint fromN
, which make up the actual content of the sentence. The set of terminals is the alphabet of the language defined by the grammarG
.R
is a finite relation inN × (N ∪ T)∗
, where the asterisk represents the Kleene star operation. The members ofR
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 ofN
(Cfg.NonTerminal
) .
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
"""
);
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
Represents one expression (list of alternative symbols) a production rule consists of.static final record
Represents the non-terminal symbols of the grammar (NT
).static final record
Represents a production rule of the grammar (R
).static interface
Represents the symbols the BNF grammar consists.static final record
Represents a terminal symbols of the grammar (T
). -
Constructor Summary
ConstructorDescriptionCfg
(List<Cfg.NonTerminal<T>> nonTerminals, List<Cfg.Terminal<T>> terminals, List<Cfg.Rule<T>> rules, Cfg.NonTerminal<T> start) Create a new context-free grammar object. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Cfg.Expression<T>
E
(Cfg.Symbol<T>... symbols) Factory method for creating an expression with the givensymbols
.final boolean
Indicates whether some other object is "equal to" this one.final int
hashCode()
Returns a hash code value for this object.<A> Cfg<A>
map
(Function<? super Cfg.Terminal<T>, ? extends A> mapper) Maps the values of the terminal symbols from typeT
to typeA
.static <T> Cfg.NonTerminal<T>
Factory method for creating non-terminal symbols.Returns the value of thenonTerminals
record component.static <T> Cfg<T>
Create a grammar object with the given rules.static <T> Cfg<T>
Create a grammar object with the given rules.static <T> Cfg.Rule<T>
R
(String name, Cfg.Expression<T>... alternatives) Factory method for creating a new rule.rule
(Cfg.NonTerminal<?> start) Return the rule for the givenstart
symbol.rules()
Returns the value of therules
record component.start()
Returns the value of thestart
record component.static Cfg.Terminal<String>
Factory method for creating a terminal symbol with the givenname
.static <T> Cfg.Terminal<T>
Factory method for creating a terminal symbol with the givenname
andvalue
.Returns the value of theterminals
record component.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
Cfg
public Cfg(List<Cfg.NonTerminal<T>> nonTerminals, List<Cfg.Terminal<T>> terminals, List<Cfg.Rule<T>> rules, Cfg.NonTerminal<T> start) Create a new context-free grammar object.- Parameters:
nonTerminals
- the non-terminal symbols ofthis
grammarterminals
- the terminal symbols ofthis
grammarrules
- the production rules ofthis
grammarstart
- the start symbol ofthis
grammar- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if a rule is defined more than once, the start symbol points to a missing rule or the rules uses symbols not defined in the list ofnonTerminals()
orterminals()
-
-
Method Details
-
rule
Return the rule for the givenstart
symbol.- Parameters:
start
- the start symbol of the rule- Returns:
- the rule for the given
start
symbol - Throws:
NullPointerException
- if the givenstart
symbol isnull
-
map
Maps the values of the terminal symbols from typeT
to typeA
.- 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 isnull
-
of
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 emptyNullPointerException
- if the list of rules isnull
-
of
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 emptyNullPointerException
- if the list of rules isnull
-
T
Factory method for creating a terminal symbol with the givenname
andvalue
.- Type Parameters:
T
- the terminal symbol value type- Parameters:
name
- the name of the terminal symbolvalue
- the value of the terminal symbol- Returns:
- a new terminal symbol
-
T
Factory method for creating a terminal symbol with the givenname
.- Parameters:
name
- the name of the terminal symbol- Returns:
- a new terminal symbol
-
N
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
Factory method for creating an expression with the givensymbols
.- 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 ofsymbols
is empty
-
R
Factory method for creating a new rule.- Type Parameters:
T
- the terminal symbol value type- Parameters:
name
- the name of start symbol of the rulealternatives
- the list af alternative rule expressions- Returns:
- a new rule
- Throws:
IllegalArgumentException
- if the given list ofalternatives
is emptyNullPointerException
- if one of the arguments isnull
-
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. -
hashCode
Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
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 withObjects::equals(Object,Object)
. -
nonTerminals
Returns the value of thenonTerminals
record component.- Returns:
- the value of the
nonTerminals
record component
-
terminals
Returns the value of theterminals
record component.- Returns:
- the value of the
terminals
record component
-
rules
Returns the value of therules
record component.- Returns:
- the value of the
rules
record component
-
start
Returns the value of thestart
record component.- Returns:
- the value of the
start
record component
-