Automated grammar to codec generator ?

+1 vote
asked Aug 11, 2018 by kiryaka (130 points)
Were there any attempts to automatically generate file reader / codec source code from the grammar? If yes - are there any results, even uncompleted?

It feels like very reasonable PRO feature. In general application already has the ability to read the file based on the grammar, so the logic is somewhere there. Just need to create structures like "grammar to C struct" script and mix it with the reading code.

1 Answer

0 votes
answered Aug 23, 2018 by andreas (3,300 points)
The idea exists since a long time to generate parsing code that acts like the generic parser using a grammar.

However, depending on the features a grammar uses (like custom data types implemented in Lua or Python) the generated code would also have to use the scripting engines for Python or Lua. For simpler grammars I think quite performant parser code could be generated that has lookup tables for the decision which structure to parse.

So I'm not aware of some project that generates parsing code from a grammar.

On this page you find a script that exports structures from a grammar to C structs: https://www.synalysis.net/scripts.html

The generic parser you find in Hexinator and Synalyze It! is written in plain portably C so probably the best option would be to provide it as an API for other languages like Python or Ruby since the bindings already exist (using SWIG).
...