Lovely SVG railroad diagrams revisited

24 Jun 2009

Last week I blogged about Julian Hyde's Clapham utility which generates nice SVG graphs from JavaCC grammars. One painful bit was that JavaCC didn't output a clean BNF format of a grammar, so you had to do some copying and pasting and general munging to get a grammar in shape for Clapham to use it.

Well, no more. I've added a BNF option to JJDoc (here's an updated javacc.jar) so that it outputs plain old BNF. So now you can easily go from a JavaCC grammar to a Clapham-generated graph:

$ /Users/tom/java.net/javacc/bin/jjdoc -bnf \
-output_file=java.bnf \
/Users/tom/java.net/javacc/examples/JavaGrammars/1.5/Java1.5.jj
Java Compiler Compiler Version 4.3 (Documentation Generator Version 0.1.4)
(type "jjdoc" with no arguments for help)
Reading from file /Users/tom/java.net/javacc/examples/JavaGrammars/1.5/Java1.5.jj . . .
Grammar documentation generated successfully in java.bnf
$ $ java -cp lib/batik-awt-util.jar:lib/batik-bridge.jar:lib/batik-css.jar\
:lib/batik-dom.jar:lib/batik-ext.jar:lib/batik-gvt.jar:lib/batik-parser.jar\
:lib/batik-script.jar:lib/batik-svg-dom.jar:lib/batik-svggen.jar\
:lib/batik-transcoder.jar:lib/batik-util.jar:lib/batik-xml.jar\
:lib/clapham.jar:lib/javacc.jar:lib/junit.jar:lib/xercesImpl.jar net.hydromatic.clapham.Clapham \
-d grammar java.bnf 
Created output directory grammar
Symbol AdditiveExpression 
[... etc ...]

Here's the svg version of one of the nonterminals in the output (using an embed tag):

And the png version:

Fun stuff!