#ast-parser #ebnf #earley-parser #ast #earley #grammar #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

#33 in Parser tooling

Download history 19/week @ 2025-01-07 2/week @ 2025-01-14 6/week @ 2025-02-04 3/week @ 2025-02-11 4/week @ 2025-02-25 1/week @ 2025-03-04 5/week @ 2025-03-11 1/week @ 2025-03-18

701 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