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?
5 Answers
# 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`

- 472
- 4
- 10
-
Yes, you can map between different syntax at least for simple grammars. I was looking for a tool that does this conversion (BNF, ISO EBNF, ABNF, ANTLR...). Sure it is possible to create your own tool ;-) – Jakob Apr 24 '12 at 19:07
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.

- 166,582
- 36
- 299
- 288
-
Thanks, ANTLR grammar written in ANTLR was one thing I was looking for. As far as I understand ANTLR you *can* write down left-recursive grammars, but you must then refactor them by hand. A converter from other BNF-forms and ANTLR would at least reduce the need to manually adopting the differences in punctation and other minor aspects of BNF-syntax variants. – Jakob Feb 02 '11 at 10:18
-
Looks like ANTLR cannot cope with Unicode characters above Basic Multilingual Plane: ESC: '\\' ( ... | 'u' XDIGIT XDIGIT XDIGIT XDIGIT ) cannot match codepoints above \uFFFF. – Jakob Feb 02 '11 at 10:23
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.

- 30,230
- 67
- 195
- 328
There's a site that host a huge variety of grammars and tools to convert between their formats:

- 93,541
- 22
- 172
- 341
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

- 1,159
- 9
- 19