I am working on an ANTLR model grammar code translation project as follows:
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 this:
group My;
program_decl_tmpl() ::= <<
*WHAT?*
>>
class_decl_tmpl(cid) ::= <<
public class <cid> {}
>>
Based on this, I have the following questions:
- Everything works fine, except what should I express in
WHAT?to say that the program is just a list of class declarations to get the final generated output? - Is this approach on average suitable for a not very high level of language?
- I also studied ANTLR Code Translation with String Templates , but it seems that this approach has a big advantage in interleaving code in a tree grammar. Is it possible to do as much as possible in String Templates?
, , , :
start: ^(PROGRAM d+=declaration+) -> program_decl_tmpl(decls={$d});
declaration: cd = class_decl -> decl_tmpl(decl={$cd.st})
| id = interface_decl -> decl_tmpl(decl={$id.st});
class_decl: ^(CLASS ^(ID CLASS_IDENTIFIER))
-> class_decl_tmpl(cid={$CLASS_IDENTIFIER.text});
:
group My;
program_decl_tmpl(decls) ::= <<
<decls>
>>
decl_tmpl(decl) ::= <<
<decl>
>>
class_decl_tmpl(cid) ::= <<
public class <cid> {}
>>