EBNF

EBNF

一、简介

句法元语言(Syntactic metalanguages)是计算机科学的重要工具是大家熟知的概念,因为使用了许略有不同的符号,导致句法元语言未能被广泛的使用。
EBNF(Extended BNF)引进一些句法的正式定义,从而广泛使用在编程语言的定义中。

二、句法元语言

句法元语言是一种通过使用许多规则来定义语言句法的标记性语言。
每个规则都命名了语言的一部分(称为语言的非终结符),并定义其可能的形式。
语言的终结符是不能拆分为该语言更小组成部分的原子组成。
句法元语言在要求明确的描述和定义时非常有效,比如,提交给期刊的论文中的参考文献格式,或执行复杂任务的说明。

正式的语法定义有三种不同用法:
    * 命名该语言的各个句法组成
    * 显示哪些符号序列是该语言的有效语句
    * 显示该语言各个语句的句法结构

三、标准句法元语言的需求

若没有标准的句法元语言,每种编程语言的定义都会从指定用于定义其句法的元语言开始,这会导致很多问题:
    * 许多不同的标记:两种不同编程语言使用的元语言一般是不相同的,这样人们在学习新语言之前必须先学习一门新的元语言。
    * 不易理解的概念:标准标记的缺乏会阻碍严格而明确的定义概念
    * 不完善的标识:每种编程语言都要定义一种元语言,那么,不可避免的,该元语言会有缺陷
    * 特殊语义的标记:为特定编程语言定义的元语言要使用该语言的特性来简化和定义,从而,元语言不能被其它编程程使用
    * 没有通用的句法处理器:多种不同的句法元语言限制了使用计算机程序统一分析、处理语句,比如:列出句法,为句法中的标识符建立索引,进行语法检查等等
在实践中,经验丰富的读者可以轻易的学习并鉴别一种新的标记,但这些标记的差异会阻碍彼此的理解和沟通。标准的元语言使得人们将模糊的想法具体为明确的定义。也让其他需要提供正式概念定义的人不用再重新发明类似的概念。

四、标准句法元语言的目标

理想的标准的句法元语言应该是:
    * 简洁的:以便简明的定义语言,且易于理解
    * 精确的:以便规则是明确的
    * 正式的:以便计算机解析、处理这些规则
    * 自然的:以便其他人易于学习并理解语言的标记和格式
    * 通用的:以便标记可以统一的描述不同的语言
    * 简单的:简单的字符集,尽量避免标准键盘(打字机和计算机终端)通常不提供的字符,以便键入规则,并被计算机程序处理
    * 可自我描述的:以便标记可以描述自身
    * 线性的:以便句法可以表示为单个字符流(这简化了句法的打印,且计算机易于处理句法)

五、常用的句法元语言

以下列出几种常用但不符合标谁的元语言:
    * COBOL 
    * Backus-Naur Form 
    * FORTRAN 77

六、标准句法元语言EBNF

EBNF是国际标准组织ISO定义的标准句法元语言,基于BNF,并做了许多扩展:
    * EBNF的终结符被严格的包围在引号 (“…” 或 ‘…’) 中
    * […]表示可选符
    * {…}表示重复符
    * 每个规则有显式的终止字符,以避免规则结束时岐义
    * (…)表示分组符,把多个规则组合在一起
    * (**)表示注释符
    * ?..?表示扩展符
    * 元标识符:元标识符(非终结符的名字)不必是某个单词,也不必用括号括起来,因为有显式的连接符

七、EBNF的限制及扩展

EBNF的主要局限性在于所定义的语言必须是线性的,即该语言语句中的标识符可以按顺序排列。
EBNF的另一个局限性是不足以定义更复杂的语法形式,因为EBNF的主要目的是定义一个更简单、更通用的标识符。
EBNF可以通过special-sequence concept进行扩展,更多复杂的扩展参见附录。

八、信息技术句法元语言EBNF

1、EBNF使用范围scope

EBNF指定了线性符号序列的句法,它定义了标记的逻辑结构和图形表示。
EBNF应用在定义编程语言、操作系统命令、数据格式操作等领域。
和其它元语言一样,EBNF也会被滥用作定义一些无法解析和不明确的语言。

2、 引用规范

