Possible Duplicate:
Tool for generating railroad diagram used on json.org
SQLite has some awesome graphs showing the grammer of the language on their website, does anyone know how these are made?
Is there a tool for generating graphs from grammas?
Possible Duplicate:
Tool for generating railroad diagram used on json.org
SQLite has some awesome graphs showing the grammer of the language on their website, does anyone know how these are made?
Is there a tool for generating graphs from grammas?
This example looks a lot like a finite automaton -- i.e. the graph equivalent of a regular expression. If you can repesent your grammar to a RE (naturally, not all grammars will be representable as REs!), you can use the Kleene's theorem to translate it into a FA graph.
Note that the alphabet in question for the REs is not single letters, but words and tokens. In the above example, the corresponding RE looks like:
DELETE FROM qualified-table-name
(WHERE expr|()) /* "WHERE expr" is optional; the alternative branch is the empty expression "()" */
(
(ORDER BY ordering-term (, ordering-term)*|()) /* ", ordering-term" may be repeated */
LIMIT expr ((OFFSET|,) expr|()) /* can use "OFFSET" or "," */
|()
)
This translates into a FA very similar to your diagram. GraphViz will do a passable job of drawing it legibly.
However that's not quite the same as the original, is it? Presenting it nicely is the next challenge. I'd suggest taking the nested RE expressions and rendering them recursively, starting at the leaves.
For example, to render (WHERE expr|())
:
WHERE expr
:
WHERE
as box.expr
as a box.()
as a single arrow.Doing this graphically means keeping track of box sizes and positions, including invisible boxes. There is an invisible box round each subpart. There are three things to note about the recursive structure:
This implies that you should first calculate the sizes of each part, starting at the bottom. Then, once you know the size of the root, you can start positioning the parts, top-down.