Module ielr::input

source ·
Expand description

Structures to build your Grammar.

The Grammar will then be used as input to compute_table.

Note on conflicts and their resolution

When trying to write a LR(1) grammar, one almost always stumble upon conflicts: places in the grammar where the parser generator thinks there is an ambiguity. Those will be reported when calling the compute_table function: it will return Error::Conflict.

It should be noted that conflicts are usually resolved when dealing with expressions (aka E + E, is it left or right associative ?). In most other cases, the actual grammar should be changed in order to avoid the conflict.

This crate provides two ways of dealing with such conflicts:

  • conflict resolution: by using the Grammar::add_conflict_solution method, you can resolve specific conflict. In a parser generator, those may be added in bulk when resolving conflicts related to expressions precedence.

See ConflictSolution for an example.

let expr = Node(0);
let plus = Token::new(1).unwrap();
let times = Token::new(2).unwrap();
let mut grammar = Grammar::new();
let add_prod = grammar.add_production(expr, vec![Symbol::Node(expr), Symbol::Token(plus), Symbol::Node(expr)]).unwrap();
let mul_prod = grammar.add_production(expr, vec![Symbol::Node(expr), Symbol::Token(times), Symbol::Node(expr)]).unwrap();
let precedence_family = grammar.add_precedence_family();
grammar
    .get_production_mut(add_prod)
    .unwrap()
    .set_left_precedence(precedence_family, 1)
    .set_right_precedence(precedence_family, 2);
grammar
    .get_production_mut(mul_prod)
    .unwrap()
    .set_left_precedence(precedence_family, 3)
    .set_right_precedence(precedence_family, 4);

Remark

On big grammars (like Rust’s 😉), precedence annotations slow the generator considerably. In should be fine on small/medium grammars though.

Structs

Enums

Type Definitions