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.
Fields
rules: BTreeMap<N, Vec<Rhs<T, N, A>>>
The rules for each nonterminal.
start: N
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.
Methods
impl<T: Ord, N: Ord, A> Grammar<T, N, A>
[src]
fn lr0_state_machine<'a>(&'a self) -> LR0StateMachine<'a, T, N, A>
Create the LR(0) state machine for a grammar.
fn first_sets(&self) -> BTreeMap<&N, (BTreeSet<&T>, bool)>
Compute the FIRST sets of the grammar. Returns a map from nonterminal to (first set, nullable).
fn follow_sets<'a>(&'a self, first: BTreeMap<&'a N, (BTreeSet<&'a T>, bool)>) -> BTreeMap<&'a N, (BTreeSet<&'a T>, bool)>
Compute the FOLLOW sets of the grammar. Returns a map mapping from nonterminal to (follow set, whether follow set contains EOF)
fn lalr1<'a, FR, FO>(&'a self, reduce_on: FR, priority_of: FO) -> Result<LR1ParseTable<'a, T, N, A>, LR1Conflict<'a, T, N, A>> where FR: FnMut(&Rhs<T, N, A>, Option<&T>) -> bool, FO: FnMut(&Rhs<T, N, A>, Option<&T>) -> i32
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 (orNone
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 anelse
token.priority_of
allows you to resolve reduce-reduce conflicts, by giving reduce rules different "priorities". This takes the same parameters asreduce_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.