EBNF引用了下列规范:
ISO 2382-15 : 1985, Data processing — Vocabulary Part 15: Programming languages.
ISO/IEC 646 : 1991, Information technology — ISO 7-bit coded character set for information interchange. ISO/IEC 6429 : 1992, Information technology — Control functions for 7-bit and 8-bit coded character sets.
BS 6154 : 1981, Method of defining — Syntactic metalanguage.

3、定义

为了定义语言的”定义“,EBNF有以下基本概念:

    1. sequence:An ordered list of zero or more items.
    2. subsequence: A sequence within a sequence.
    3. non-terminal symbol: A syntactic part of the language being defined.
    4. meta-identifier: The name of a non-terminal symbol.
    5. start symbol: A non-terminal symbol that is defined by one or more syntax rules but does not occur in any other syntax rule.
    6. sentence: A sequence of symbols that represents the start symbol.
    7. terminal symbol: A sequence of one or more characters forming an irreducible element of a language.

1. 序列:零个或多个item的有序列表
    2. 子序列:序列中的序列
    3. 非终结符: 被定义的语言的句法部分
    4. 元标识符: 非终结符的名称
    5. 起始符: 由一个或多个句法规则定义但未在其它任何句法规则中出现的非终结符
    6. 语句: 表示起始符的一系列符号
    7. 终结符:一个或多个字符的序列,构成一门语言不可拆解的元素

EBNF中的终结符(a terminal symbol)可以称作”终结字符“(a terminal-character),而由EBNF语法定义的语言中的终结符由终结字符串(terminal-string)表示。

4、EBNF句法元素格式

1. 约定:

1. EBNF中的元标识符都可以写成由一个或多个单词并用连接词”-“连在一起的单词
        2. EBNF中的元标识符都是以"-symbol"为结尾的终结符的名称

2. EBNF操作符的正常字符及优先级(由上及下依次降低):
* repetition-symbol                     重复操作符
- except-symbol                         排除操作符
, concatenate-symbol                     串连操作符
| definition-separator-symbol         定义分隔操作符
= defining-symbol                         定义操作符
; terminator-symbol                    终止操作符
3. 下列括号对超越正常优先级:
´        first-quote-symbol                 first-quote-symbol            ´
"        second-quote-symbol             second-quote-symbol            "
(*     start-comment-symbol              end-comment-symbol            *)
(        start-group-symbol              end-group-symbol            )
[         start-option-symbol              end-option-symbol            ]
{         start-repeat-symbol              end-repeat-symbol            {
?         special-sequence-symbol        special-sequence-symbol    ?
4. 句法逻辑结构

EBNF的逻辑结构定义如下,语言由语句构成,注: 句法,即语句的逻辑结构,4.1—4.21:
    1. 句法:语言的语句结构包含一个或多个句法规则
    2. 句法规则:句法规则由一个元标识符(要定义的非终结符的名称)、一个定义操作符、一个定义列表、一个终止符组成
    3. 定义列表:定义列表由一个或多个定义的有序列表组成,定义之间用定义分隔符"|"隔开
    4. 定义:定义由一个或多个句法项的有序列表组成,句法项之间用串连符”,“隔开
    5. 句法项:句法项由一个句法因子(a syntactic-factor)或一个句法例外(a syntactic-exception)组成。

a) a syntactic-factor, or
b) a syntactic-factor followed by an except-symbol followed by a syntactic-exception.

6. 句法例外:句法例外由"一个句法因子+排除操作符"组成,用句法例外表示的符号序列同样可以用不包含排除操作符的句法因子表示。

注:若允许将句法例外用在任意的句法因子上,EBNF可以定义一些上下文语法无关的语言,包括罗素悖论:
        xx = "A" - xx;
"A"是"xx"的示例吗?这样是不可取的,因此,句法例外的形式仅限于可以证明是安全的情况。

7. 句法因子:句法因子由一个句法主体或"一个整数+一个重复操作符+句法主体"组成。

a) an integer followed by a repetition-symbol followed by a syntactic-primary, or
b) a syntactic-primary.

8. 整数:整数由一个或多个十进制数字的有序列表组成
    9. 句法主体(syntactic-primary):句法主体由下列几种语句组成

a) an optional-sequence;        一个可选序列
b) a repeated-sequence;        一个重复序列
c) a grouped-sequence;        一个分组序列
d) a meta-identifier;            一个元标识符
e) a terminal-string;            一个终止字符串
f) a special-sequence;        一个特殊序列
g) an empty-sequence.            一个空序列

