pub struct Grammar<T, N, A> {
pub rules: BTreeMap<N, Vec<Rhs<T, N, A>>>,
pub start: N,
}Expand description
A context-free grammar.
Fields§
§rules: BTreeMap<N, Vec<Rhs<T, N, A>>>The rules for each nonterminal.
start: NThe 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.
Implementations§
Source§impl<T: Ord, N: Ord, A> Grammar<T, N, A>
impl<T: Ord, N: Ord, A> Grammar<T, N, A>
Sourcepub fn lr0_state_machine<'a>(&'a self) -> LR0StateMachine<'a, T, N, A>
pub fn lr0_state_machine<'a>(&'a self) -> LR0StateMachine<'a, T, N, A>
Create the LR(0) state machine for a grammar.
Sourcepub fn first_sets(&self) -> BTreeMap<&N, (BTreeSet<&T>, bool)>
pub 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).
Sourcepub fn follow_sets<'a>(
&'a self,
first: BTreeMap<&'a N, (BTreeSet<&'a T>, bool)>,
) -> BTreeMap<&'a N, (BTreeSet<&'a T>, bool)>
pub 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)
Sourcepub fn lalr1<'a, FR, FO>(
&'a self,
reduce_on: FR,
priority_of: FO,
) -> Result<LR1ParseTable<'a, T, N, A>, LR1Conflict<'a, T, N, A>>
pub fn lalr1<'a, FR, FO>( &'a self, reduce_on: FR, priority_of: FO, ) -> Result<LR1ParseTable<'a, T, N, A>, LR1Conflict<'a, T, N, A>>
Try to create an LALR(1) parse table out of the grammar.
You can tweak the behaviour of the parser in two ways:
reduce_onis 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 (orNonefor 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 anelsetoken.priority_ofallows 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.
Trait Implementations§
Auto Trait Implementations§
impl<T, N, A> Freeze for Grammar<T, N, A>where
N: Freeze,
impl<T, N, A> RefUnwindSafe for Grammar<T, N, A>
impl<T, N, A> Send for Grammar<T, N, A>
impl<T, N, A> Sync for Grammar<T, N, A>
impl<T, N, A> Unpin for Grammar<T, N, A>where
N: Unpin,
impl<T, N, A> UnwindSafe for Grammar<T, N, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more