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/ -bnf \
-output_file=java.bnf \
Java Compiler Compiler Version 4.3 (Documentation Generator Version 0.1.4)
(type "jjdoc" with no arguments for help)
Reading from file /Users/tom/ . . .
Grammar documentation generated successfully in java.bnf
$ $ java -cp lib/batik-awt-util.jar:lib/batik-bridge.jar:lib/batik-css.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!