10. 可选序列:可选序列由"[ 定义列表 ]“组成
    11. 重复序列:重复序列由”{ 定义列表 }“组成
    12. 分组序列:分组序列由”( 定义列表 )“组成
    13. 元标识符:元标识符由一个或多个元标识字符的有序列表组成,前提是首个元标识字符是字母
    14. 元标识字符:元标识字符由一个字母或一个十进制数成组成
    15. 终结字符串:终结字符串由一个"一个或多个第一终结字符的有序列表"或一个”“一个或多个第二终结字符的有序列表”“组成
    16. 第一终结字符:第一终结字符(first-terminal-character)由单引号以外的任意终结字符组成
    17. 第二终结字符:第二终结字符(second-terminal-character)由双引号以外的做任意终结字符组成
    18. 特殊序列:特殊序列(special-sequence)由”? 特殊字符序列或空序列 ?"组成
    19. 特殊字符序列:特殊字符序列由问号以外的任意终结字符组成
    20. 空序列:空序列(empty-sequence)由终结字符的空序列组成
    21. 实例:ISO 14977的例子没看懂,知道的告诉下

5、各句法元素细则

1. 概述:每个句法规则都是一个定义终结符序列和非终结符序列的句法规则。每一个符号序列都由上段4.x句法规则定义的由元标识符命名的非终结符表示。 5.2-5.12定义了由任何定义列表表示的符号序列。

注:定义完语言的完整句法后,语句形式如下:
1. 一个起始符(a start symbol)
2. 最少一种以元标识符开头的句法规则作为句法主体(syntactic-primary)

2. 终结字符串:终结字符串由"第一终结字符序列"或"“第二终结字符序列”“组成
    3. 元标识符:用作句法主体的元标识符表示以该元标识符开头的任何句法规则的定义列表的符号序列。
    4. 分组序列:分组序列表示”( 定义列表序列 )“
    5. 可选序列:可选序列表示空序列或”[ 定义列表序列 ]“
    6. 重复序列:重复序列表示空序列或”{ 定义列表序列 }“
    7. 句法因子:句法因子表示一定数量的子序列,每个子序列是由作为该句法因子一部分的句法主体表示的符号序列,即"一个整数+一个重复操作符+句法主体”。若没有给定整数值,则子序列数量等于1,否则,子序列的数量等于整数的值

下列句法规则实例演示了重复操作符的用法:

aa = "A"; 
bb = 3 * aa, "B"; 
cc = 3 * [aa], "C"; 
dd = {aa}, "D"; 
ee = aa, {aa}, "E"; 
ff = 3 * aa, 3 * [aa], "F"; 
gg = 3 * {aa}, "D";

上述规则表示的终结字符串如下:

aa:     A
bb:     AAAB
cc:    C AC AAC AAAC 
dd:     D AD AAD AAAD AAAAD etc.
ee:     AE AAE AAAE AAAAE AAAAAE etc.
ff:    AAAF AAAAF AAAAAF AAAAAAF etc.

NOTE — The definition for gg, although syntactically valid, is not sensible. The sequences of symbols represented by gg are identical with those given by dd but cannot be parsed unambiguously.

8. 句法项:当句法项是单个句法因子时,它代表该句法因子表示的任何符号序列。

当句法项由”句法因子+排除操作符“组成时,该句法例外表示的符号序列必须满足以下条件:
a) 它是该句法因子表示的符号序列
b) 它不是句法例外表示的符号序列

以下句法规则实例演示了except-symbol的用法:

letter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"; 
vowel = "A" | "E" | "I" | "O" | "U"; 
consonant = letter - vowel; 
ee = {"A"}-, "E";

上述规则表示的终结字符串如下:
letter: A B C D E F G H I J etc.
vowel: A E I O U 
consonant: B C D F G H J K M N etc.
ee: AE AAE AAAE AAAAE etc.

注:{"A"}- 表示一个或多个A的序列,因为它是一个”空句法例外“的句法项。

9. 定义:定义表示一个或多个子序列的序列,每个子序列是由该定义中相应句法项表示的符号序列
    10. 定义列表:句法列表表示由构成该定义列表的所有定义所表示的任何符号序列
    11. 特殊序列:特殊序列所表示的符号序列超出本标准规定的范围。特殊序列用于扩展标识符。
    12. 空序列:空序列表示空符号序列

6、 布局和注释

