Syntax diagram: Difference between revisions

Content deleted Content added
Undid revision 1293116503 by Jmason37 (talk) spam; we do not need two links to the same site
 
(131 intermediate revisions by 94 users not shown)
Line 1:
{{Short description|Visual description of context-free grammar}}
'''Syntax diagrams''' (or '''railroad diagrams''') are a way to represent a [[formalcontext-free grammar]]. They represent a graphical alternative to [[Backus-NaurBackus–Naur Formform]] or, [[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">Wirth,[http://e-collection.library.ethz.ch/eserv/eth:3059/eth-3059-01.pdf Niklaus, Wirth: ''PASCALThe -Programming UserLanguage Manual and ReportPascal.'', Springer(July Verlag, 1974.1973)]</ref> (diagrams start at page 47) and the Burroughs [[CANDE]] manual 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 welltext-based, understoodand used by compiler writers and compilers,parser butgenerators. isRailroad notdiagrams wellare understoodvisual, byand mostmay users of languages. Railroad diagrams arebe more readily understood by mostlaypeople, people.sometimes Someincorporated ofinto thegraphic popularitydesign. ofThe canonical source defining the [[JSON]] [[data exchange|data interchange]] format isprovides dueyet toanother itsexample representationof ina popular modern usage of railroadthese diagrams.
 
== Principle ==
 
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 possiblespossible 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 squarerectangular boxes but there is no official standard.
 
== Example ==
 
We use arithmetic expressions as an example., Firstin wevarious providegrammar a simplified BNF grammar:formats.
 
BNF:
<expression> ::= <term> | <term> "+" <expression>
<syntaxhighlight lang="bnf">
<termexpression> ::= <factorterm> | <factorterm> "*+" <termexpression>
<term> ::= <factor> | <factor> "*" <term>
<factor> ::= <constant> | <variable> | "(" <expression> ")"
<variable> ::= "x" | "y" | "z"
<variableconstant> ::= "x"<digit> | "y" | "z"<digit> <constant>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<constant> ::= <digit> | <digit> <constant>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
 
</syntaxhighlight>
This grammar can also be expressed in [[EBNF]]:
 
EBNF:
expression = term , {"+" term};
<syntaxhighlight lang="ebnf">
termexpression = factorterm , {[ "*+" factor}, expression ];
term = factor , [ "*" , term ];
factor = constant | variable | "(" , expression , ")";
variable = "x" | "y" | "z";
variableconstant = "x"digit |, "y"{ |digit "z"};
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
constant = digit , {digit};
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
 
</syntaxhighlight>
One possible set of syntax diagrams for this grammar is :
 
ABNF:
[[Image:Syntax_Diagrams.png|Diagrams for a simple grammar of expressions]]
<syntaxhighlight lang="abnf" highlight="6">
<expression> ::= <term> | <term> ["+" <expression>]
term = factor ["*" term]
factor = constant / variable / "(" expression ")"
variable = "x" / "y" / "z"
constant = digit ,= {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:
==See also==
<syntaxhighlight lang="Red" highlight="7">
* [[Extended Backus-Naur form]] (EBNF)
Red [Title: "Parse Dialect"]
expression: = [term ,opt {["+" term};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]].
==References==
<references/>
 
[[Image:Syntax-diagram-example.png|"Railroad" syntax diagram]]
==External Links==
* {{en}} [http://www.json.org/ JSON website including syntax diagrams]
* {{en}} [http://www-cgi.uni-regensburg.de/~brf09510/syntax.html Automatic generator of syntax diagrams]
* {{en}} [http://www-cgi.uni-regensburg.de/~brf09510/syntax/lazyebnf.ebnf.html Syntax diagrams of BNF]
* {{en}} [http://dotnet.jku.at/applications/Visualizer/ Generator from EBNF]
* {{en}} [http://www.informatik.uni-freiburg.de/~thiemann/haskell/ebnf2ps/ From EBNF to a postscript file wit the diagrams]
 
==See also==
{{Formal languages and grammars}}
* [[Recursive transition network]]
* [[Extended Backus-NaurBackus–Naur form]] (EBNF)
 
==References==
[[Category:Formal languages]]
{{reflist}}
Note: the first link is sometimes blocked by the server outside of its ___domain, but it is available on [https://web.archive.org/web/20180909121538/https://www.research-collection.ethz.ch/bitstream/handle/20.500.11850/68910/eth-3059-01.pdf archive.org]. The file was also mirrored at [http://www.standardpascal.org/The_Programming_Language_Pascal_1973.pdf standardpascal.org].
 
==External Linkslinks==
{{computer-stub}}
* {{en}} [httphttps://www.json.org/ JSON website including syntax diagrams]
* {{en}} [http://dotnet.jku.at/applications/Visualizer/ Generator from EBNF]
* {{en}} [http://www.informatik.uni-freiburg.de/~thiemann/haskell/ebnf2ps/ From EBNF to a postscript file witwith the diagrams]
* [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 ]
* [https://github.com/gbrault/railroad-diagrams/blob/gh-pages/live/doc/readme.md/ SRFB Syntax Diagram representation by Function Basis + svg generation]
 
[[Category:Formal languages]]
[[fr:Diagramme syntaxique]]
[[Category:Diagrams]]