Railroad diagrams
Right now this is WIP - work in progress!
But let me tell you: Railroad diagrams are a great thing!
Let's start by collecting the pieces
Grammar tools and generators
KGT: Kate's Grammar Tool
Do you want to convert various kinds of BNF to other kinds of BNF? No? Well imagine if you did! This would be the tool for you.
Try it online at arthursonzogni.com/Diagon/#code_area
Building KGT on Ubuntu 20.04:
git clone https://github.com/katef/kgt.git
cd kgt
sudo apt update
sudo apt install pmake build-essential
pmake
build/bin/kgt --help
Assume this file INPUT.abnf.txt
:
expr = term "+" expr / term
term = factor "*" term / factor
factor = "(" expr ")" / const
const = integer
Create all known output formats from file INPUT.abnf.txt
:
#!/bin/bash
set -x
# AST - input or output
kgt -l abnf -e abnf < INPUT.abnf.txt >result.abnf.txt
kgt -l abnf -e bnf < INPUT.abnf.txt >result.bnf.txt
kgt -l abnf -e iso-ebnf < INPUT.abnf.txt >result.iso-ebnf.txt
kgt -l abnf -e wsn < INPUT.abnf.txt >result.wsn.txt
# AST - output
kgt -l abnf -e ebnfhtml5 < INPUT.abnf.txt >result.ebnfhtml5.txt
kgt -l abnf -e blab < INPUT.abnf.txt >result.blab.txt
kgt -l abnf -e dot < INPUT.abnf.txt >result.dot.txt
kgt -l abnf -e sid < INPUT.abnf.txt >result.sid.txt
# RRD tree
kgt -l abnf -e rrdot < INPUT.abnf.txt >result.rrdot.txt
kgt -l abnf -e rrdump < INPUT.abnf.txt >result.rrdump.txt
kgt -l abnf -e rrll < INPUT.abnf.txt >result.rrll.txt
kgt -l abnf -e rrparcon < INPUT.abnf.txt >result.rrparcon.txt
kgt -l abnf -e rrta < INPUT.abnf.txt >result.rrta.txt
# tnode tree
kgt -l abnf -e svg < INPUT.abnf.txt >result.svg.txt
kgt -l abnf -e rrtext < INPUT.abnf.txt >result.rrtext.txt
kgt -l abnf -e rrtdump < INPUT.abnf.txt >result.rrtdump.txt
This is how result.svg.svg
looks like:
Here is result.rrutf8.txt
:
expr:
│├──╮── term ── "+" ── expr ──╭──┤│
│ │
╰───────── term ──────────╯
term:
│├──╮── factor ── "*" ── term ──╭──┤│
│ │
╰───────── factor ──────────╯
factor:
│├──╮── "(" ── expr ── ")" ──╭──┤│
│ │
╰──────── const ─────────╯
const:
│├── integer ──┤│
This is how result.rrtext.txt
looks like:
expr:
||--v-- term -- "+" -- expr -->--||
| |
`--------- term ----------'
term:
||--v-- factor -- "*" -- term -->--||
| |
`--------- factor ----------'
factor:
||--v-- "(" -- expr -- ")" -->--||
| |
`-------- const ---------'
const:
||-- integer --||
Here is file result.rrta.txt
:
add('expr', Diagram(
Choice(0,
Sequence(
NonTerminal("term"),
Terminal("+"),
NonTerminal("expr")),
NonTerminal("term"))));
add('term', Diagram(
Choice(0,
Sequence(
NonTerminal("factor"),
Terminal("*"),
NonTerminal("term")),
NonTerminal("factor"))));
add('factor', Diagram(
Choice(0,
Sequence(
Terminal("("),
NonTerminal("expr"),
Terminal(")")),
NonTerminal("const"))));
add('const', Diagram(
NonTerminal("integer")));
'tabatkins' generator then can produce output like
tabatkins/railroad-diagrams !!!
A small JS+SVG library for drawing railroad syntax diagrams, like on JSON.org. Now with a Python port!
parcon
Parcon is a Python parser combinator library written by Alexander Boyd. It's designed to be easy to learn, easy to use, and to provide informative error messages. Parcon also allows parsing using BNF grammars.
Sid
sid turns specifications of languages into programs that recognise those languages (Katherine Flavel, The TenDRA Project).
lukaslueg railroad_dsl
A small DSL to generate syntax diagrams