BNF是为了描述ALGOL 60语言而出现的。
http://www.blog.edu.cn/user1/18646/archives/2005/139037.shtml
巴科斯范式及其扩展
BNF & Augmented BNF
什么是巴科斯范式?
巴科斯范式(BNF: Backus-Naur Form 的缩写)是由 John Backus 和 Peter Naur 首先引入的用来描述计算机语言语法的符号集。
现在,几乎每一位新编程语言书籍的作者都使用巴科斯范式来定义编程语言的语法规则。
巴科斯范式的内容
在双引号中的字("word")代表着这些字符本身。而double_quote用来代表双引号。
在双引号外的字(有可能有下划线)代表着语法部分。
尖括号( < > )内包含的为必选项。
方括号( [ ] )内包含的为可选项。
大括号( { } )内包含的为可重复0至无数次的项。
竖线( | )表示在其左右两边任选一项,相当于"OR"的意思。
::= 是“被定义为”的意思。
巴科斯范式示例
这是用BNF来定义的Java语言中的For语句的实例:
FOR_STATEMENT::=
"for" "(" ( variable_declaration |
( expression ";" ) | ";" )
[ expression ] ";"
[ expression ] ";"
")" statement
|
这是Oracle packages的BNF定义:
package_body ::= "package" package_name "is"
package_obj_body { package_obj_body }
[ "begin" seq_of_statements ]
"end" [ package_name ] ";"
package_obj_body ::= variable_declaration
| subtype_declaration
| cursor_declaration
| cursor_body
| exception_declaration
| record_declaration
| plsql_table_declaration
| procedure_body
| function_body
procedure_body ::= "procedure" procedure_name
[ "(" argument { "," argument } ")" ]
"return" return_type
"is"
[ "declare" declare_spec ";" { declare_spec ";" } ]
"begin"
seq_of_statements
[ "exception" exception_handler { exception_handler } ]
"end" [ procedure_name ] ";"
statement ::= comment
| assignment_statement
| exit_statement
| goto_statement
| if_statement
| loop_statement
| null_statement
| raise_statement
| return_statement
| sql_statement
| plsql_block
|
这是用BNF来定义的BNF本身的例子:
syntax ::= { rule }
rule ::= identifier "::=" expression
expression ::= term { "|" term }
term ::= factor { factor }
factor ::= identifier |
quoted_symbol |
"(" expression ")" |
"[" expression "]" |
"{" expression "}"
identifier ::= letter { letter | digit }
quoted_symbol ::= """ { any_character } """
|
扩展的巴科斯范式 Augmented BNF
RFC2234 定义了扩展的巴科斯范式(ABNF)。近年来在Internet的定义中ABNF被广泛使用。ABNF做了更多的改进,比如说,在ABNF中,尖括号不再需要。
rfc2234:
http://www.rfc-editor.org/cgi-bin/rfcdoctype.pl?loc=RFC&letsgo=2234&type=ftp&file_format=txt
更多资源:
http://www-128.ibm.com/developerworks/cn/java/j-diag/part19/index.html
-
W3C 就有关 使用 BNF 符号提供了八种符号约定。
-
可以在这里找到关于 BNF 符号的简史和介绍。
-
有关 BNF 的更多信息,请参阅这个 扩展 BNF 符号的列表。
What is BNF notation?
http://cui.unige.ch/db-research/Enseignement/analyseinfo/AboutBNF.html#Marcotty86
(包括BNF的历史和较早的文献)
answers.com上的介绍:
http://www.answers.com/topic/backus-naur-form-3
有很多有意义的文献:
-
Algol-60 BNF, the original BNF.
-
Sample grammars at the BNF Web club.
-
[1] contains a posting on news:comp.compilers that explains some of the history of the two names (Backus-Naur form vs. Backus normal form).
-
Article BNF and EBNF: What are they and how do they work? by Lars Marius Garshol.
-
RFC 4234 Augmented BNF for Syntax Specifications: ABNF
-
Comparision of different variants of BNF
-
Syntax diagram of EBNF
-
Generation of syntax diagrams from EBNF
One syntax for specifying BNF grammars can be found in RFC 2234. Another can be found in the ISO 14977 standard.
相当简洁,全面的BNF,EBNF定义(!!!!):
http://www-cgi.uni-regensburg.de/~brf09510/backus_naur_wirth.html
EBNF的国际标准ISO 14977
http://www.cl.cam.ac.uk/~mgk25/iso-14977.pdf
BNF多个版本的比较
http://www-cgi.uni-regensburg.de/~brf09510/grammartypes.html
Augmented BNF for Syntax Specifications: ABNF
ftp://ftp.rfc-editor.org/in-notes/rfc4234.txt
|