java.lang.Object
io.jenetics.ext.grammar.Cfg<T>
- Type Parameters:
T
- the terminal symbol value type
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 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> cfg = Bnf.parse("""
<expr> ::= <num> | <var> | '(' <expr> <op> <expr> ')'
<op> ::= + | - | * | /
<var> ::= x | y
<num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
"""
);
Creating Cfg programmatically
final Cfg<String> cfg = Cfg.of(
R("expr",
N("num"),
N("var"),
E(T("("), N("expr"), N("op"), N("expr"), T(")"))
),
R("op", T("+"), T("-"), T("*"), T("/")),
R("var", T("x"), T("y")),
R("num",
T("0"), T("1"), T("2"), T("3"),
T("4"), T("5"), T("6"), T("7"),
T("8"), T("9")
)
);
Creating Cfg programmatically with builders
Using the CFG builder makes it easier to define annotation for symbols without pushing the Java generics to its edges.
final Cfg<String> cfg = Cfg.<String>builder()
.R(N("expr", "Rule start annotation"), rule -> rule
.N("num", "Non-terminal annotation 1")
.N("var", "Non-terminal annotation 2")
.E(exp -> exp
.add(T("(").at("Terminal annotation"))
.N("expr").N("op").N("expr")
.T(")")
.at("Expression annotation")))
.R("op", rule -> rule.T("+").T("-").T("*").T("/"))
.R("var", rule -> rule.T("x").T("y"))
.R("num", rule -> rule
.T("0").T("1").T("2").T("3").T("4")
.T("5").T("6").T("7").T("8").T("9")
.at("Rule annotation")
)
.build();
Annotating CFG elements can be used to influence the Generator
classes,
which creates sentences from a given grammar.
- Since:
- 7.1
- Version:
- 8.2
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface for annotatable CFG types.static final class
Builder class for buildingCfg
objects.static interface
Represents an element of a CFGstatic 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
ConstructorsConstructorDescriptionCfg
(List<Cfg.NonTerminal<T>> nonTerminals, List<Cfg.Terminal<T>> terminals, List<Cfg.Rule<T>> rules, Cfg.NonTerminal<T> start) Deprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Cfg.Builder
<T> builder()
Return a new CFG builder.static <T> Cfg.Expression
<T> E
(Cfg.Symbol<T>... symbols) Factory method for creating an expression with the givensymbols
.boolean
int
hashCode()
<A> Cfg
<A> map
(Function<? super Cfg.Terminal<? extends 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.static <T> Cfg.NonTerminal
<T> Factory method for creating non-terminal symbols.Return the non-terminal symbols ofthis
grammar.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
(Cfg.NonTerminal<T> start, Cfg.Element<T>... elements) Factory method for creating a new rule.static <T> Cfg.Rule
<T> R
(String name, Cfg.Element<T>... elements) Factory method for creating a new rule.static <T> Cfg.Rule
<T> R
(String name, Cfg.Expression<T>... alternatives) Deprecated, for removal: This API element is subject to removal in a future version.Will be removed, useR(String, Element[])
insteadrule
(Cfg.NonTerminal<?> start) Return the rule for the givenstart
symbol.rules()
Return the rules ofthis
grammar.start()
Return the start symbol ofthis
grammar.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
.static <T> Cfg.Terminal
<T> Factory method for creating a terminal symbol with the givenname
andvalue
.Return the terminal symbols ofthis
grammar.toString()
-
Constructor Details
-
Cfg
@Deprecated(forRemoval=true, since="8.2") public Cfg(List<Cfg.NonTerminal<T>> nonTerminals, List<Cfg.Terminal<T>> terminals, List<Cfg.Rule<T>> rules, Cfg.NonTerminal<T> start) Deprecated, for removal: This API element is subject to removal in a future version.This constructor will be removed, useof(Rule[])
orof(List)
instead.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
-
nonTerminals
Return the non-terminal symbols ofthis
grammar. The returned symbols have no annotation.- Returns:
- the non-terminal symbols of
this
grammar
-
terminals
Return the terminal symbols ofthis
grammar. The returned symbols have no annotation.- Returns:
- the terminal symbols of
this
grammar
-
rules
Return the rules ofthis
grammar.- Returns:
- the rules of
this
grammar
-
start
Return the start symbol ofthis
grammar.- Returns:
- the start symbol of
this
grammar
-
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
-
hashCode
-
equals
-
toString
-
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 rule list is empty or 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()
NullPointerException
- if the list of rules isnull
-
of
Create a grammar object with the given rules.- Parameters:
rules
- the rules the grammar consists of- Throws:
IllegalArgumentException
- if the list of rules is empty or there are duplicate rulesNullPointerException
- 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
andvalue
.- Type Parameters:
T
- the terminal symbol value type- Parameters:
name
- the name of the terminal symbolvalue
- the value of the terminal symbolannotation
- the terminal annotation- Returns:
- a new terminal symbol
- Since:
- 8.2
-
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
-
N
Factory method for creating non-terminal symbols.- Type Parameters:
T
- the terminal symbol value type- Parameters:
name
- the name of the symbol.annotation
- the annotation of the symbol- Returns:
- a new non-terminal symbol
- Since:
- 8.2
-
E
Factory method for creating an expression with the givensymbols
.- Type Parameters:
T
- the terminal symbol value type- Parameters:
symbols
- the expression symbols- Returns:
- a new expression
- Throws:
IllegalArgumentException
- if the list ofsymbols
is empty
-
R
Factory method for creating a new rule. Theelements
array doesn't allowCfg.Rule
objects.- Type Parameters:
T
- the terminal symbol value type- Parameters:
name
- the name of start symbol of the ruleelements
- the list of alternative rule expressions- Returns:
- a new rule
- Throws:
IllegalArgumentException
- if the given list ofalternatives
is emptyNullPointerException
- if one of the arguments isnull
-
R
@Deprecated(forRemoval=true, since="8.2") @SafeVarargs public static <T> Cfg.Rule<T> R(String name, Cfg.Expression<T>... alternatives) Deprecated, for removal: This API element is subject to removal in a future version.Will be removed, useR(String, Element[])
insteadFactory method for creating a new rule. Theelements
array doesn't allowCfg.Rule
objects.- Type Parameters:
T
- the terminal symbol value type- Parameters:
name
- the name of start symbol of the rulealternatives
- the list of alternative rule expressions- Returns:
- a new rule
- Throws:
IllegalArgumentException
- if the given list ofalternatives
is emptyNullPointerException
- if one of the arguments isnull
-
R
Factory method for creating a new rule. Theelements
array doesn't allowCfg.Rule
objects.- Type Parameters:
T
- the terminal symbol value type- Parameters:
start
- the start symbol of the ruleelements
- the list of alternative rule expressions- Returns:
- a new rule
- Throws:
NullPointerException
- if one of the arguments isnull
- Since:
- 8.2
-
builder
Return a new CFG builder.- Type Parameters:
T
- the terminal symbol value type- Returns:
- a new CFG builder
- Since:
- 8.2
-
of(Rule[])
orof(List)
instead.