ANTLR Decision 可以使用多种替代方案来匹配输入
我有这个简单的语法:
expr: factor;
factor: atom (('*' ^ | '/'^) atom)*;
atom: INT
| ':' expr;
INT: ('0'..'9')+
当我运行它时,它说:
决策可以使用多个替代项匹配输入,例如“*”1,2
决策可以使用多个替代项匹配输入,例如“/”1,2
我无法发现歧义。红色箭头如何指向? 任何帮助将不胜感激。
I have this simple grammer:
expr: factor;
factor: atom (('*' ^ | '/'^) atom)*;
atom: INT
| ':' expr;
INT: ('0'..'9')+
when I run it it says :
Decision can match input such as '*' using multiple alternatives 1,2
Decision can match input such as '/' using multiple alternatives 1,2
I can't spot the ambiguity. How are the red arrows pointing ?
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您想要解析输入:
由语法生成的解析器可以将此输入匹配到以下解析树中:
和:
(我省略了冒号以使树木更加清晰)
请注意,您看到的只是一个 警告。通过明确指示 ANTLR
(('*' | '/')atom)*
需要贪婪地匹配,如下所示:解析器“知道”采用哪种替代方案,并且不会发出警告。
编辑
我使用 ANTLR 3.3 测试了语法,如下所示:
然后从命令行:
它不会产生任何警告(或错误)。
Let's say you want to parse the input:
The parser generated by your grammar could match this input into the following parse trees:
and:
(I omitted the colons to keep the trees more clear)
Note that what you see is just a warning. By specifically instructing ANTLR that
(('*' | '/') atom)*
needs to be matched greedily, like this:the parser "knows" which alternative to take, and no warning is emitted.
EDIT
I tested the grammar with ANTLR 3.3 as follows:
And then from the command line:
which does not produce any warning (or error).