如何在ANTLR中进行更复杂的树转换?

时间:2012-01-04 23:22:10

标签: antlr

我知道我想制作的AST(我认为),即:

 ^( STATEMENT alias ( FOREACH rel ( FOREACH_PLAN_SIMPLE ( GENERATE STAR ) ) ) )

所以我添加了以下规则:

realias_statement : alias EQUAL rel SEMI_COLON
                 -> ^( STATEMENT alias ( FOREACH rel ( FOREACH_PLAN_SIMPLE ( GENERATE STAR ) ) ) )
;

但是,当我尝试编译时,我收到以下错误......

 [java] error(100): src//org/apache/pig/parser/QueryParser.g:174:49: syntax error: antlr: MismatchedTokenException(73!=69)
 [java] error(100): src//org/apache/pig/parser/QueryParser.g:174:51: syntax error: antlr: NoViableAltException(75@[])
 [java] error(100): src//org/apache/pig/parser/QueryParser.g:174:95: syntax error: antlr: NoViableAltException(69@[])
 [java] error(100): src//org/apache/pig/parser/QueryParser.g:174:97: syntax error: antlr: NoViableAltException(69@[])
 [java] error(100): src//org/apache/pig/parser/QueryParser.g:174:99: syntax error: antlr: NoViableAltException(69@[])
 [java] error(100): src//org/apache/pig/parser/QueryParser.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(0!=18)
 [java] error(10):  internal error: src//org/apache/pig/parser/QueryParser.g : java.lang.NullPointerException
 [java] org.antlr.grammar.v3.DefineGrammarItemsWalker.countAltsForRule(DefineGrammarItemsWalker.java:153)
 [java] org.antlr.grammar.v3.DefineGrammarItemsWalker.rule(DefineGrammarItemsWalker.java:1546)
 [java] org.antlr.grammar.v3.DefineGrammarItemsWalker.rules(DefineGrammarItemsWalker.java:1190)
 [java] org.antlr.grammar.v3.DefineGrammarItemsWalker.grammarSpec(DefineGrammarItemsWalker.java:623)
 [java] org.antlr.grammar.v3.DefineGrammarItemsWalker.grammar_(DefineGrammarItemsWalker.java:275)
 [java] org.antlr.tool.Grammar.defineGrammarSymbols(Grammar.java:748)
 [java] org.antlr.tool.CompositeGrammar.defineGrammarSymbols(CompositeGrammar.java:369)
 [java] org.antlr.Tool.process(Tool.java:465)
 [java] org.antlr.Tool.main(Tool.java:93)

我当然会继续深入研究这些错误的含义,但我想知道更多一般是否可以做到这一点? IE我确切地知道我希望它看起来如何,但我不确定如何让它看起来像那样。

由于

1 个答案:

答案 0 :(得分:2)

假设在STATEMENT部分中定义FOREACHFOREACH_PLAN_SIMPLEGENERATE STARtokens { ... },这应该有效:

realias_statement
 : alias EQUAL rel SEMI_COLON
   -> ^(STATEMENT alias ^(FOREACH rel ^(FOREACH_PLAN_SIMPLE ^(GENERATE STAR))))
 ;

请注意左括号前的^