Backus–Naur Form
来源:互联网 发布:卡卡西实力如何知乎 编辑:程序博客网 时间:2024/06/05 18:25
A BNF specification is a set of derivation rules, written as
<symbol> ::= __expression__
where <symbol> is anonterminal, and the__expression__ consists of one or more sequences of symbols; more sequences are separated by thevertical bar, '|', indicating achoice, the whole being a possiblesubstitution for the symbol on the left. Symbols that never appear on a left side areterminals. On the other hand, symbols that appear on a left side arenon-terminals and are always enclosed between the pair <>.
Example
As an example, consider this possible BNF for a U.S. postal address:
<postal-address> ::= <name-part> <street-address> <zip-part> <name-part> ::= <personal-part> <last-name> <opt-jr-part> <EOL> | <personal-part> <name-part> <personal-part> ::= <first-name> | <initial> "." <street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL> <zip-part> ::= <town-name> "," <state-code> <ZIP-code> <EOL> <opt-jr-part> ::= "Sr." | "Jr." | <roman-numeral> | ""
This translates into English as:
- A postal address consists of a name-part, followed by a street-address part, followed by a zip-code part.
- A name-part consists of either: a personal-part followed by a last name followed by an optional suffix (Jr., Sr., or dynastic number) and end-of-line, or a personal part followed by a name part (this rule illustrates the use ofrecursion in BNFs, covering the case of people who use multiple first and middle names and/or initials).
- A personal-part consists of either a first name or an initial followed by a dot.
- A street address consists of a house number, followed by a street name, followed by an optionalapartment specifier, followed by an end-of-line.
- A zip-part consists of a town-name, followed by a comma, followed by a state code, followed by a ZIP-code followed by an end-of-line.
- A opt-jr-part consists of a suffix, such as "Sr.", "Jr." or a roman-numeral, or an empty string (i.e. nothing).
Note that many things (such as the format of a first-name, apartment specifier, ZIP-code, and Roman numeral) are left unspecified here. If necessary, they may be described using additional BNF rules.
Further examples
BNF's syntax itself may be represented with a BNF like the following:
<syntax> ::= <rule> | <rule> <syntax> <rule> ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end> <opt-whitespace> ::= " " <opt-whitespace> | "" <!-- "" is empty string, i.e. no whitespace --> <expression> ::= <list> | <list> "|" <expression> <line-end> ::= <opt-whitespace> <EOL> | <line-end> <line-end> <list> ::= <term> | <term> <opt-whitespace> <list> <term> ::= <literal> | "<" <rule-name> ">" <literal> ::= '"' <text> '"' | "'" <text> "'" <!-- actually, the original BNF did not use quotes -->
This assumes that no whitespace is necessary for proper interpretation of the rule. <EOL> represents the appropriateline-end specifier (inASCII, carriage-return and/or line-feed, depending on theoperating system). <rule-name> and <text> are to be substituted with a declared rule's name/label or literal text, respectively.
In the U.S. postal address example above, the entire block-quote is a syntax. Each line or unbroken grouping of lines is a rule; for example one rule begins with "<name-part> ::=". The other part of that rule (aside from a line-end) is an expression, which consists of two lists separated by a pipe "|". These two lists consists of some terms (three terms and two terms, respectively). Each term in this particular rule is a rule-name.
Variants
There are many variants and extensions of BNF, generally either for the sake of simplicity and succinctness, or to adapt it to a specific application. One common feature of many variants is the use ofregular expression repetition operators such as *
and +
. TheExtended Backus–Naur Form (EBNF) is a common one. In fact the example above is not the pure form invented for the ALGOL 60 report. The bracket notation "[ ]" was introduced a few years later inIBM's PL/I definition but is now universally recognised.ABNF and RBNF are other extensions commonly used to describe Internet Engineering Task Force (IETF) protocols.
Parsing expression grammars build on the BNF andregular expression notations to form an alternative class of formal grammar, which is essentially analytic rather than generative in character.
Many BNF specifications found online today are intended to be human readable and are non-formal. These often include many of the following syntax rules and extensions:
- Optional items enclosed in square brackets. E.g. [<item-x>]
- Items repeating 0 or more times are enclosed in curly brackets or suffixed with an asterisk. E.g. <word> ::= <letter> {<letter>}
- Items repeating 1 or more times are followed by a '+'
- Terminals may appear in bold and NonTerminals in plain text rather than using italics and angle brackets
- Alternative choices in a production are separated by the ‘|’ symbol. E.g., <alternative-A> | <alternative-B>
- Where items need to be grouped they are enclosed in simple parentheses
- Backus–Naur Form
- Backus–Naur Form(巴科斯范式)
- 扩展的巴科斯范式(Augmented Backus–Naur Form)
- 巴科斯范式(BNF: Backus-Naur Form)
- 巴科斯范式BNF: Backus-Naur Form介绍
- The syntax of C in Backus-Naur form
- 类C语言的 BNF(backus-Naur Form)文法
- 闲聊Backus Naur Form--巴科斯-诺尔(BNF范式)
- 学习巴克斯范式BNF(Backus Naur Form)读懂SAP HANA语法
- John W.Backus
- 2005年的图灵奖获得者-Peter Naur
- 1977年的图灵奖获得者-John W. Backus
- Fortran的发明人John W. Backus过世了
- Form
- form
- form
- Form
- form
- SQL截取字符串
- WINCE6.0下开始菜单的“挂起(suspend)”是否可见及阻止系统进入睡眠模式
- 与TCL风雨同行
- Android中SQLite应用详解
- Run DTS from stored procedure
- Backus–Naur Form
- JSP重定向和转发
- 计算完美HASH
- spring声明式事务(一)
- spring声明式事务(二)
- spring声明式事务(三)
- java虚拟机CPU占用率过高排查方法
- spring声明式事务(四)
- spring声明式事务(五)