ANTLR: Convert ANTLR to BNF - go-sqlparser/current GitHub Wiki
| 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
Apr 24 '12
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.
Jan 3 '19
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.
Feb 2 '11