對於question和@BartKiers建議的語法(謝謝!),我增加了options
塊指定輸出爲生成AST
options{
language=Java;
output=AST;
ASTLabelType=CommonTree;
}
但是,我不能圖瞭解如何訪問輸出即AST
。我需要遍歷樹並處理輸入中指定的每個操作。
使用您的示例here,我試圖實現返回值的規則。但是,我遇到了以下錯誤:
relational returns [String val]
: STRINGVALUE ((operator)^ term)?
{val = $STRINGVALUE.text + $operator.text + $term.text; }
;
term returns [String rhsOperand]
: QUOTEDSTRINGVALUE {rhsOperand = $QUOTEDSTRINGVALUE.text;}
| NUMBERVALUE {rhsOperand = $NUMBERVALUE.text; }
| '(' condition ')'
;
編譯錯誤:
Checking Grammar RuleGrammarParser.g...
\output\RuleGrammarParser.java:495: cannot find symbol
symbol : variable val
location: class RuleGrammarParser
val = (STRINGVALUE7!=null?STRINGVALUE7.getText():null) + (operator8!=null?input.toString(operator8.start,operator8.stop):null) + (term9!=null?input.toString(term9.start,term9.stop):null);
^
\output\RuleGrammarParser.java:612: cannot find symbol
symbol : variable rhsOperand
location: class RuleGrammarParser
rhsOperand = (QUOTEDSTRINGVALUE10!=null?QUOTEDSTRINGVALUE10.getText():null);
^
\output\RuleGrammarParser.java:632: cannot find symbol
symbol : variable rhsOperand
location: class RuleGrammarParser
rhsOperand = (NUMBERVALUE11!=null?NUMBERVALUE11.getText():null);
^
3 errors
能否請你幫我明白爲什麼這個不能編譯器?
添加了引擎收錄:http://pastebin.com/u1Bv3L0A
不客氣的黑暗。 – 2012-03-02 17:03:17