如何从antlr中的解析器获取所有标记

发布于 2025-01-02 04:56:17 字数 1841 浏览 1 评论 0原文

我想从我的解析器中获取所有标记,然后我想过滤输出,获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文