ANTLRWorks:无法让操作员工作

发布于 2024-11-19 12:45:16 字数 419 浏览 4 评论 0原文

我已经尝试学习 ANTLR 一段时间了,终于得到了权威的 ANTLR 参考资料。 好吧,我在 ANTLRWorks 1.4 中尝试了以下内容

grammar Test;

INT :   '0'..'9'+
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

expression
    :   INT ('+'^ INT)*;

当我传递 2+4 并处理表达式时,我没有得到以 + 作为根、以 2 和 4 作为子节点的树。相反,我将表达式作为根,将 2、+ 和 4 作为同一级别的子节点。

无法弄清楚我做错了什么。迫切需要帮助。

顺便说一句,我怎样才能获得这些图形描述?

I've been trying to learn ANTLR for some time and finally got my hands on The Definitive ANTLR reference.
Well I tried the following in ANTLRWorks 1.4

grammar Test;

INT :   '0'..'9'+
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

expression
    :   INT ('+'^ INT)*;

When I pass 2+4 and process expression, I don't get a tree with + as the root and 2 and 4 as the child nodes. Rather, I get expression as the root and 2, + and 4 as child nodes at the same level.

Can't figure out what I am doing wrong. Need help desparately.

BTW how can I get those graphic descriptions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-11-26 12:45:16

是的,您得到了表达式,因为它是您唯一的规则表达式 返回的表达式。

我刚刚在您的示例中添加了一个虚拟令牌 PLUS 以及一个显示您期望的结果的重写表达式。
但看来你已经找到了解决方案:o)

grammar Test;

options {
    output=AST;
    ASTLabelType = CommonTree;
}
tokens {PLUS;}

@members {
   public static void main(String [] args) {
          try {
           TestLexer lexer =
               new TestLexer(new ANTLRStringStream("2+2"));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            TestParser parser = new TestParser(tokens);
            TestParser.expression_return p_result = parser.expression();

            CommonTree ast = p_result.tree;
            if( ast == null ) {
               System.out.println("resultant tree: is NULL");
            } else {
               System.out.println("resultant tree: " + ast.toStringTree());
            }
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
}

expression
    :   INT ('+' INT)* -> ^(PLUS INT+);

INT :   '0'..'9'+
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

Yes, you get the expression because it's an expression that your only rule expression is returning.

I have just added a virtual token PLUS to your example along with a rewrite expression that show the result your are expecting.
But it seems that you have already found the solution :o)

grammar Test;

options {
    output=AST;
    ASTLabelType = CommonTree;
}
tokens {PLUS;}

@members {
   public static void main(String [] args) {
          try {
           TestLexer lexer =
               new TestLexer(new ANTLRStringStream("2+2"));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            TestParser parser = new TestParser(tokens);
            TestParser.expression_return p_result = parser.expression();

            CommonTree ast = p_result.tree;
            if( ast == null ) {
               System.out.println("resultant tree: is NULL");
            } else {
               System.out.println("resultant tree: " + ast.toStringTree());
            }
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
}

expression
    :   INT ('+' INT)* -> ^(PLUS INT+);

INT :   '0'..'9'+
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文