BNF 语法歧义
我最近在想下面的 BNF,
A -> x | yA | yAzA
where x,y,z are terminals.
我很确定这个语法是有歧义的,但是如何使它明确呢?
I was recently thinking of the following BNF
A -> x | yA | yAzA
where x,y,z are terminals.
I'm pretty sure this grammar is ambiguous, but how would one make it unambiguous?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果特定字符串可以有多个解析树,则语法是不明确的。在您的语言中,字符串 yyxzx 可以具有这两个解析树中的任何一个:
因此语法是不明确的。
这实际上相当于类 C 语言中臭名昭著的“if/then/else”歧义,其中
y=if
、z=else
和x=声明
。 http://en.wikipedia.org/wiki/Dangling_else。我建议您查看该页面以获取有关如何解决此问题的想法。A grammar is ambiguous if a particular string can have more than one parse tree. In your language the string
yyxzx
can have either of these two parse trees:Therefore the grammar is ambiguous.
This actually is equivalent to the notorious "if/then/else" ambiguity in C-like languages, where
y=if
,z=else
, andx=statement
. http://en.wikipedia.org/wiki/Dangling_else. I would recommend checking out that page for ideas on how to get around this problem.