将 EBNF 转换为 BNF 以用于 LALR 解析器

发布于 2024-12-31 22:52:30 字数 539 浏览 2 评论 0原文

我知道有几个帖子的标题相似。大多数链接到一个死网站 - 无论如何我有一个更具体的问题。

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2025-01-07 22:52:30

很好......

您可以将递归放在右侧,它应该可以工作,但是您将使解析器“做更多工作”,因为它必须跟踪递归的主干,并且要做到这一点,它必须使用更多堆栈位置。

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.

少跟Wǒ拽 2025-01-07 22:52:30

您的翻译在规则 Expr 上保持递归,并且它接受其他 Expr 产生式。

正确的翻译是:

Expr::=
    ExprSingle Expr1
Expr1::=
    Expr1 "," ExprSingle | ε

Your translation is left recursive on the rule Expr and it accepts other Expr productions.

The correct translation would be:

Expr::=
    ExprSingle Expr1
Expr1::=
    Expr1 "," ExprSingle | ε
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文