ANTLRWorks:无法让操作员工作
我已经尝试学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您得到了表达式,因为它是您唯一的规则
表达式
返回的表达式。我刚刚在您的示例中添加了一个虚拟令牌
PLUS
以及一个显示您期望的结果的重写表达式。但看来你已经找到了解决方案:o)
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)