Grako "code" generation
<p>I am trying to understand how one can re-create a document parsed by a parser generated by <a href="https://bitbucket.org/apalala/grako">grako</a>.</p>

<p>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?</p>

<ol>
<li>One creates a PEG grammar one wishes to parse. Grako creates a parser class and a sematics class based on it.</li>
<li>One creates (by hand) a python module containing (more or less) a separate class (subclass of <code>grako.model.Node</code>) 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.</li>
<li>One subclasses (by hand) the generated semantics class to replace the ast for each rule by the corresponding class one created in step 2. </li>
<li>One creates (by hand) a python module a subclass of <code>grako.codegen.ModelRenderer</code> defining the template for "code" generation for (more or less) each rule in one's grammar. </li>
<li>One feeds the AST consisting of the Node subclasses and the python module containing the templates to <code>grako.codegen.CodeGenerator().render(...)</code> to create the output.</li>
</ol>

<p>Can this be right? This does not seem intuitive at all.</p>

<ul>
<li>Why would one go through the significant effort of step 2 &amp; 3 to do nothing more than store the information that is already contained in the AST?</li>
<li>What is the advantage of this approach rather than working directly from the AST?</li>
<li>Is there a way to automate or sidestep steps 2 &amp; 3 if one only wants to recreate a document in the original grammar?</li>
<li>Given a PEG grammar definition, is it theoretically possible to automatically create a "code generator generator" just as one creates a "parser generator"?</li>
</ul>