kgt in Org Babel

Making use of kgt in Emacs Org-mode with a new Babel driver.

kgt is an excellent tool written by @thingskatedid for converting E?BNF grammars into railroad diagrams. I like to write literate blog posts using Babel in Org-mode so I decided to flex my non-existent elisp powers and write a new Babel backend – that tiny bit of glue between Org-mode and a compiler, interpreter or useful CLI tool.

The ob-kgt driver has :in and :out parameters as well as the standard babel features. For example, the following source block will generate an ASCII railroad in a results block.

#+begin_src kgt :in bnf :out rrtext
<expr> ::= <term> "+" <expr>
        |  <term>
#+end_src

By default, :in=bnf and :out=rrutf8 for Unicode railroad diagrams.

Setting Up

ob-kgt.el is currently available from my dotfiles here:

You need to put ob-kgt.el in your Emacs load path and then add (kgt . t) to your org-babel-load-languages.

kgt needst to be installed on your machine. You can get hold of kgt here:

You can find out more about Org Babel here:

ASCII Output

#+begin_src kgt :out rrtext
<expr> ::= <term> "+" <expr>
        |  <term>
#+end_src
expr:
    ||--v-- term -- "+" -- expr -->--||
        |                         |
        `--------- term ----------'

Unicode Output

#+begin_src kgt
<expr> ::= <term> "+" <expr>
        |  <term>
#+end_src
expr:
    │├──╮── term ── "+" ── expr ──╭──┤│
        │                         │
        ╰───────── term ──────────╯

SVG Output

#+begin_src kgt :out svg :file expr.svg :results file
<expr> ::= <term> "+" <expr>
        |  <term>

<term> ::= <factor> "*" <term>
        |  <factor>

<factor> ::= "(" <expr> ")"
          |  <const>

<const> ::= <integer>
#+end_src
SVG railroad diagram for the BNF in the example
comments powered by Disqus