earlgrey

Crates.ioearlgrey
lib.rsearlgrey
version0.4.1
sourcesrc
created_at2016-09-10 18:49:05.333505
updated_at2024-10-11 04:27:35.569019
descriptionA library for parsing context-free grammars using Earley algorithm
homepage
repositoryhttps://github.com/rodolf0/tox/tree/master/earlgrey
max_upload_size
id6320
size1,207,675
Rodolfo Granata (rodolf0)

documentation

README

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());
  }
}
Commit count: 591

cargo fmt