0
我有一个用antlr2语法编写的语法文件,需要帮助了解如何重写antlr4语法中的一些解析器规则。我知道antlr4消除了构建AST的必要性,所以我不确定如何处理AST动作翻译。 ANTLR Tree Construction解释了一些语法和如何使用#构造,但我仍然不确定如何阅读这些规则并重新编写它们。重写AST动作翻译为ANTLR4
temp_root :
temp { #temp_root = #([ROOT, "root"], #temp_root); } EOF;
temp :
c:temp_content
{ #temp = #(#([FUNCTION_CALL, "template"], #template), c);
reparent((MyAST)#temp, MyAST)#c); };
temp_content :
(foo | bar);
foo :
{
StringBuilder result = new StringBuilder("");
}
: (c:FOO! { result.append(c.getText()); })+
{ #foo = #([TEMPLATE_STRING_LITERAL, result.toString()], #foo); };
bar :
BEGIN_BAR! expr END_BAR!
exception
catch [Exception x] {
bar_AST = handleException(x);
};
每个“#”表示节点中应该有一个“子树”吗? – Raven