是否有工具可以在 ANTLR 和其他形式的 BNF 之间进行转换?

发布于 2024-10-15 08:37:24 字数 1807 浏览 8 评论 0原文

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

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

发布评论

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

评论(5

很酷不放纵 2024-10-22 08:37:24
# Grammar Syntax

|                               | BNF                           | ISO EBNF                      | ABNF                          | ANTLR                         |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|
| rule definition               | `<name> ::= ...`              | `name = ... ;`                | `name = ...`                  | `name : ... ;`                |
| terminal items                | `...`                         | `'...'` or `"..."`            | integer or `"..."`            | `'...'`                       |
| non-terminal items            | `<...>`                       | `...`                         | `...` or `<...>`              | `...`                         |
| concatenation                 | (space)                       | `,`                           | (space)                       | (space)                       |
| choice                        | `|`                           | `|`                           | `/`                           | `|`                           |
| optional                      | requires choice syntax[^1]    | `[...]`                       | `*1...` or `[...]`            | `...?`                        |
| 0 or more repititions         | requires choice syntax[^2]    | `{...}`                       | `*...`                        | `...*`                        |
| 1 or more repititions         | requires choice syntax[^3]    | `{...}-`                      | `1*...`                       | `...+`                        |
| n repititions                 |                               | `n*...`                       | `n*n...`                      |                               |
| n to m repititions            |                               |                               | `n*m...`                      |                               |
| grouping                      |                               | `(...)`                       | `(...)`                       | `(...)`                       |
| comment                       |                               | `(*...*)`                     | `;...`                        | `// ...` or `/* ... */`       |


[^1]: `optionalb ::= a b c d | a c d`

[^2]: `list ::= | listitem list`

[^3]: `list ::= listitem | listitem list`
# Grammar Syntax

|                               | BNF                           | ISO EBNF                      | ABNF                          | ANTLR                         |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|
| rule definition               | `<name> ::= ...`              | `name = ... ;`                | `name = ...`                  | `name : ... ;`                |
| terminal items                | `...`                         | `'...'` or `"..."`            | integer or `"..."`            | `'...'`                       |
| non-terminal items            | `<...>`                       | `...`                         | `...` or `<...>`              | `...`                         |
| concatenation                 | (space)                       | `,`                           | (space)                       | (space)                       |
| choice                        | `|`                           | `|`                           | `/`                           | `|`                           |
| optional                      | requires choice syntax[^1]    | `[...]`                       | `*1...` or `[...]`            | `...?`                        |
| 0 or more repititions         | requires choice syntax[^2]    | `{...}`                       | `*...`                        | `...*`                        |
| 1 or more repititions         | requires choice syntax[^3]    | `{...}-`                      | `1*...`                       | `...+`                        |
| n repititions                 |                               | `n*...`                       | `n*n...`                      |                               |
| n to m repititions            |                               |                               | `n*m...`                      |                               |
| grouping                      |                               | `(...)`                       | `(...)`                       | `(...)`                       |
| comment                       |                               | `(*...*)`                     | `;...`                        | `// ...` or `/* ... */`       |


[^1]: `optionalb ::= a b c d | a c d`

[^2]: `list ::= | listitem list`

[^3]: `list ::= listitem | listitem list`
风启觞 2024-10-22 08:37:24

我为此目的写了一个翻译器。 Universal-transpiler 能够转换 ANTLR 语法进入 PEG.js, ABNFXBNF 以及其他一些语法符号。目前还无法将 ANTLR 转换为 W3C-BNF,但我会尝试在未来版本中添加此功能。

该翻译器仅与 ANTLR 语言的一小部分兼容,但我希望它仍然有用。

I wrote a translator for this purpose. Universal-transpiler is able to convert ANTLR grammars into PEG.js, nearley, ABNF, XBNF, and several other grammar notations. It is not yet able to translate ANTLR into W3C-BNF, but I will try to add this feature in a future version.

This translator is only compatible with a small subset of the ANTLR language, but I hope it will still be useful.

那一片橙海, 2024-10-22 08:37:24

雅各布写道:

ANTLR语法似乎只是通过示例来描述。

ANTLR (v3) 是用以下语法“用自己的话”编写的(正如特伦斯·帕尔本人所说):

http://www.antlr.org/grammar/ANTLR/ANTLRv3.g

雅各布写道:

但是你应该至少能够转换公共子集 - 有人已经自动完成了吗?

据我所知没有。如果它确实存在,我也从未在我定期阅读的 ANTLR 邮件列表上看到过讨论这个工具。

另请注意,许多 BNF 变体允许 左递归 规则,这是 LL 解析器生成器像ANTLR无法应对。左递归规则当然可以由该工具重新分解,但这可能相当棘手,并且可能会导致比手动执行此操作所得到的语法“可读”得多。

至于将 ANTLR 语法转换为类似 BNF 的形式,我想会更容易,尽管只是使用最简单的语法。一旦将各种类型的谓词放入 ANTLR 语法中,转换可能会再次变得棘手。

Jakob wrote:

The ANTLR grammar syntax only seems to be described by examples.

ANTLR (v3) is written "in its own words" (as Terence Parr himself put it) in this grammar:

http://www.antlr.org/grammar/ANTLR/ANTLRv3.g

Jakob wrote:

but you should be able to convert at least the common subset - has anyone done yet automatically?

Not that I know of. And if it does exist, I've never seen this tool being discussed on the ANTLR mailing list that I read on a regular basis.

Also note that many BNF-variants allow for left-recursive rules, something that an LL-parser generator like ANTLR cannot cope with. The left recursive rules can of course be re-factored out by the tool, but that could be rather tricky, and will probably result in a far less "readable" grammar than one would get than doing this manually.

As to converting ANTLR grammars into BNF-like form would be easier I guess, although only with the most trivial grammars. As soon as various types of predicates are put into an ANTLR grammar, the conversion might again become tricky.

那一片橙海, 2024-10-22 08:37:24

有一个网站提供了大量的语法和工具来在其格式之间进行转换:

http:// slebok.github.io/zoo/index.html

There's a site that host a huge variety of grammars and tools to convert between their formats:

http://slebok.github.io/zoo/index.html

爱殇璃 2024-10-22 08:37:24

Antlr4 确实允许左递归,具有显着的灵活性。我找到了一个现代工具,可以在 Antlr 语法与其他类型的语法之间进行转换。目前这得到了很好的支持:
trconvert

Antlr4 does allow left recursion, with remarkable flexibility. I found a modern tool to convert to and from Antlr grammars to other types of grammars. This is well supported at this time:
trconvert

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