Is it possible to export ANTLR4 railroad diagrams as png? If so, how?

2k Views Asked by At

I want to export ANTLR4 rules in the railroad format.

Please notice that this format is not the same as generated by command line option "-atn".

The railroad format is generated by antlrworks, but I'd like to generate this programatically.

I am looking for something like http://clapham.sourceforge.net/

clapham generates what I want from BNF. Is there any tool to convert from antlr4 to BNF? (question probably related to Generate BNF diagrams from an antlr grammar?, but notice I am not asking the same thing here. Converting to BNF is just a workaround, what I want is to generate the railroad diagram directly from ANTLR grammar)

Following the answer for Generate BNF diagrams from an antlr grammar? worked for me

Just get your ANTLR grammar, send to https://www.bottlecaps.de/convert/ and then to https://www.bottlecaps.de/rr/ui

ANTLR4 expressions such as "WS : [ \t\r\n]+ -> skip;" could not be processed so I've changed to the traditional form "WS ::= ( #x9 | #xD | #xA )*"

Clapham could not work with the BNF grammar generated by https://www.bottlecaps.de/convert/ unfortunately.

This is a way to get the diagram, but this is not programatically. I wish I had some JAR I could just add to generate on the fly.

2

There are 2 best solutions below

3
On

Checkout ANTLR 4 IDE: https://github.com/jknack/antlr4ide

Here is a Java.g4 to HTML example: http://jknack.github.io/antlr4ide/Java/Javav4.g4.html

You will find images under the http://jknack.github.io/antlr4ide/Java/images directory.

The last update to this tool was in December 2016.

0
On

Here's a library [I wrote] that you can execute from the command line, or embed into your own code, to generate PNG or SVG railroad diagrams from ANTLR 4 grammar rules:

https://github.com/bkiers/rrd-antlr4