ebnf

Bigger-granularity Railroad diagram generator?


Is there an EBNF railroad generator that can create bigger chunks, not one diagram per grammar production?

I've tried https://www.bottlecaps.de/rr/ui and it's quite good: can scrape EBNF grammars from W3C specs (eg http://www.w3.org/TR/sparql11-query/), generates either a single XHTML with embedded SVG, or a zip with many PNGs, cross-links the definitions and usages.

However, the SPARQL 1.1 grammar has 173 productions. It's very hard to understand when you have one diagram per production. I'm looking for something chunked like this one: http://ontologicalengineering.blogspot.com/2008/12/sparql-railroad-diagram-from-hell.html


Solution

  • I ran into this problem too. I ended up writing a quick and dirty vim script to compress my entire grammar into a single production.

    The problem with less granular railroad diagrams is that they lose some of the labeling given to them by the more granular ones.

    For any regular (IE: sans recursion) subset of the EBNF you can just 'find-and-replace' the left hand side with the right hand side until it is in the desired form. For example I compressed:

    Identifier ::= Letter AlphaNum*
    Letter ::= [a-zA-Z]|[_]
    NonZeroDigit ::= [1-9]
    Digit ::= NonZeroDigit | [0]
    AlphaNum ::= Letter|Digit
    

    into:

    Identifier ::= ([a-zA-Z]|[_]) (([a-zA-Z]|[_])|(([1-9])|[0]))*
    

    which had the desired effect.

    Process:

    1. Find a rule containing only terminals (EG: Letter ::= [a-zA-Z]|[_]) EDIT: If you can't find any, just pick any rule, it still works but leads to a slightly messier process
    2. Replace all occurrences of the left hand side with the right hand side surrounded in brackets.
    3. Repeat until the desired granularity is achieved.

    Hope this helps!