Learn R Programming

gramEvol (version 2.1-4)

GrammarRandomExpression: Random Expression Generation

Description

Creates random expressions from context-free grammar.

Usage

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)

Arguments

grammar

A grammar object.

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 inputString if non-terminal symbols are still remaining.

retries

Number of retries until a terminal and valid expressions is found.

Value

An expressions, or a list of expressions.

Details

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.

Examples

Run this code
# NOT RUN {
# 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)
# }

Run the code above in your browser using DataLab