6.9.4. LR parser engine

6.9.4. LR parser engine

The 'parse' function is a temporary test parser to checkout the tables produced by LR parser generators.
data
A sequence of pairs. The first item in each pair should usually be a terminal symbol of the grammar, and the second item some value or other data associated with it.
gram
A sequence of productions.
tab
A pair (action, goto) containing LR parsing tables as follows: each table consists of a sequence of rows, one row for each non-error state of the parser. Each row is a single dictionary, keyed by a grammar symbol.

For the action table, the keys are terminals of the grammar, and the values are pairs consisting of an action indicator and an integer. The action indicator must be an empty string for an error, the letter 'r' for a reduce operation, the letter 's' for a shift operation, or the letter 'a' for an accept operation.

For a reduce, the integer is the index of the production in the grammar to be reduced, plus one. (The plus one is an artefact of the use of an augmented grammar in the LALR1 parser generator. This needs to be fixed.)

For a shift, the integer is a state to be shifted.

For the goto table, the keys are nonterminals of the grammar, the value is simply an integer denoting the state to be shifted.

start
The initial state of the parser. Defaults to 0.
reduce
The reduction functor to use.

This is the name of the attribute of a production which will be used to locate a function to be executed when the production is reduced, and defaults to 'func'. This facility is useful to allow a single grammar to have multiple action categories. For example, a grammar for expressions could build a parse tree, emit the input in reverse polish order, evaluate the expression, or generate code to evaluate the expression.