#context-free-grammar #grammar #earley #ast #ebnf #parser

earlgrey

A library for parsing context-free grammars using Earley algorithm

13 releases

0.4.1 Oct 11, 2024
0.3.2 Mar 24, 2022
0.3.0 Jan 20, 2020
0.2.4 May 1, 2019
0.0.4 Sep 10, 2016

#54 in Parser tooling

29 downloads per month
Used in 2 crates

MIT license

1MB
2K SLoC

Example

// Full code at examples/ebnftree.rs
fn main() {
  let grammar = r#"
    expr   := expr ('+'|'-') term | term ;
    term   := term ('*'|'/') factor | factor ;
    factor := '-' factor | power ;
    power  := ufact '^' factor | ufact ;
    ufact  := ufact '!' | group ;
    group  := num | '(' expr ')' ;
  "#;

  use std::str::FromStr;
  let grammar = earlgrey::EbnfGrammarParser::new(grammar, "expr")
      .plug_terminal("num", |n| f64::from_str(n).is_ok())
      .into_grammar()
      .unwrap();

  let parser = earlgrey::sexpr_parser(grammar).unwrap();

  for tree in parser(tokenizer(input.chars()))? {
      println!("{}", tree.print());
  }
}

No runtime deps