View source: R/GrammarRandomExpression.R
| GrammarRandomExpression | R Documentation | 
Creates random expressions from context-free grammar.
GrammarRandomExpression(grammar, 
          numExpr = 1, 
          max.depth = length(grammar$def), 
          startSymb = GrammarStartSymbol(grammar),
          max.string = GrammarMaxSequenceRange(grammar, max.depth, startSymb, 
                                               approximate = TRUE), 
          wrappings = 3, 
          retries = 100)
| grammar | A  | 
| numExpr | Number of random expressions to generate. | 
| max.depth | Maximum depth of recursion, in case of a cyclic grammar. By default it is limited to the number of production rules in the grammar. | 
| startSymb | The symbol where the generation of a new expression should start. | 
| max.string | Maximum value for each element of the sequence. | 
| wrappings | The number of times the function is allowed to wrap around  | 
| retries | Number of retries until a terminal and valid expressions is found. | 
GrammarRandomExpression creates num.expr random expressions from the given grammar.
It can be used to quickly examine the expressibility of the grammar, 
or as a form of random search over the grammar.
An expressions, or a list of expressions.
# Define a simple grammar
# <expr> ::= <var><op><var>
# <op>   ::= + | - | *
# <var>  ::= A | B | C
ruleDef <- list(expr = gsrule("<var><op><var>"),
                op =   gsrule("+", "-", "*"),
                var =  grule(A, B, C))
# Create a grammar object
grammarDef <- CreateGrammar(ruleDef)
# Generate 5 random expressions
exprs <- GrammarRandomExpression(grammarDef, 5)
print(exprs)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.