Struct GlrAnalysis

Source
pub struct GlrAnalysis {}
Expand description

An analysis of conflicts and their resolvability via GLR/LR(k).

Implementations§

Source§

impl GlrAnalysis

Source

pub fn compute(grammar: &Grammar, item_sets: &ItemSets) -> GlrAnalysis

Analyze a grammar.

Examples found in repository?
examples/glr-analysis.rs (line 48)
9fn main() {
10    // Build the following grammar which has a shift/reduce conflict in the
11    // first item set, which can be resolved with an additional lookahead token.
12    //
13    //   S : A B z ;
14    //
15    //   B : B y C | C ;
16    //   C : x ;
17    //
18    //   A : D | E ;
19    //   D : x ;
20    //   E : epsilon ;
21    //
22    let mut g = Grammar::new();
23
24    let nt_s = g.add_nonterminal("S");
25    let nt_a = g.add_nonterminal("A");
26    let nt_b = g.add_nonterminal("B");
27    let nt_c = g.add_nonterminal("C");
28    let nt_d = g.add_nonterminal("D");
29    let nt_e = g.add_nonterminal("E");
30    let t_x = g.add_terminal("x");
31    let t_y = g.add_terminal("y");
32    let t_z = g.add_terminal("z");
33
34    g.add_rule(Rule::new(nt_s, vec![nt_a.into(), nt_b.into(), t_z.into()]));
35    g.add_rule(Rule::new(nt_b, vec![nt_b.into(), t_y.into(), nt_c.into()]));
36    g.add_rule(Rule::new(nt_b, vec![nt_c.into()]));
37    g.add_rule(Rule::new(nt_c, vec![t_x.into()]));
38    g.add_rule(Rule::new(nt_a, vec![nt_d.into()]));
39    g.add_rule(Rule::new(nt_a, vec![nt_e.into()]));
40    g.add_rule(Rule::new(nt_d, vec![t_x.into()]));
41    g.add_rule(Rule::new(nt_e, vec![]));
42
43    // Compute the item sets for the grammar.
44    let is = ItemSets::compute(&g);
45    println!("{}", is.pretty(&g));
46
47    // Perform the GLR analysis.
48    let ga = GlrAnalysis::compute(&g, &is);
49    println!("{:#?}", ga);
50
51    // Generate the parser code.
52    // let sm = StateMachine::try_from(&is).unwrap();
53    // let backend = Backend::new();
54    // generate_parser(
55    //     &mut File::create("tests/generated/tribble2_parser.rs").unwrap(),
56    //     &backend,
57    //     &sm,
58    //     &g,
59    // ).unwrap();
60}

Trait Implementations§

Source§

impl Debug for GlrAnalysis

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.