'Are there tools to convert between ANTLR and other forms of BNF? [closed]
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?
Solution 1:[1]
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.
Solution 2:[2]
# 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`
Solution 3:[3]
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.
Solution 4:[4]
There's a site that host a huge variety of grammars and tools to convert between their formats:
Solution 5:[5]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Travis |
Solution 3 | |
Solution 4 | Ira Baxter |
Solution 5 |