Grammar

Struct Grammar 

Source
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: 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.

Implementations§

Source§

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

Source

pub fn lr0_state_machine<'a>(&'a self) -> LR0StateMachine<'a, T, N, A>

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

Source

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).

Source

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)

Source

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>>
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 (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§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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>
where N: Send, A: Send, T: Send,

§

impl<T, N, A> Sync for Grammar<T, N, A>
where N: Sync, A: Sync, T: Sync,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.