Learn R Programming

gramEvol (version 2.1-4)

GrammarGetNextSequence: Grammar Iterator

Description

Iterates through grammar's valid sequences.

Usage

GrammarGetFirstSequence(grammar, 
            seqStart = NULL, 
            startSymb = GrammarStartSymbol(grammar),
            max.depth = GrammarGetDepth(grammar),
            max.len = GrammarMaxSequenceLen(grammar, max.depth, startSymb))

GrammarGetNextSequence(grammar, seqStart = NULL, startSymb = GrammarStartSymbol(grammar), max.depth = GrammarGetDepth(grammar), max.len = GrammarMaxSequenceLen(grammar, max.depth, startSymb)) is.GrammarOverflow(object)

Arguments

grammar

A grammar object.

seqStart

The sequence to be incremented. For a value of NULL, the first sequence is returned. Partial sequences are completed and returned.

startSymb

The non-terminal symbol where the generation of a new expression should start.

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.

max.len

Maximum length of sequence to return. Used to avoid recursion.

object

An object to be tested.

Value

GrammarGetFirstSequence returns a numeric vector representing the first sequence of the grammar.

GrammarGetNextSequence returns a numeric vector or a GrammarOverflow object.

is.GrammarOverflow returns TRUE if object is a GrammarOverflow, otherwise FALSE.

Details

GrammarGetFirstSequence returns the first sequence that creates a valid expression with the given grammar object. GrammarGetNextSequence allows iterating through all valid sequences in a grammar. If a seqStart = NULL is used, GrammarGetFirstSequence is called to and the first sequence in the grammar is returned. Calling GrammarGetNextSequence or GrammarGetFirstSequence with an incomplete sequence returns a full-length sequence starting with the given seqStart.

When GrammarGetNextSequence reaches the last of all valid sequences, it returns a GrammarOverflow object. This object can be identified using is.GrammarOverflow.

See Also

GrammaticalExhaustiveSearch

Examples

Run this code
# NOT RUN {
# Define a simple grammar
# <expr> ::= <var><op><var>
# <op>   ::= + | - | *
# <var>  ::= A | B
ruleDef <- list(expr = gsrule("<var><op><var>"),
                op   = gsrule("+", "-", "*"),
                var  = gsrule("A", "B"))

# Create a grammar object
grammarDef <- CreateGrammar(ruleDef)			   

# Iterate and print all valid sequence and expressions
string <- NULL
while (TRUE) {
  string <- GrammarGetNextSequence(grammarDef, string)

  if (is.GrammarOverflow(string)) {
    break
  }

  expr <- GrammarMap(string, grammarDef)
  cat(string, " -> ", as.character(expr), "\n")
}

# test a partial string
GrammarGetNextSequence(grammarDef, c(0, 0, 2))

# }

Run the code above in your browser using DataLab