Content deleted Content added
→Principle of syntax diagrams: square => rectangular |
Undid revision 1293116503 by Jmason37 (talk) spam; we do not need two links to the same site |
||
(29 intermediate revisions by 17 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]]
== Principle
The representation of a grammar is
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.
== Example ==
We use arithmetic expressions as an example
<source lang="bnf">▼
BNF:
<expression> ::= <term> | <term> "+" <expression>
<term> ::= <factor> | <factor> "*" <term>
Line 18 ⟶ 21:
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
</syntaxhighlight>
EBNF:
<
expression = term , [ "+" , expression ];
term = factor , [ "*" , term ];
Line 29 ⟶ 32:
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
</syntaxhighlight>
ABNF:
<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]].
[[Image:
==See also==
Line 48 ⟶ 74:
* [http://www.informatik.uni-freiburg.de/~thiemann/haskell/ebnf2ps/ From EBNF to a postscript file with the diagrams]
* [https://karmin.ch/ebnf/index EBNF Parser & Renderer]
* [https://www.sqlite.org/docsrc/finfo?name=art/syntax/bubble-generator.tcl
* [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 ]
|