Are there any tools to convert ANTLR grammar syntax to and from other BNF syntaxes? There are several forms Backus-Naur Form (BNF, EBNF, ABNF, W3C-BNF, XBNF...) with specification, e.g. see this list. The ANTLR grammar syntax only seems to be described by examples. I know that ANTLR grammar files contain more than the specification of a context-free syntax, but you should be able to convert at least the common subset - has anyone done yet automatically?
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.
# 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`
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.
There's a site that host a huge variety of grammars and tools to convert between their formats:
来源:https://stackoverflow.com/questions/4868884/are-there-tools-to-convert-between-antlr-and-other-forms-of-bnf