Generating Syntax Diagrams Using Tk

Tcl/Tk saves the day!

SQLite users wanted syntax diagrams instead of BNF to describe the SQL language. Syntax diagrams look something like this:

http://www.sqlite.org/draft/images/syntax/create-table-stmt.gif

created from this:

 create-table-stmt {
   stack
      {line CREATE {or {} TEMP TEMPORARY} TABLE {opt IF NOT EXISTS}}
      {line {optx /database-name .} /table-name}
      {or {line ( {loop column-def ,} {loop {} {, table-constraint}} )
                 {opt WITHOUT ROWID}}
          {line AS select-stmt}
      }
 }

The question become: how does one generate diagrams? A quick Tcl/Tk script did the job for me nicely. A high-level description of each diagram (in the form of a nested Tcl list, though really more of a lisp predicate) is entered and a Tk script renders the diagram on a canvas widget. The postscript method of the canvas widget is then used to create a postscript file of the diagram. Finally, the "convert" utility of ImageMagick is invoked (via the exec command) to convert the postscript into an antialias-ed GIF at the appropriate resolution.

Lars H: Is that "lisp predicate" an alias for data is code, I wonder? Checking the code, I see it isn't implemented that way, but it seems plausible that it could be…

Since the diagrams have gone up on the SQLite website [L1 ], there have been multiple questions of "how did you do that?" So in answer, I present the script that does the syntax diagram generation for SQLite. (The link below is directly into the Fossil repository for the SQLite documentation [L2 ]. Click on one of the "[view]" link to see the actual text of the script.)

http://www.sqlite.org/docsrc/finfo?name=art/syntax/bubble-generator.tcl

Lars H: Hmm… When just jumping to that page, there aren't any [view] links on it — turns out one has to log in (as anonymous) to see them. And after logging in, I'm put in some sort of limbo, from which I have to manually jump back to the bubble-generator.tcl page. Only then can I [view] the code!

Direct links to the code:

http://www.sqlite.org/docsrc/doc/tip/art/syntax/bubble-generator.tcl?mimetype=text/plain
http://www.sqlite.org/docsrc/doc/tip/art/syntax/bubble-generator.tcl

The first link above will show the code on your browser screen. The second has a mimetype of application/x-tcl and so will either try to download the code or (if you have the Tcl/Tk Tclet Plugin installed, it will try to execute the script in your browser (which won't work.)

MHo And where/what is bubble-generator-data.tcl?

EMJ It specifies the graphs for the SQLite SQL syntax, but should be enough to show you how to do something else. It can be found at:

http://www.sqlite.org/docsrc/doc/tip/art/syntax/bubble-generator-data.tcl?mimetype=text/plain