1. 概述:页面上句法的布局几乎完全是任意的。6.2-6.4定义,若非印刷字符(non-printing character)(比如空格或换行符)不在终结字符串或成对字符内,则对句法没有形式上的影响。6.5-6.7定义在句法中可以插入任意文本作为注释。

注:
1 若每个句法规则都从新行开始,且每个元语言符号均合理的展开,其他人阅读和理解语句时就容易的多。
2 EBNF定义的语言的词汇规则可能与EBNF本身完全不同

3 插入解释性文本到语法中可以使其他人理解语法。比如,可以对语法规则编号,并在每个元标识符后加上注释,以确定该句法规则定义的位置。建议有关语法规则的任何注释都应出现在规则的终止符之前。
4 注释对于句法定义的语言没有任何影响。

2. 终结字符:EBNF中的终结字符由以下组成:

a) a letter;
b) a decimal-digit;
c) a concatenate-symbol;
d) a defining-symbol;
e) a definition-separator-symbol;
f) an end-comment-symbol;
g) an end-group-symbol;
h) an end-option-symbol;
i) an end-repeat-symbol;
j) an except-symbol;
k) a first-quote-symbol;
l) a repetition-symbol;
m) a second-quote-symbol;
n) a special-sequence-symbol;
o) a start-comment-symbol;
p) a start-group-symbol;
q) a start-option-symbol;
r) a start-repeat-symbol;
s) a terminator-symbol;
t) an other-character.

3. 无间隙符:无间隙符(gap-free-symbol)由非单引号和非双引号的终结字符或终结字符串组成

a) a terminal-character that is neither a first-quotesymbol nor a second-quote-symbol, or
b) a terminal-string.

4. 间隔符:分割符(gap-separator)是以下非印刷字符之一: 空格、换行、水平制表符、垂直制表符、换页符。

一个或多个间隔符的位置:
a) before a syntax, and
b) between any two gap-free-symbols of a syntax, and
c) after a syntax without affecting the language defined by the syntax.

5. 非注释符:非注释符( commentless-symbol)如下:

a) a terminal-character that is neither a letter nor a decimal-digit nor a first-quote-symbol nor a second-quote-symbol nor a start-comment-symbol nor an end-comment-symbol nor a special-sequence-symbol nor an other-character;

b) a meta-identifier;
c) an integer;
d) a terminal-string;
e) a special-sequence.

6. 注释符:注释符(comment-symbol)如下:

a) a bracketed-textual-comment;        //括号内的文字评论
b) a commentless-symbol;                //非注释符
c) an other-character.                //其它的字符

7. 括号文本注释:括号文本注释(bracketed-textual-comment)由”(* 非注释符序列 *)“组成

注释可以出现但不影响语法的位置:
a) before a syntax, and    //语句开始前
b) between any two commentless-symbols of a syntax, and        //语句的非注释符之间
c) after a syntax            //语句开始后

注释不能出现的位置:
a) a meta-identifier;
b) an integer;
c) a special-sequence;
d) a terminal-string.

7、 EBNF字符集

1. 概述:7.2-7.8定义的每个EBNF终结符(terminal-character)和间隙符(gap-separator)都使用7bit的字符集,该字符集由(ISO/IEC 646)定义。
    2. 字母和数字:每个字母和十进制数字都由相应的字符表示。
    3. 其它终结字符:Table 1定义了非字母非数字的其它终结字符(Other terminal characters)
    4. 可选终结字符:Table 2定义了某些终结字符的替代字符(alternative character)

