I am trying to understand how one can re-create a document parsed by a parser generated by grako.
After burying myself deep in the grako source code, I believe I have finally understood how one returns from the AST to generated document. Could somebody please check that my following understanding is correct, and let me know if there is a more straight forward method?
- One creates a PEG grammar one wishes to parse. Grako creates a parser class and a sematics class based on it.
- One creates (by hand) a python module containing (more or less) a separate class (subclass of
grako.model.Node
) for every rule in one's grammar. Each class must at least have a constructor with parameters for every named element in the corresponding rule and store its values in a class property. - One subclasses (by hand) the generated semantics class to replace the ast for each rule by the corresponding class one created in step 2.
- One creates (by hand) a python module a subclass of
grako.codegen.ModelRenderer
defining the template for "code" generation for (more or less) each rule in one's grammar. - One feeds the AST consisting of the Node subclasses and the python module containing the templates to
grako.codegen.CodeGenerator().render(...)
to create the output.
Can this be right? This does not seem intuitive at all.
- Why would one go through the significant effort of step 2 & 3 to do nothing more than store the information that is already contained in the AST?
- What is the advantage of this approach rather than working directly from the AST?
- Is there a way to automate or sidestep steps 2 & 3 if one only wants to recreate a document in the original grammar?
- Given a PEG grammar definition, is it theoretically possible to automatically create a "code generator generator" just as one creates a "parser generator"?