Struct lalr::Grammar [] [src]

pub struct Grammar<T, N, A> {
    pub rules: BTreeMap<N, Vec<Rhs<T, N, A>>>,
    pub start: N,

A context-free grammar.


The rules for each nonterminal.

The starting state. There must be exactly one rule of the form "start -> N", for some nonterminal N. start must not be referred to elsewhere in the grammar.


impl<T: Ord, N: Ord, A> Grammar<T, N, A>

Create the LR(0) state machine for a grammar.

Compute the FIRST sets of the grammar. Returns a map from nonterminal to (first set, nullable).

Compute the FOLLOW sets of the grammar. Returns a map mapping from nonterminal to (follow set, whether follow set contains EOF)

Try to create an LALR(1) parse table out of the grammar.

You can tweak the behaviour of the parser in two ways:

  • reduce_on is a predicate, allowing you to control certain reduce rules based on the lookahead token. This function takes two parameters: the rule, given by its right-hand side, and the lookahead token (or None for EOF). You can use this to resolve shift-reduce conflicts. For example, you can solve the "dangling else" problem by forbidding the reduce action on an else token.
  • priority_of allows you to resolve reduce-reduce conflicts, by giving reduce rules different "priorities". This takes the same parameters as reduce_on, so you can vary the priority based on the lookahead token. If there would be a reduce-reduce conflict between rules, but they have different priority, the one with higher priority is used.

Trait Implementations

impl<T: Debug, N: Debug, A: Debug> Debug for Grammar<T, N, A>

Formats the value using the given formatter.