Table 1 — 终结字符
Metalanguage symbol                Normal representation
concatenate-symbol                     , comma
defining-symbol                         = equals sign
definition-separator-symbol             | vertical line
end-comment-symbol                    *) asterisk,right parenthesis
end-group-symbol                         ) right parenthesis
end-option-symbol                     ] right square bracket
end-repeat-symbol                     } right curly bracket
except-symbol                         - hyphen-minus
first-quote-symbol                     ´apostrophe
repetition-symbol                     * asterisk
second-quote-symbol                     " quotation mark
special-sequence-symbol                 ? question mark
start-comment-symbol                    (* left parenthesis,asterisk
start-group-symbol                     ( left parenthesis
start-option-symbol                     [ left square bracket
start-repeat-symbol                     { left curly bracket
terminator-symbol                        ; semicolon

Table 2 — 终结字符的替代字符
Metalanguage symbol                    Alternative representation
definition-separator-symbol                / solidus
definition-separator-symbol                ! exclamation mark
end-option-symbol                            /) solidus,right parenthesis
end-repeat-symbol                            :) colon,right parenthesis
start-option-symbol                        (/ left parenthesis,solidus
start-repeat-symbol                        (: left parenthesis,colon
terminator-symbol                            . full stop

NOTES
1 指定终结字符的替代字符原因是有些计算机和打印机不支持Table 1 所列出的终结字符。
2 为避免岐义,任何文档内终结字符的使用应保持一致。
3 
3 7.2-7.4表示EBNF要求的字符:
letters digits = , - * ( ) ?
| or / or !
/ or both of [ ] 
: or both of { } 
´or " (Both characters are needed if either is a terminal symbol of the language being defined)

5. 其它字符:EBNF中的其它字符(other-character)是ISO/IEC 646:1991定义的字符集中的控制字符或其它终结符:

a)控制字符: a control character            
b)其它终结符: required to represent any other terminal-character

注:Table 1定义的终结字符外,其它字符如下:
    space
. full stop
: colon
! exclamation mark
+ plus sign
_    lowline
% percent sign
@ commercial at
& ampersand
# number sign
$ dollar sign
< less-than sign
> greater-than sign
/ solidus
\ reverse solidus
ˆ circumflex accent
` grave accent
˜    tilde

6. 间隔符:间隔符如下:

a)空格: a space is represented by a Space character,
b)水平制表符: a horizontal-tabulation is represented by a Horizontal Tabulation character,

Table 3 — 表示终结符的符号对:
(* 
*)
(:
:)
(/ 
/)

Table 4 — 无效的字符
(*) 
(:) 
(/)

c)换行符: a new-line is represented by a (possibly empty) sequence of Carriage Return characters, a Line Feed character, and a (possibly empty) sequence of Carriage Return characters,

d)垂直制表符: a vertical-tabulation is represented by a Vertical Tabulation character,

e)分页符: a form-feed is represented by a Form Feed character.

7. 成对终结符:Table 3定义的成对终结符(terminal-characters represented by a pair of characters)在句法上表示一个单个终结符,成对终结符不能出现在终结字符串(terminal-string)和特殊序列(special-sequence)中。

NOTE — This restriction is necessary because these character sequences are ambiguous, for example /) could be a definitionseparator-symbol followed by an end-group-symbol, or an end-option-symbol.

8. 无效字符序列:Table 4 定义的无效字符序列只能出现在终结字符串(terminal-string)和特殊序列(special-sequence)中。

注:上述限制是非常必要的,因为这些字符序列意义不明确。比如(*)可能被误解为"一个注释起始符+一个分组结束符"或"一个分组起始符+注释结束符"
在无效字符序列添加间隔符有特殊的意义,比如(* )表示"一个注释起始符+一个分组结束符",而( *)表示"一个分组起始符+注释结束符"

8、 EBNF实例

1. 8.1 EBNF句法

(*
The syntax of Extended BNF can be defined using itself. There are four parts in this example, 
the first part names the characters, 
the second part defines the removal of unnecessary nonprinting characters, 
the third part defines the removal of textual comments, 
and the final part defines the structure of Extended BNF itself.

Each syntax rule in this example starts with a comment that identifies the corresponding clause in the standard.

The meaning of special-sequences is not defined in the standard. In this example (see the reference to 7.6) they represent control functions defined by ISO/IEC 6429:1992.

Another special-sequence defines a syntactic-exception (see the reference to 4.7).
*)

(*
The first part of the lexical syntax defines the characters in the 7-bit character set (ISO/IEC 646:1991) that represent each terminal-character and gap-separator in Extended BNF.
*)

(* see 7.2 *) letter
= 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h'
| 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p'
| 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x'
| 'y' | 'z'
| 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H'
| 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P'
| 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X'
| 'Y' | 'Z';

(* see 7.2 *) decimal digit
= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7'
| '8' | '9';

(*
The representation of the following terminal-characters is defined in clauses 7.3,7.4 and tables 1, 2.
*)
concatenate symbol = ',';
defining symbol = '='; 
definition separator symbol = '|' | '/' | '!'; 
end comment symbol = '*)'; 
end group symbol = ')'; 
end option symbol = ']' | '/)'; 
end repeat symbol = '}' | ':)'; 
except symbol = '-'; 
first quote symbol = "'"; 
repetition symbol = '*'; 
second quote symbol = '"'; 
special sequence symbol = '?'; 
start comment symbol = '(*'; 
start group symbol = '('; 
start option symbol = '[' | '(/'; 
start repeat symbol = '{' | '(:'; 
terminator symbol = ';' | '.';

(* see 7.5 *) other character
= ' ' | ':' | '+' | '_' | '%' | '@'
| '&' | '#' | '$' | '<' | '>' | '\'
| 'ˆ' | '‘' | '˜'; 

(* see 7.6 *) space character = ' ';

horizontal tabulation character = ? ISO 6429 character Horizontal Tabulation ? ; 

new line = { ? ISO 6429 character Carriage Return ? },
 ? ISO 6429 character Line Feed ?,
 { ? ISO 6429 character Carriage Return ? }; 

vertical tabulation character = ? ISO 6429 character Vertical Tabulation ? ; 

form feed = ? ISO 6429 character Form Feed ? ;

(*
The second part of the syntax defines the removal of unnecessary non-printing characters from a syntax.
*) 

(* see 6.2 *) terminal character 
= letter 
| decimal digit 
| concatenate symbol 
| defining symbol 
| definition separator symbol 
| end comment symbol 
| end group symbol 
| end option symbol 
| end repeat symbol 
| except symbol 
| first quote symbol 
| repetition symbol 
| second quote symbol 
| special sequence symbol 
| start comment symbol 
| start group symbol 
| start option symbol 
| start repeat symbol 
| terminator symbol 
| other character;

(* see 6.3 *) gap free symbol 
= terminal character - (first quote symbol 
| second quote symbol) 
| terminal string; 

(* see 4.16 *) terminal string 
= first quote symbol, first terminal character, {first terminal character}, first quote symbol 
| second quote symbol, second terminal character, {second terminal character}, second quote symbol; 

(* see 4.17 *) first terminal character = terminal character - first quote symbol; 

(* see 4.18 *) second terminal character = terminal character - second quote symbol; 

(* see 6.4 *) gap separator = space character 
| horizontal tabulation character 
| new line 
| vertical tabulation character 
| form feed;


(* see 6.5 *) syntax = {gap separator}, gap free symbol, {gap separator}, {gap free symbol, {gap separator}};

(*
The third part of the syntax defines the removal of bracketed-textual-comments from gap-free-symbols that form a syntax.
*)

(* see 6.6 *) commentless symbol= terminal character - (letter
        | decimal digit 
        | first quote symbol 
        | second quote symbol 
        | start comment symbol 
        | end comment symbol 
        | special sequence symbol 
        | other character) 
| meta identifier 
| integer 
| terminal string 
| special sequence; 

(* see 4.9 *) integer = decimal digit, {decimal digit}; 

(* see 4.14 *) meta identifier = letter, {meta identifier character}; 

(* see 4.15 *) meta identifier character = letter | decimal digit; 

(* see 4.19 *) special sequence = special sequence symbol, {special sequence character}, special sequence symbol; 

(* see 4.20 *) special sequence character = terminal character - special sequence symbol; 

(* see 6.7 *) comment symbol = bracketed textual comment | other character | commentless symbol; 

(* see 6.8 *) bracketed textual comment = start comment symbol, {comment symbol}, end comment symbol; 

(* see 6.9 *) syntax = {bracketed textual comment}, commentless symbol, {bracketed textual comment}, {commentless symbol, {bracketed textual comment}};

(*
The final part of the syntax defines the abstract syntax of Extended BNF, i.e. the structure in terms of the commentless symbols.
*)

(* see 4.2 *) syntax = syntax rule, {syntax rule}; 

(* see 4.3 *) syntax rule = meta identifier, defining symbol, definitions list, terminator symbol; 

(* see 4.4 *) definitions list = single definition, {definition separator symbol, single definition}; 

(* see 4.5 *) single definition = syntactic term, {concatenate symbol, syntactic term}; 

(* see 4.6 *) syntactic term = syntactic factor, [except symbol, syntactic exception]; 

(* see 4.7 *) syntactic exception = ? a syntactic-factor that could be replaced by a syntactic-factor containing no meta-identifiers ? ; 

(* see 4.8 *) syntactic factor = [integer, repetition symbol], syntactic primary; 

(* see 4.10 *) syntactic primary 
= optional sequence 
| repeated sequence 
| grouped sequence
| meta identifier 
| terminal string 
| special sequence 
| empty sequence; 

(* see 4.11 *) optional sequence = start option symbol, definitions list, end option symbol; 

(* see 4.12 *) repeated sequence = start repeat symbol, definitions list, end repeat symbol;

(* see 4.13 *) grouped sequence = start group symbol, definitions list, end group symbol;

(* see 4.21 *) empty sequence = ;

2. EBNF非正式定义:

(* This example defines Extended BNF informally. 
Many of the syntax rules include a comment to explain their meaning; inside a comment a meta identifier is enclosed in angle brackets < and > to avoid confusion with similar English words. 
The non-terminal symbols <letter>, <decimal digit> and <character> are not defined. 
The position of <comments> is stated in a comment but not formally defined. *) 

syntax = syntax rule, {syntax rule}; 
syntax rule = meta identifier, '=', definitions list, ';' 
(* A <syntax rule> defines the sequences of symbols represented by a <meta identifier> *); 

definitions list = single definition, {'|', single definition} 
(* | separates alternative <single definitions> *); 

single definition = term, {',', term} 
(* , separates successive <terms> *); 

term = factor, ['-', exception] 
(* A <term> represents any sequence of symbols that is defined by the <factor> but not defined by the <exception> *); 

exception = factor 
(* A <factor> may be used as an <exception> if it could be replaced by a <factor> containing no <meta identifiers> *); 

factor = [integer, '*'], primary 
(* The <integer> specifies the number of repetitions of the <primary> *); 

primary = optional sequence | repeated sequence | special sequence | grouped sequence | meta identifier | terminal string | empty; 

empty = ; 

optional sequence = '[', definitions list, ']' 
(* The brackets [ and ] enclose symbols which are optional *); 

repeated sequence = '{', definitions list, '}' 
(* The brackets { and } enclose symbols which may be repeated any number of times *); 

grouped sequence = '(', definitions list, ')' 
(* The brackets ( and ) allow any <definitions list> to be a <primary> *); 

terminal string = "'", character - "'", {character - "'"}, "'" | '"', character - '"', {character - '"'}, '"' 
(* A <terminal string> represents the <characters> between the quote symbols '_' or "_" *); 

meta identifier = letter, {letter | decimal digit} 
(* A <meta identifier> is the name of a syntactic element of the language being defined *); 

integer = decimal digit, {decimal digit}; 

special sequence = '?', {character - '?'}, '?' 
(* The meaning of a <special sequence> is not defined in the standard metalanguage. *); 

comment = '(*', {comment symbol}, '*)' 
(* A comment is allowed anywhere outside a <terminal string>, <meta identifier>, <integer> or <special sequence> *); 

comment symbol = comment | terminal string | special sequence | character;

3. EBNF非正式定义:

(* THIS EXAMPLE USES THE REPRESENTATION DEFINED IN TABLE 2. *) 
SYNTAX = SYNTAX RULE, (: SYNTAX RULE :). 

SYNTAX RULE = META IDENTIFIER, '=', DEFINITIONS LIST, '.'. 

DEFINITIONS LIST = SINGLE DEFINITION,(: '/', SINGLE DEFINITION :).

SINGLE DEFINITION = TERM, (: ',', TERM :). 

TERM = FACTOR, (/ '-', EXCEPTION /). 

EXCEPTION = FACTOR.

FACTOR = (/ INTEGER, '*' /), PRIMARY. 

PRIMARY = OPTIONAL SEQUENCE / REPEATED SEQUENCE / SPECIAL SEQUENCE / GROUPED SEQUENCE / META IDENTIFIER / TERMINAL / EMPTY.

EMPTY = .

OPTIONAL SEQUENCE = '(/', DEFINITIONS LIST, '/)'. 

REPEATED SEQUENCE = '(:', DEFINITIONS LIST, ':)'. 

GROUPED SEQUENCE = '(', DEFINITIONS LIST, ')'. 

TERMINAL = "'", CHARACTER - "'", (: CHARACTER - "'" :), "'" / '"', CHARACTER - '"', (: CHARACTER - '"' :), '"'.

META IDENTIFIER = LETTER, (: LETTER / DIGIT :). INTEGER = DIGIT, (: DIGIT :).

SPECIAL SEQUENCE = '?', (: CHARACTER - '?' :), '?'. 

COMMENT = '(*', (: COMMENT SYMBOL :), '*)'. 

COMMENT SYMBOL = COMMENT / TERMINAL / SPECIAL SEQUENCE / CHARACTER.

iso-14977

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值