Raw capture — raw/automatic-layout-railroad-diagrams.md
url: https://arxiv.org/abs/2509.15834 title: Automatic Layout of Railroad Diagrams author: Shardul Chiplunkar, Clément Pit-Claudel date_fetched: 2026-07-25 date_published: 2025-09-19
Authors: Shardul Chiplunkar and Clément Pit-Claudel, EPFL, School of Computer and Communication Sciences, Lausanne, Switzerland Submitted: 19 September 2025 Subject: Computer Science > Programming Languages (cs.PL) License: CC BY 4.0 Pages: 24 pages + 2 appendix + 3 references
Railroad diagrams (syntax diagrams) are a common visualization of grammars but mostly remain hand-drawn due to limited tooling. This paper presents the first formal treatment of railroad diagram layout with a principled, practical implementation. The authors define compiling from a diagram language (conceptual components and connections) to a layout language (shapes with sizes/positions), implementing line wrapping via optimization, vertical alignment, and horizontal justification per user policies.
The paper characterizes railroad layout as a "1.5-dimensional" problem — between 1D (text wrapping, code pretty-printing) and 2D (graph layout). Unlike text, "a row can split into two rows that independently follow the reading order until they merge, or turn backwards."
Four constructors:
- Terminal tokens: "lbl" (literal string)
- Nonterminal tokens: [lbl] (reference)
- Sequences: (d ...) — n-ary, zero or more subterms
- Stacks: (pol d d) — binary, with polarity + or -
Empty sequence () is denoted ε. Sequences are n-ary because association order doesn't affect layout. Stacks are binary because "having stacks be only binary is the simplest model that accounts exactly for all possible layouts."
Six constructors: rail, space, station, hconcat, vconcat-inline, vconcat-block — with tip specifications (vertical, logical row, or physical proportion 0–1) and direction (ltr/rtl).
Formal inference rules define width, logical rows, and connectability (up/down/both/neither) for each constructor.
The diagram-of function maps layouts back to diagrams. A layout ℓ of diagram d must satisfy equivalence and well-formedness.
Alignment and justification are styled as "à la Flexbox" — main-axis (horizontal) justification and cross-axis (vertical) alignment.
Mappings from regex/BNF to diagram language, including Kleene star as (- () (rrd r)). Notes tension between canonicity (equal representations for equal objects) and idiomaticity (convenient expression of common patterns).
Analysis of SQLite's 71 syntax diagrams: 45 perfectly expressible in layout language, 22 expressible in diagram language but must be laid out differently, 4 ill-nested requiring rewriting. Identifies stylistic inconsistencies (extra arrowheads, identical diagrams laid out differently "for no apparent reason").
Excluded cases from the formalism: Apple Pascal's ill-nesting, Crockford's visually ill-nested JSON layout, SQLite's vertical ε lines, and the hypothetical AlternatingSequence constructor.
Formal treatment of railroad layout is both possible and practical. The three-step approach produces layouts comparable to hand-drawn ones. Wrapping as optimization with local heuristics avoids combinatorial explosion. The diagram language captures most real-world railroad diagrams.
Repository: https://github.com/epfl-systemf/librrd Web UI: https://systemf.epfl.ch/etc/librrd/