非空闭包和问号:只有第一个元素被放入 AST 中?
我被一个奇怪的现象所困扰:
只有 x
中的第一个 in z: x | '<'!是吗? '>'
,其中 y: x (','!x)*
出现在生成的 AST 中。但前提是我使用 Maven 存储库中部署的 Antlr3 编译代码。通过 AntlrWorks,我看到了正确的树。
是b: aa*; c:数据库? d;
语义错误?我做错了什么?或者只是 Antlr 中有一个错误?
如果您需要更完整的示例(我的问题出现在 try_ 的 $v2 中):(
variables
: annotatedVariable
-> ^(VARIABLES annotatedVariable)
| v='<' annotatedVariableList? '>'
-> ^(VARIABLES[$v] annotatedVariableList?)
;
annotatedVariableList
: annotatedVariable (','! annotatedVariable)*
;
try_
: v='try' e1=expression 'of' v1=variables '->' e2=expression
'catch' v2=variables '->' e3=expression
-> ^(TRY[$v] $e1 $v1 $e2 $v2 $e3)
;
完整源代码和示例输入文件: https://gist.github.com/1004329。树中只存在catch中的T,但我在AntlrWorks中看到了T和R。)
I'm haunted by a strange phenomenon:
Only the first in x
in z: x | '<'! y? '>'
, where y: x (','! x)*
, occurs in the resulting AST. But only if I compile the code using Antlr3 as deployed in the maven repositories. With AntlrWorks I see a correct Tree.
Is b: a a*; c: d b? d;
semantically wrong? What am I doing wrong? Or is there simply an error in Antlr?
If you need a more complete example (my problem occurs in the $v2 of try_):
variables
: annotatedVariable
-> ^(VARIABLES annotatedVariable)
| v='<' annotatedVariableList? '>'
-> ^(VARIABLES[$v] annotatedVariableList?)
;
annotatedVariableList
: annotatedVariable (','! annotatedVariable)*
;
try_
: v='try' e1=expression 'of' v1=variables '->' e2=expression
'catch' v2=variables '->' e3=expression
-> ^(TRY[$v] $e1 $v1 $e2 $v2 $e3)
;
(Complete source and an example input file: https://gist.github.com/1004329. Only the T of in catch is present in the tree, but I see both T and R in AntlrWorks.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ANTLRWorks 生成解析树,不是您的解析器规则生成的 AST(假设您正在谈论 ANTLRWorks 的解释器)。
一个简单的测试显示一切正常(使用 ANTLR v3 测试):
将解析输入:
为以下 AST:
你可以自己测试一下:
不。但是这个问题与您的
z: x | 有关吗? '<'!是吗? '>'
规则?抱歉,你说得有点含糊。我不知道。您必须在此处发布一个简短的、独立的示例,就像我发布的那样。抱歉,我不热衷于跟踪外部链接并费力地浏览数百行代码。
ANTLRWorks produces a parse tree, not the AST your parser rules produce (assuming you're talking about ANTLRWorks' interpreter).
A simple test shows everything works just fine (tested with ANTLR v3):
will parse the input:
into the following AST:
as you can test yourself:
No. But is this question related to your
z: x | '<'! y? '>'
rule? Sorry to say, but you're a bit vague.I don't know. You'll have to post a short, self contained example here on SO, just like I posted. I'm not keen on following an external link and wading through hundreds of lines of code, sorry.