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
Nis a finite set; each elementn ∈ Nis 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.Tis 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.Ris a finite relation inN × (N ∪ T)∗, where the asterisk represents the Kleene star operation. The members ofRare called the (rewrite) rules (Cfg.Rule) or productions of the grammar.Sis 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 interfaceInterface for annotatable CFG types.static final classBuilder class for buildingCfgobjects.static interfaceRepresents an element of a CFGstatic final recordRepresents one expression (list of alternative symbols) a production rule consists of.static final recordRepresents the non-terminal symbols of the grammar (NT).static final recordRepresents a production rule of the grammar (R).static interfaceRepresents the symbols the BNF grammar consists.static final recordRepresents 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.booleaninthashCode()<A> Cfg<A> map(Function<? super Cfg.Terminal<? extends T>, ? extends A> mapper) Maps the values of the terminal symbols from typeTto 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 ofthisgrammar.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 givenstartsymbol.rules()Return the rules ofthisgrammar.start()Return the start symbol ofthisgrammar.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 givennameandvalue.static <T> Cfg.Terminal<T> Factory method for creating a terminal symbol with the givennameandvalue.Return the terminal symbols ofthisgrammar.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 ofthisgrammarterminals- the terminal symbols ofthisgrammarrules- the production rules ofthisgrammarstart- the start symbol ofthisgrammar- Throws:
NullPointerException- if one of the arguments isnullIllegalArgumentException- 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 ofthisgrammar. The returned symbols have no annotation.- Returns:
- the non-terminal symbols of
thisgrammar
-
terminals
Return the terminal symbols ofthisgrammar. The returned symbols have no annotation.- Returns:
- the terminal symbols of
thisgrammar
-
rules
Return the rules ofthisgrammar.- Returns:
- the rules of
thisgrammar
-
start
Return the start symbol ofthisgrammar.- Returns:
- the start symbol of
thisgrammar
-
rule
Return the rule for the givenstartsymbol.- Parameters:
start- the start symbol of the rule- Returns:
- the rule for the given
startsymbol - Throws:
NullPointerException- if the givenstartsymbol isnull
-
map
Maps the values of the terminal symbols from typeTto 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 givennameandvalue.- 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 givennameandvalue.- 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 ofsymbolsis empty
-
R
Factory method for creating a new rule. Theelementsarray doesn't allowCfg.Ruleobjects.- 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 ofalternativesis 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. Theelementsarray doesn't allowCfg.Ruleobjects.- 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 ofalternativesis emptyNullPointerException- if one of the arguments isnull
-
R
Factory method for creating a new rule. Theelementsarray doesn't allowCfg.Ruleobjects.- 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.