BNF之讲解 && ABNF之规范 (RFC 5234)

为什么要了解BNF呢?

  • RFC的规范很多都使用了BNF,比如TCP、IP、HTTP、URI、DNS等等 的RFC里面都使用了ABNF。可以说这是个阅读的基础内容了
  • 很多的计算机的书籍上也使用了BNF,比如《编译原理》龙书,

阅读内容:

自我观感:

  • 为什么BNF可以给人一种清晰的感觉?1、因为它可以定义一个有限集,把所有可能的情况都列出来。2、规则是可以复用的,就和代码可以复用一样,让人感觉简洁明了。3、BNF的规则是可以递归的,所以规则可以表示一个无限集。
  • RFC系列的源文件地址:
    都提交于Gitee,https://gitee.com/testzyh/notes/tree/master/RFC

BNF

元语法(metasyntax):在逻辑学与计算机科学里,一个元语法定义了可以在元语言(metalanguage)中使用的短语与句子的结构与组合。
元语言(Metalanguage):是一种用来描述其他语言的语言(对象语言)。
元:这个词我认为可以指以一种更高层次的方式、角度去看待、认识原有的知识内容。

上下文无关语法(Context-free grammar):在形式语言的理论中,一个context-free grammar (CFG) 就是一个形式语法其生产规则有着如下形式:A → α ;A是一个非终止符,α是一个终止符或者非终止符(with A a single nonterminal symbol, and α a string of terminals and/or nonterminals (α can be empty). )。一个形式语法是上下文自由的,如果其生成规则可以在任意的上下文的nonterminal(非终止符)里使用。无论什么符号包围着它,这个在左边的nonterminal(A)总是可以被替换为右边的(α)。这就是区分上下文敏感语法(context-sensitive grammar)的地方。
参考:https://en.wikipedia.org/wiki/Context-free_grammar

巴科斯范式(Backus–Naur form 或者 Backus normal form,BNF)是一种元语法(metasyntax)标记,可以用在上下文无关的语法。它经常用来描述语言中语法中的计算(often used to describe the syntax of languages used in computing)。比如 在 计算机编程语言、文档格式、指令集以及通信协议中。其用在需要准确无误地描述一个语言的地方,比如官方语言规范、手册、计算机理论教科书上。


Peter Naur在其 ALGOL 60的报告中描述了BNF,作为元语言学的公式:

  • <>包围中的连续字符表示了元语言学的变量,其值为符号的序列。而标记 ::=|作为元语言学的连接符。任何的公式中的标记,不是变量、不是连接符都表示其本身。
  • 并置:Juxtaposition of marks or variables in a formula signifies juxtaposition of the sequence denoted.

概览

一个BNF声明了一组推演规则,书写如下:

  • <symbol>是nonterminal,而 __expression__包含一个或者多个terminal或者nonterminal的序列。
  • ::=意味着 左边的符号必须被右边的表达式替换。
  • 多个符号的序列被 | 分隔,表示一个选择,是所有可能出现的情况。
  • 不会再左边出现的是terminal。反之,出现在左边的就是nonterminal,其总是被 <>包围。
<symbol> ::= __expression__
  • 例子

    • postal-address由三部分组成,首先是name-part,后面跟着street-address,最后就是zip-part。
    • name-part有两个选择,第一个是就是4个连在一起。第二个,一个personal-part后面又跟着一个name-part(这里使用了 BNF的递归:比如一个名字包含多个first name,或者多个中间部分middle name)
    • personal-part的两条路,1、initial部分后跟着 字符 . 2、first-name
    • street-address由4个部分组成
    • zip-part由5个部分组成
    • opt-suffix-part有4个选择:1、字符 Sr. 2、字符 Jr. 3、roman-numeral 4、空字符串
    • opt-apt-num:apt-num 或者 空字符串
    <postal-address> ::= <name-part> <street-address> <zip-part>
    <name-part> ::= <personal-part> <last-name> <opt-suffix-part> <EOL> | <personal-part> <name-part>
    <personal-part> ::= <initial> "." | <first-name>
    <street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL>
    <zip-part> ::= <town-name> "," <state-code> <ZIP-code> <EOL>
    <opt-suffix-part> ::= "Sr." | "Jr." | <roman-numeral> | ""
    <opt-apt-num> ::= <apt-num> | "
    

此外,BNF有很多其他的扩展:比如:extended Backus–Naur form (EBNF) and augmented Backus–Naur form (ABNF)

ABNF

Augmented BNF for Syntax Specifications: ABNF

被更新:
Updated by: 7405

替代:
Obsoletes: 4234 --(替代)–》2234

representational:(used especially of art) depicting objects, figures,or scenes as seen

概要:
互联网技术的规范里经常定义一种形式化的语法。BNF的修订版ABNF逐渐在互联网的规范里变得流行起来。
Internet technical specifications often need to define a formal syntax. Over the years, a modified version of Backus-Naur Form (BNF), called Augmented BNF (ABNF), has been popular among many Internet specifications. The current specification documents ABNF. It balances compactness and simplicity with reasonable representational power. The differences between standard BNF and ABNF involve naming rules, repetition, alternatives, order-independence, and value ranges. This specification also supplies additional rule definitions and encoding for a core lexical analyzer of the type common to several Internet specifications.

