API Documentation

Grammar

Walkers

class grammaropt.grammar.Walker(grammar, min_depth=None, max_depth=None, strict_depth_limit=False)[source]

Walkers are objects that do a random walk (deterministic walks are a special case of random walks) on the production rules space of a given grammar : the responsability of a Walker is to choose a production rule whenever it has to, and to choose Type values whenever it has to. After the end of the walk, a trace is left describing the traversal of the space. The trace differs depending on the kind of Walker.

Parameters:

grammar : Grammar

grammar where to walk

min_depth : int

minimum depth of the parse tree.

max_depth : int

maximum depth of the parse tree. Note that it could exceed max_depth because when it reaches max_depth there is no garanthee that there would always be a terminal production rule to choose. The solution to this problem is that when max_depth is reached, non-terminal production rules stop from being candidates to be chosen, but when only what we can choose are non-terminal production rules, we just choose one of them, even if max_depth is exceeded, otherwise the obtained string will not be a valid one according to the grammar.

strict_depth_limit : bool

if True, when max_depth is reached, forbid any further production rules when a choice should be made. If False, even when max_depth is reached, choose terminals when terminals are available, otherwise keep applying production rules.

Methods

next_rule
next_value
walk
next_rule(rules)[source]

Given a set of production rules, choose the next one. Implemented by specific Walkers. depth is an int provides information about the current depth of the parsetree.

next_value(rule)[source]

Given a Type rule, choose its value. Implemented by specific Walkers.

walk(start=None)[source]

Do a random walk on the grammar.

Parameters:

start : str

str that provides the starting rule name if start is not provides it uses the default rule of the grammar (which is the first rule).

class grammaropt.grammar.DeterministicWalker(grammar, expr)[source]

a very specific Walker that uses a given str expression expr, parse it usign the grammar grammar, then use the parse tree to force the next rule to choose and the next value to choose to correspond exactly to the expression expr. This very specific walker trace is used by the RNN walker to compute the loss for a given groundtruth expressions. Unfortunately I had to patch the uncached_match method of parsimonious OneOf and Type rules of the grammar to get some missing information required in the trace, the missing information needed by DeterministicWalker was the parent rule that is used to create a Node.

Methods

next_rule
next_value
walk

Adapters

Models

Optimizers

Types