[ACCEPTED]-Are there tools to convert between ANTLR and other forms of BNF?-bnf

Accepted answer
Score: 12
# 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`

0

Score: 5

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

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

Score: 4

Jakob wrote:

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

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

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


Jakob wrote:

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

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

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

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

Score: 2

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

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

Score: 1

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

More Related questions