将 EBNF 转换为 BNF 以用于 LALR 解析器
我知道有几个帖子的标题相似。大多数链接到一个死网站 - 无论如何我有一个更具体的问题。
我正在尝试将 XPath 规范中的 EBNF 转换为直接 BNF我可以轻松创建一个与 Bison 兼容的语法文件。
我已经有一段时间没有这样做了,我不记得递归属于产生式的哪一边。我以为是左边 - 但当通过 Bison 生成的解析器运行时,我的“直接”翻译给我带来了简单的 XPath 表达式的语法错误。
因此,如果有人能迁就我并参与进来——那么我就不会在追鬼了:
在下面的 Expr
规则中:
Expr::=
ExprSingle ("," ExprSingle)*
这是正确的翻译吗? (将递归放在左边):
Expr::=
Expr "," ExprSingle
| ExprSingle
I know there are several posts with a similar title. Most link to a dead site - and I have a more specific question anyways.
I'm trying to convert the EBNF in the XPath spec to straight BNF so that I can easily create a Bison-compatiable grammar file.
It's been a while since I've done this, and I don't remember which side of the production a recursion belongs. I thought it was the left - but my "straight-forward" translation is giving me syntax errors with plain-jane XPath expressions when they are run through the Bison-generated parser.
So if someone could humor me and weigh in - so I'm not chasing a ghost:
In the Expr
rule below:
Expr::=
ExprSingle ("," ExprSingle)*
Is this the correct translation? (putting the recursion on the left):
Expr::=
Expr "," ExprSingle
| ExprSingle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很好......
您可以将递归放在右侧,它应该可以工作,但是您将使解析器“做更多工作”,因为它必须跟踪递归的主干,并且要做到这一点,它必须使用更多堆栈位置。
That's fine....
You could put the recursion on the right and it should work, but you'll make the parser "do more work" as it has to track the spine of the recursion, and to do so it has to use more stack locations.
您的翻译在规则
Expr
上保持递归,并且它接受其他Expr
产生式。正确的翻译是:
Your translation is left recursive on the rule
Expr
and it accepts otherExpr
productions.The correct translation would be: