ANTLR:如何生成一棵有 2 个以上子节点的树?
给定语法
parse : expr EOF -> ^(ROOT expr);
expr : atom ('|'^ atom)*;
atom : LITERAL | ('('! expr ')'!);
LITERAL : 'a'..'z';
WS : (' '|'\t'|'\r'|'\n'){Skip();};
和输入,
a|b|c
我得到一棵看起来像
http://graph.gafol 的 树.net/pic/dsqoQhzgs.png
而我想要一棵看起来像
http://graph.gafol.net/pic/dsrGWVUfz.png
我如何在语法中表达这一点?
Given the grammar,
parse : expr EOF -> ^(ROOT expr);
expr : atom ('|'^ atom)*;
atom : LITERAL | ('('! expr ')'!);
LITERAL : 'a'..'z';
WS : (' '|'\t'|'\r'|'\n'){Skip();};
And input,
a|b|c
I get a tree that looks like,
http://graph.gafol.net/pic/dsqoQhzgs.png
Whereas I'd like a tree that looks like,
http://graph.gafol.net/pic/dsrGWVUfz.png
How would I express that in the grammar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点棘手。您可以通过使用语法谓词
(LOOK-AHEAD -TOKENS-HERE)=>
在匹配“OR-链”之前:正确处理
a|b|c
、a|b
和一个。
但您可能想解释一下您实际上要解析的语言:可能有更好(更优雅?)的方式来表达它。
为什么您不想像第一个图中那样拥有 AST?当根(操作数)只有两个子项时,计算表达式很容易,对吧?
It's a bit tricky. You could do it by using a syntactic predicate
(LOOK-AHEAD-TOKENS-HERE)=>
before matching an "OR-chain":which properly handles
a|b|c
,a|b
anda
.But you might want to explain what language you're actually trying to parse: there might be better (more elegant?) ways to express it.
Why wouldn't you want to have an AST as in your first diagram? Evaluating expressions is easy when the root (operand) has only two children, right?
我认为这可以通过添加重写规则来实现,但我现在没有 antlrworks,所以我不能确定。但它已经很接近了,所以请尝试一下,并在必要时修改重写语法。
I think this will do it by adding a rewrite rule but I don't have antlrworks right now so I can't be sure. But it's close so give it a shot and modify the rewrite syntax if necessary.