我想从我的解析器中获取所有令牌,然后我想过滤输出,得到一个AST列表(myAST):
ANTLRStringStream stream = new ANTLRStringStream("P + 1 + F(A + 3)");
MyLexer lexer = new MyLexer (stream);
MyParser parser = new MyParser(new CommonTokenStream(lexer));
LanguajeTreeAdaptor treeAdaptor = new LanguajeTreeAdaptor();
parser.setTreeAdaptor(treeAdaptor);
我有一个TreeAdaptor用于我的语言:
public class LanguajeTreeAdaptor extends CommonTreeAdaptor{
public LanguajeTreeAdaptor(){
super();
}
@Override
public Object create(Token payload) {
if(payload == null)
return super.create(payload);
switch(payload.getType()){
case EtesGrammarParser.ID:
return new IdAST(payload);
..........
}
return super.create(payload);
}
以下是我语法的一些规则:
program
: expression EOF!
;
expression
: exprFinal
;
exprFinal: exprFuncCall | ID | INT | DOUBLE | STRING | exprParenthesis;
exprFuncCall : ID LPARENT exprList RPARENT -> ^(FUNC_CALL ID exprList);
所以P,F和A在我的规则中是Id,我正在尝试使用此代码:
TokenStream input = parser.getTokenStream();
TokenSource tSource = input.getTokenSource();
Token currentToken = tSource.nextToken();
while(currentToken != null){
System.out.println(currentToken.getText());
currentToken = tSource.nextToken();
}
但我想只处理IdAST(我的想法),
while(currentToken != null){
if(currentToken instanceof IdAST){
//call to another method to process the Id
}
System.out.println(currentToken.getText());
currentToken = tSource.nextToken();
}
我不能这样做因为currentToken是CommonToken,我怎么才能得到那些Id?我正在使用antlr 3 Regards Zinov