ANTLR Tree Grammar and StringTemplate Code Translation

Posted by Behrooz Nobakht on Stack Overflow See other posts from Stack Overflow or by Behrooz Nobakht
Published on 2010-12-21T11:50:12Z Indexed on 2010/12/21 11:54 UTC
Read the original article Hit count: 352

Filed under:
|
|

I am working on a code translation project with a sample ANTLR tree grammar as:

start:               ^(PROGRAM declaration+) -> program_decl_tmpl();
declaration:         class_decl | interface_decl;
class_decl:          ^(CLASS ^(ID CLASS_IDENTIFIER))
                        -> class_decl_tmpl(cid={$CLASS_IDENTIFIER.text});

The group template file for it looks like:

group My;

program_decl_tmpl() ::= <<
*WHAT?*
>>

class_decl_tmpl(cid) ::= <<
public class <cid> {}
>>

Based on this, I have these questions:

  • Everything works fine apart from that what I should express in WHAT? to say that a program is simply a list of class declarations to get the final generated output?
  • Is this approach averagely suitable for not so a high-level language?
  • I have also studied ANTLR Code Translation with String Templates, 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 just in String Templates?

© Stack Overflow or respective owner

Related posts about antlr

Related posts about antlr3