1、介绍

标准的BNF与ABNF在规则的命名、重复、可选择项、顺序独立以及取值范围上不同。

通用规则:在附录B上,定义一组在多个规范上通用的规则。
(Appendix B supplies rule definitions and encoding for a core lexical analyzer of the type common to several Internet specifications. It is provided as a convenience and is otherwise separate from the meta language defined in the body of this document, and separate from its formal status.)

2、规则的定义

2.1、规则的命名

一序列的字符串,以字母开头,后面跟着字母、数字或横线(hyphen)

注意:不区分大小写

所以,<rulename>, <Rulename>, <RULENAME>, 与 <rUlENamE> 指的是同一个规则。

不像原来的BFN,需要 <>进行包围。但是在提到某个规则时,可以给它加上 <>,用以区分。

2.2 规则的形式

规则的定义:name = elements crlf

  • <name>是规则的名字
  • <elements>是一个或多个规则的名字
  • <crlf>是行的终止符

2.3 终止的值

Rules resolve into a string of terminal values, sometimes called characters.

In ABNF, a character is merely a non-negative integer.

先定义进制:

b = binary
d = decimal
x = hexadecimal

所以:

CR = %d13
CR = %x0D

使用 .号表示字符串的连接:
CRLF = %d13.10

使用双引号包裹的是字面的文本(literal text):
command = "command string"

Literal text strings are interpreted as a concatenated set of printable characters.

注意:即使是literal text也是不区分大小写的。

rulename = “abc” ,可以匹配"abc", “Abc”, “aBc”, “abC”, “ABc”, “aBC”, “AbC”, 与 “ABC”.

区分大小写的定义:

rulename = %d97 %d98 %d99
rulename = %d97.98.99

2.4 其他的编码方式

3、操作符

3.1 规则连接:Rule1 Rule2

规则 <mumble>匹配小写字母 aba

foo = %x61 ; a
bar = %x62 ; b
mumble = foo bar foo

3.2 可选:Rule1 / Rule2

/分隔的element是可选择的。

可以接受 <foo><bar>

foo / bar

3.3 自增可选:Rule1 =/ Rule2

oldrule =/ additional-alternatives

所以:

ruleset = alt1 / alt2
ruleset =/ alt3
ruleset =/ alt4 / alt5

表示的是:ruleset = alt1 / alt2 / alt3 / alt4 / alt5

3.4 范围可选:%c##-##

DIGIT = %x30-39

等同于:

DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" /
	    "7" / "8" / "9"

3.5 序列组:(Rule1 Rule2)

elements被圆括号包围的当做单个element处理。

elem (foo / bar) blat可以匹配 (elem foo blat)或者 (elem bar blat)

elem foo / bar blat可以匹配 (elem foo) 或者 (bar blat)

3.6 变量重复:*Rule

操作符 *出现在element前表示重复。

完全形式为:<a>*<b>element

  • <a><b>是可选项,表示最少出现a次,最多出现b次。
  • 只有一个 *表示0次或者任意多次。

3.7 指明重复:nRule

<n>element等同 <n>*<n>element

3.8 可选序列:[RULE]

[foo bar]等同 *1(foo bar)

3.9 注释:; Comment

注释部分:;号的出现到行尾

3.10 优先级

从上到下:优先级降低

Rule name, prose-val, Terminal value
Comment
Value range
Repetition
Grouping, Optional
Concatenation
Alternative

B.1 核心规则

基础的规则:

         ALPHA          =  %x41-5A / %x61-7A   ; A-Z / a-z
         BIT            =  "0" / "1"
         CHAR           =  %x01-7F
                                ; any 7-bit US-ASCII character,
                                ;  excluding NUL
         CR             =  %x0D
                                ; carriage return
         CRLF           =  CR LF
                                ; Internet standard newline
         CTL            =  %x00-1F / %x7F
                                ; controls
         DIGIT          =  %x30-39
                                ; 0-9
         DQUOTE         =  %x22
                                ; " (Double Quote)
         HEXDIG         =  DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
         HTAB           =  %x09
                                ; horizontal tab
         LF             =  %x0A
                                ; linefeed
         LWSP           =  *(WSP / CRLF WSP)
                                ; Use of this linear-white-space rule
                                ;  permits lines containing only white
                                ;  space that are no longer legal in
                                ;  mail headers and have caused
                                ;  interoperability problems in other
                                ;  contexts.
                                ; Do not use when defining mail
                                ;  headers and use with caution in
                                ;  other contexts.
         OCTET          =  %x00-FF
                                ; 8 bits of data
         SP             =  %x20
         VCHAR          =  %x21-7E
                                ; visible (printing) characters
         WSP            =  SP / HTAB
                                ; white space
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值