Content deleted Content added
Undid revision 1293116503 by Jmason37 (talk) spam; we do not need two links to the same site |
|||
(40 intermediate revisions by 27 users not shown) | |||
Line 1:
{{Short description|Visual description of context-free grammar}}
== Principle
The representation of a grammar is a set of syntax diagrams. Each diagram defines a "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. Historically, terminals have been represented by round boxes and nonterminals by rectangular boxes but there is no official standard.
== Example ==
We use arithmetic expressions as an example
<source lang="bnf">▼
BNF:
<expression> ::= <term> | <expression> "+" <term>▼
<term> ::= <factor> | <term> "*" <factor>▼
<factor> ::= <constant> | <variable> | "(" <expression> ")"
<variable> ::= "x" | "y" | "z"
Line 15 ⟶ 21:
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
</syntaxhighlight>
EBNF:
<
expression = term
term = factor
factor = constant | variable | "(" , expression , ")";
variable = "x" | "y" | "z";
constant = digit , { digit };
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 38 ⟶ 67:
==References==
{{reflist}}
Note: the first link is sometimes blocked by the server outside of its ___domain, but it is available on [
==External links==
*
*
*
*
*
*
*
*
*
[[Category:Formal languages]]
|