vnmakarov / yaep

Yet Another Earley Parser
Other
135 stars 13 forks source link
ambiguous-grammars ast earley-parser error-recovery grammar library minimal-cost-ast

YAEP -- standalone Earley parser library

YAEP features:

Usage example:

static const char *description =
"\n"
"TERM NUMBER;\n"
"E : T         # 0\n"
"  | E '+' T   # plus (0 2)\n"
"  ;\n"
"T : F         # 0\n"
"  | T '*' F   # mult (0 2)\n"
"  ;\n"
"F : NUMBER    # 0\n"
"  | '(' E ')' # 1\n"
"  ;\n"
  ;

static void parse (void)
{
  struct grammar *g;
  struct earley_tree_node *root;
  int ambiguous_p;

  if ((g = earley_create_grammar ()) == NULL) {
      fprintf (stderr, "earley_create_grammar: No memory\n");
      exit (1);
  }
  if (earley_parse_grammar (g, TRUE, description) != 0) {
      fprintf (stderr, "%s\n", earley_error_message (g));
      exit (1);
    }
  if (earley_parse (g, read_token_func, syntax_error_func, parse_alloc_func,
                    NULL, &root, &ambiguous_p))
    fprintf (stderr, "earley_parse: %s\n", earley_error_message (g));
  earley_free_grammar (g);
}

Installing:

Speed comparison of YACC, MARPA, YAEP, and GCC parsers:

Parse time only Overall Memory (parse only) MB
YACC 0.07 0.17 20
MARPA 3.48 3.77 516
YAEP 0.18 0.28 26
Parse time only Overall Memory (parse only) MB
YACC 0.25 0.55 120
gcc -fsyntax-only - 1.22 194
gcc -O0 - 19.37 761
MARPA 22.23 23.41 30310
YAEP 1.43 1.68 142

Future directions