我有一个用 antlr2 语法编写的语法文件,需要帮助理解如何用 antlr4 语法重写一些解析器规则。我知道 antlr4 消除了构建 AST 的需要,所以我不确定如何处理AST 动作翻译的规则。ANTLR Tree Construction解释了一些语法以及如何使用 # 构造,但我仍然不确定如何阅读这些规则并重新编写它们。
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);
};