将 EBNF 语法转换为上下文无关语法

发布于 2024-10-22 22:28:46 字数 141 浏览 1 评论 0原文

我必须编写一个 JavaCUP 规范,并且我得到了 EBNF 语法。但是,我不知道如何在两者之间进行转换。我听说过基本的想法,但我真的不明白我需要改变什么,“终端”是什么,等等。

任何人都可以解释如何从一种转换为另一种,或者是否有地方我可以阅读关于它?

I have to write a JavaCUP specification, and I've been given an EBNF grammar. However, I don't know how to convert between the two. I've heard the basic ideas, but I don't really understand what I need to change, what would be the "terminals", etc.

Can anyone explain how to convert from one to another, or if there's somewhere where I can read about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦开始←不甜 2024-10-29 22:28:46

EBNF 语法与普通 BNF 类似,但具有一些额外的功能(类似于正则表达式运算符)作为语法糖。由于您没有显示语法,我只能猜测您需要脱糖以转换为正常 BNF 的哪些部分,但以下是最常见的部分(对于像 JavaCUP 这样的 LALR 生成器):

B*    becomes Bstar, defined as Bstar ::= epsilon; Bstar ::= Bstar B
B+    becomes Bplus, defined as Bplus ::= B; Bplus ::= Bplus B
B?    becomes Bquestion, defined as Bquestion ::= epsilon; Bquestion ::= B
B | C becomes BorC, defined as BorC ::= B; BorC ::= C

epsilon 标识符然而,您的解析器生成器表示空字符串。

EBNF grammars are similar to normal BNF, but with some extra features (similar to regular expression operators) as syntax sugar. Since you did not show your grammar, I can only guess at what parts you need to desugar to convert to normal BNF, but here are the most common (for a LALR generator like JavaCUP):

B*    becomes Bstar, defined as Bstar ::= epsilon; Bstar ::= Bstar B
B+    becomes Bplus, defined as Bplus ::= B; Bplus ::= Bplus B
B?    becomes Bquestion, defined as Bquestion ::= epsilon; Bquestion ::= B
B | C becomes BorC, defined as BorC ::= B; BorC ::= C

The epsilon identifier here is however your parser generator denotes the empty string.

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