Syntax diagram

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Syntax diagrams (or railroad diagrams) are a way to represent a large thing context-free grammar. They represent a graphical alternative to Backus–Naur Form or to EBNF. 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 well understood by compiler writers and compilers, but is not well understood by most users of languages. Railroad diagrams are more readily understood by most people. Some of the popularity of the JSON data interchange format is due to its representation in railroad diagrams.[citation needed]

Contents

[edit] 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.

[edit] Example

We use arithmetic expressions as an example. First we provide a simplified BNF grammar:

<expression> ::= <term> | <term> "+" <expression>
<term>       ::= <factor> | <factor> "*" <term>
<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 , {"+" , term};
term       = factor , {"*" , 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:

Syntax Diagrams.png

[edit] See also

[edit] References

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::