如何从antlr中的解析器获取所有标记
我想从我的解析器中获取所有标记,然后我想过滤输出,获取 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 问候 Zinov
I want to get all tokens from my parser and then I want to filter the output, getting a List of 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);
I have a TreeAdaptor for my languaje:
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);
}
Here are some rules of my grammar:
program
: expression EOF!
;
expression
: exprFinal
;
exprFinal: exprFuncCall | ID | INT | DOUBLE | STRING | exprParenthesis;
exprFuncCall : ID LPARENT exprList RPARENT -> ^(FUNC_CALL ID exprList);
So P, F, and A are Id in my rules, I am trying with this code:
TokenStream input = parser.getTokenStream();
TokenSource tSource = input.getTokenSource();
Token currentToken = tSource.nextToken();
while(currentToken != null){
System.out.println(currentToken.getText());
currentToken = tSource.nextToken();
}
but I want to process only IdAST(my idea),
while(currentToken != null){
if(currentToken instanceof IdAST){
//call to another method to process the Id
}
System.out.println(currentToken.getText());
currentToken = tSource.nextToken();
}
I can't do this because currentToken is CommonToken, how can I get those Id only?. I am working with antlr 3 Regards Zinov
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论