Question

Is there any way to parse source string with custom (e)bnf and get AST as json? Let me explain what do i need:

  • I have source string and bnf grammar (as string too).
  • I put EBNF as lexer.
  • Add source.
  • Get AST and save it as JSON.
Was it helpful?

Solution

"EBNF as lexer" is nonsensical. But the rest of your question can be interpreted as, "can I get a parser driven by an EBNF to produce an AST in JSON form?"

Sure.

Most parser generators accept (E)BNF and "parse". Most of them don't automatically produce an AST; they make the programmer define how each rule should generate tree nodes. Those won't work for your task.

Some do generate ASTs as data structures automatically using just the BNF and a source file: ANTLR4 (I think), and our DMS Software Reengineering Toolkit. Neither of these produce JSON directly, but in both cases it should be straighforward to write (once) a generic tree-walker that spits JSON.

DMS's BNF will handle any context-free grammar using just BNF rules. ANTLR4 handles most grammars but has restrictions on what you can write (e.g., certain kinds of left recursion are verboten), and requirements for you to add extra disambiguating information where the grammar isn't LL(1).

DMS will export XML directly. See this example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top