Skip to content

Railroad EBNF

Last updated:

The EBNF notation (railroad-ebnf-beta) draws a railroad diagram from an Extended Backus-Naur Form grammar. Among the railroad notations, it is the one Mermaid Studio parses with a dedicated grammar, so rule names resolve as symbols and editing an EBNF diagram feels like editing code.

The summary below shows the editor support the EBNF notation ships with, grouped by capability.

IDE feature supportSelect a feature for its description.
Editing
Code Intelligence
Refactoring

Rule references resolve to their declarations, so find usages, Go to Declaration, and rename ( Shift + F6 Shift + F6 ) work across the whole grammar without touching quoted terminals. The commenter understands all three comment styles (W3C /* */, ISO (* *), and Mermaid %% lines), and a parse error in one rule stays in that rule instead of cascading through the file.

An EBNF diagram opens in a split editor with the highlighted grammar on the left and the rendered railroad tracks on the right, both following your IDE theme. Because the dedicated grammar colors by role, each rule name carries the declaration color where it is defined and the reference color where another rule uses it, so the shape of the grammar reads at a glance.

A JSON grammar in the EBNF notation, its rule declarations and references in distinct colors, rendered as railroad tracks in the live preview under the Islands Dark theme

An EBNF diagram opens with the railroad-ebnf-beta keyword and optional meta statements, then declares one rule per statement, each ending with a semicolon. The example below defines a small JSON grammar:

railroad-ebnf-beta
title "JSON Grammar"
json = element ;
element = object | array | string | number | "true" | "false" | "null" ;
object = "{" [ member ( "," member )* ] "}" ;
member = string ":" element ;

Key elements:

  • A rule assigns a name with = or the ISO ::= form and ends with ;
  • | separates alternatives, ( ) groups, [ ] marks an optional part, and { } marks repetition
  • The postfix quantifiers ?, *, and + work on any term, and the ISO exception operator - excludes a match
  • Terminals are quoted strings; ? free text ? marks a special sequence
  • Comments come in three styles: W3C /* */, ISO (* *), and Mermaid %% line comments
  • title, accTitle, and accDescr statements must appear before the first rule

For complete syntax details, see the Mermaid railroad documentation.