Syntax diagram: Difference between revisions

Content deleted Content added
Yobot (talk | contribs)
m top: References after punctuation per WP:REFPUNCT, WP:CITEFOOT, WP:PAIC + other fixes
Undid revision 1293116503 by Jmason37 (talk) spam; we do not need two links to the same site
 
(24 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Visual description of context-free grammar}}
'''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]], [[Augmented Backus–Naur form]], and other text-based grammars as [[metalanguages]]. Early books using syntax diagrams include the "Pascal User Manual" written by [[Niklaus Wirth]] <ref name="Wirth1974">[http://e-collection.library.ethz.ch/eserv/eth:3059/eth-3059-01.pdf Niklaus Wirth: ''The Programming Language Pascal.'' (July 1973)]</ref> (diagrams start at page 47) and the Burroughs [[CANDE]] Manual.<ref name="burroughs71">[http://bitsavers.org/pdf/burroughs/B6500_6700/5000318_B6700_CANDE_Oct72.pdf Burroughs B6700/B7700 ''Command AND Edit (CANDE) Language: Information Manual'']</ref> 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 exchange|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"nonterminal" stage in a process. 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. TerminalsHistorically, areterminals have been represented by round boxes whileand nonterminals are represented by rectangular boxes but there is no official standard.
 
== Example ==
 
We use arithmetic expressions as an example., Firstin wevarious providegrammar a simplified BNF grammar:formats.
 
BNF:
<syntaxhighlight lang="bnf">
<expression> ::= <term> | <term> "+" <expression>
Line 20 ⟶ 23:
</syntaxhighlight>
 
EBNF:
This grammar can also be expressed in EBNF:
<syntaxhighlight lang="ebnf">
expression = term , [ "+" , expression ];
Line 31 ⟶ 34:
</syntaxhighlight>
 
ABNF:
{{Self-contradictory|about='expression' and 'term'; the diagrams do not match EBNF|date=October 2020}}
<syntaxhighlight lang="abnf" highlight="6">
expression = term ["+" expression]
term = factor ["*" term]
factor = constant / variable / "(" expression ")"
variable = "x" / "y" / "z"
constant = 1*digit
DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
</syntaxhighlight>
ABNF also supports ranges, e.g. {{code|2=abnf|1=DIGIT = %x30-39}}, but it is not used here for consistency with the other examples.
 
[[Red (programming language)]] Parse Dialect:
<syntaxhighlight lang="Red" highlight="7">
Red [Title: "Parse Dialect"]
expression: [term opt ["+" expression]]
term: [factor opt ["*" term]]
factor: [constant | variable | "(" expression ")"]
variable: ["x" | "y" | "z"]
constant: [some digit]
digit: ["0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"]
</syntaxhighlight>
This format also supports ranges, e.g. {{code|2=red|digit: charset [#"0" - #"9"]}}, but it is not used here for consistency with the other examples.
 
One possible syntax diagram for the example grammars is below. While the syntax for the text-based grammars differs, the syntax diagram for all of them can be the same because it is a [[metalanguage]].
One possible set of syntax diagrams for either of these grammars is:
 
[[Image:ExampleSyntax-diagram-example.png|"Railroad" syntax diagram 3.svg]]
 
==See also==
Line 51 ⟶ 75:
* [https://karmin.ch/ebnf/index EBNF Parser & Renderer]
* [https://www.sqlite.org/docsrc/finfo?name=art/syntax/bubble-generator.tcl SQLite syntax diagram generator for SQL]
* [https://www.bottlecaps.de/rr Online Railroad Diagram Generator]
* [https://www.yorku.ca/jmason/asdgram.htm Augmented Syntax Diagram (ASD) grammars]
* [http://www.asd-networks.com (ASD) '''Augmented''' Syntax Diagram Application Demo Site ]