我正在开发一个带有样本ANTLR树语法的代码转换项目:
start: ^(PROGRAM declaration+) -> program_decl_tmpl();
declaration: class_decl | interface_decl;
class_decl: ^(CLASS ^(ID CLASS_IDENTIFIER))
-> class_decl_tmpl(cid={$CLASS_IDENTIFIER.text});
它的组模板文件如下所示:
group My;
program_decl_tmpl() ::= <<
*WHAT?*
>>
class_decl_tmpl(cid) ::= <<
public class <cid> {}
>>
基于此,我有以下问题:
WHAT?
中表达的程序只是一个类声明列表以获得最终生成的输出之外,一切正常吗? 解决方案,我根据Terence提出的建议添加解决方案:
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> {}
>>
答案 0 :(得分:4)
尝试
start: ^(PROGRAM d+=declaration+) -> program_decl_tmpl(decls=$d)
WHAT? = <decls>