Search code examples
cparsingbnf

C library that takes a BNF definition and parses accordingly?


I'm looking for a library that can take a string that defines a BNF and another string that has the text to parse and parses the latter according to the former. Note that I don't want something that requires writing the definition in a source code form that then needs to be compiled


Solution

  • libmarpa which implements the Marpa algorithm, does general BNF parsing. However, it doesn't accept a BNF as a string — you need to build the grammar from the string and the lexer as, e.g., in this JSON parser.

    Only a half of what you've asked for, but arguably the hardest half.

    In a meanwhile, Marpa::R2 based on libmarpa does exactly what you need -- takes a string describing BNF and lexer rules and builds the recognizer -- a parser you can use directly to produce an AST or evaluate it via semantic actions.

    There are efforts to wrap libmarpa conveniently to do what you said 1, 2.

    There is also a Kollos project aimed at extending and wrapping libmarpa using Lua.

    Hope it helps.