Skip to content
Mernyx
Open menu

Mermaid Railroad Diagram Cheat Sheet

A grammar rule, drawn.

Every picture on this page was drawn byMernyx, the native Mermaid editor for macOS. Not by a browser running Mermaid.js — the same renderer the app uses for its canvas.

A rule

A rule is a name, an equals sign and a definition, ending with a semicolon. terminal is a literal, nonterminal is another rule.

railroad-beta
    rule = sequence(terminal("a"), nonterminal("other")) ;
    other = terminal("b") ;
A railroad diagram of two rules: rule runs a and other in sequence, and other is b.Expand diagram

A choice

choice takes the alternatives in brackets. The rule cannot be called choice itself — that name is taken.

railroad-beta
    alt = choice(terminal("a"), terminal("b")) ;
A railroad diagram of one rule, alt, offering either a or b.Expand diagram

An optional part

optional wraps a part that may be left out.

railroad-beta
    rule = sequence(optional(nonterminal("tail")), terminal("head")) ;
    tail = terminal("t") ;
A railroad diagram of one rule whose first part, tail, is optional and whose second part is head.Expand diagram

Repeating

oneOrMore needs at least one, zeroOrMore needs none. Both wrap the part they repeat.

railroad-beta
    digits = oneOrMore(terminal("0")) ;
    words = zeroOrMore(terminal("x")) ;
Two railroad diagrams side by side: digits repeating 0 at least once, and words repeating x any number of times.Expand diagram

Draw it in Mernyx.

Open a .mmd file, move a node on the canvas, and the source comes with it. Native on macOS, no browser involved.

Diagram preview