java.lang.Object
io.jenetics.ext.grammar.DerivationTreeGenerator<T>
- All Implemented Interfaces:
Generator<T,
Tree<Cfg.Symbol<T>, ?>>
public final class DerivationTreeGenerator<T>
extends Object
implements Generator<T,Tree<Cfg.Symbol<T>,?>>
Standard implementation of a derivation-tree generator. The following code
snippet lets you generate a derivation tree from a given grammar.
final Cfg<String> cfg = Bnf.parse("""
<expr> ::= ( <expr> <op> <expr> ) | <num> | <var> | <fun> ( <arg>, <arg> )
<fun> ::= FUN1 | FUN2
<arg> ::= <expr> | <var> | <num>
<op> ::= + | - | * | /
<var> ::= x | y
<num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
"""
);
final var random = RandomGenerator.of("L64X256MixRandom");
final var generator = new DerivationTreeGenerator<String>(
SymbolIndex.of(random),
1_000
);
final Tree<Symbol<String>, ?> tree = generator.generate(cfg);
- Since:
- 7.1
- Version:
- 7.1
- See Also:
-
Constructor Summary
ConstructorDescriptionDerivationTreeGenerator
(SymbolIndex index, int limit) Create a new derivation tree generator from the given parameters. -
Method Summary
-
Constructor Details
-
DerivationTreeGenerator
Create a new derivation tree generator from the given parameters.- Parameters:
index
- the symbol index function used for generating the derivation treelimit
- the maximal allowed nodes of the tree. If the generated tree exceeds this length, the generation is interrupted and an empty tree is returned. If a tree is empty can be checked withTree.isEmpty()
.
-
-
Method Details
-
generate
Generates a new derivation tree from the given grammar, cfg.
-