Note that there are some explanatory texts on larger screens.

plurals
  1. POANTLR Tree Grammar and StringTemplate Code Translation
    text
    copied!<p>I am working on a code translation project with a sample ANTLR tree grammar as:</p> <pre><code>start: ^(PROGRAM declaration+) -&gt; program_decl_tmpl(); declaration: class_decl | interface_decl; class_decl: ^(CLASS ^(ID CLASS_IDENTIFIER)) -&gt; class_decl_tmpl(cid={$CLASS_IDENTIFIER.text}); </code></pre> <p>The group template file for it looks like:</p> <pre><code>group My; program_decl_tmpl() ::= &lt;&lt; *WHAT?* &gt;&gt; class_decl_tmpl(cid) ::= &lt;&lt; public class &lt;cid&gt; {} &gt;&gt; </code></pre> <p>Based on this, I have these questions: </p> <ul> <li>Everything works fine apart from that what I should express in <code>WHAT?</code> to say that a program is simply a list of class declarations to get the final generated output? </li> <li>Is this approach <strong>averagely</strong> suitable for not so a high-level language? </li> <li>I have also studied <a href="http://www.antlr.org/wiki/display/ST/Language+Translation+Using+ANTLR+and+StringTemplate" rel="nofollow">ANTLR Code Translation with String Templates</a>, but it seems that this approach takes much advantage of interleaving code in tree grammar. Is it also possible to do it as much as possible <em>just</em> in String Templates? </li> </ul> <p><strong>SOLUTION</strong>, I add the solution based on what Terence proposed: </p> <pre><code>start: ^(PROGRAM d+=declaration+) -&gt; program_decl_tmpl(decls={$d}); declaration: cd = class_decl -&gt; decl_tmpl(decl={$cd.st}) | id = interface_decl -&gt; decl_tmpl(decl={$id.st}); class_decl: ^(CLASS ^(ID CLASS_IDENTIFIER)) -&gt; class_decl_tmpl(cid={$CLASS_IDENTIFIER.text}); </code></pre> <p>And the templates would be:</p> <pre><code>group My; program_decl_tmpl(decls) ::= &lt;&lt; &lt;decls&gt; &gt;&gt; decl_tmpl(decl) ::= &lt;&lt; &lt;decl&gt; &gt;&gt; class_decl_tmpl(cid) ::= &lt;&lt; public class &lt;cid&gt; {} &gt;&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload