Syntax diagram
Syntax diagrams (or railroad diagrams) are a way to represent a context-free grammar. They represent a graphical alternative to Backus–Naur form or to EBNF as metalanguages. Early books using syntax diagrams include the "Pascal User Manual" written by Niklaus Wirth [1] (diagrams start at page 47) and the Burroughs CANDE manual.[2] In the compilation field, textual representations like BNF or its variants are usually preferred. BNF is text-based, and used by compiler writers and parser generators. Railroad diagrams are visual, and may be more readily understood by laypeople, sometimes incorporated into graphic design. The canonical source defining the JSON data interchange format provides yet another example of a popular modern usage of these diagrams.
Principle of syntax diagrams
The representation of a grammar is made of a set of syntax diagrams. Each diagram defines a non-terminal. There is a main diagram which defines the language in the following way: to belong to the language, a word must describe a path in the main diagram.
Each diagram has an entry point and an end point. The diagram describes possible paths between these two points by going through other nonterminals and terminals. Terminals are represented by round boxes while nonterminals are represented by square boxes.
Example
We use arithmetic expressions as an example. First we provide a simplified BNF grammar:
<expression> ::= <term> | <expression> "+" <term>
<term> ::= <factor> | <term> "*" <factor>
<factor> ::= <constant> | <variable> | "(" <expression> ")"
<variable> ::= "x" | "y" | "z"
<constant> ::= <digit> | <digit> <constant>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
This grammar can also be expressed in EBNF:
expression = term | expression, "+" , term;
term = factor | term, "*" , factor;
factor = constant | variable | "(" , expression , ")";
variable = "x" | "y" | "z";
constant = digit , {digit};
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
One possible set of syntax diagrams for this grammar is:
See also
References
Note: the first link is blocked by the server outside of its domain. The file was mirored at http://www.standardpascal.org/The_Programming_Language_Pascal_1973.pdf
External links
- Template:En icon JSON website including syntax diagrams
- Template:En icon Generator from EBNF
- Template:En icon From EBNF to a postscript file with the diagrams
- Template:En icon EBNF Parser & Renderer
- Template:En icon Sqlite syntax diagram generator for SQL
- Template:En icon Online Railroad Diagram Generator
- Template:En icon Augmented Syntax Diagram (ASD) grammars
- Template:En icon (ASD) Augmented Syntax Diagram Application Demo Site
- Template:En icon SRFB Syntax Diagram representation by Function Basis + svg generation