问题描述
https://en.wikipedia.org/wiki/Extended_Backus–Naur_form
以上文章提到花括号表示任意次重复(包括零),而方括号表示最多重复1次.
The above article mentions that curly braces denote repetition of arbitrary times (incl. zero), while square brackets denote at most one repetition.
但是我想要的是至少一个重复-也就是说,一个终端或一个非终端必须至少出现一次.
What I want however, is at least one repetition - that is, a terminal or a nonterminal must appear at least once.
我可以这样形容它:
production = nonterminal, { nonterminal };
但是我认为EBNF优于BNF的目的是避免这种"hacks"的需要.
But I thought the point of EBNF over BNF was to avoid the need of this kind of "hacks".
Wikipedia文章还提到:
The Wikipedia article also mentions:
但是EBNF是否提供描述至少一个重复的语法?
But does EBNF provide the syntax to describe at least one repetition?
推荐答案
在最后一个花括号后放置一个减号(符号除外).
Place a minus (except-symbol) after the final brace.
production = { nonterminal }-;
ISO/IEC 14977:1996(E)
ISO/IEC 14977 : 1996(E)
当一个句法术语是一个单独的句法因素时,它代表任何 该句法因子表示的符号序列.
When a syntactic-term is a single syntactic-factor it represents any sequence of symbols represented by that syntactic-factor.
当句法术语是句法因素后跟一个 除符号后跟语法异常,它表示任何 满足两个条件的符号序列:
When a syntactic-term is a syntactic-factor followed by an except-symbol followed by a syntactic-exception it represents any sequence of symbols that satisfies both of the conditions:
a)它是由句法因子表示的符号序列,
a) it is a sequence of symbols represented by the syntactic-factor,
b)它不是由 句法例外.
b) it is not a sequence of symbols represented by the syntactic-exception.
作为示例,以下语法规则说明了这些功能 由例外符号提供.
As examples the following syntax-rules illustrate the facilities provided by the except-symbol.
字母="A" | "B" | "C" | "D" | "E" | "F"
| "G" | "H" | 我" | "J" | "K" | "L" | "M"
| "N" | "O" | "P" | "Q" | "R" | "S" | "T"
| "U" | "V" | "W" | "X" | "Y" | "Z";
元音="A" | "E" | 我" | "O" |"U";
辅音=字母-元音;
ee = {"A"}-,"E";
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";
这些规则定义的终端字符串如下:
Terminal-strings defined by these rules are as follows:
字母:A B C D E F G H I J等.
元音:A E I O U
辅音:B C D F G H J K L M等.
ee:AE AAE AAAE AAAAE AAAAAE等.
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 L M etc.
ee: AE AAE AAAE AAAAE AAAAAE etc.
注意— {"A"}-代表一个或多个A的序列,因为它是一个 句法项带有空的句法例外.
NOTE — {"A"}- represents a sequence of one or more A’s because it is a syntactic-term with an empty syntactic-exception.
请注意,在第二段(添加了重点)中,满足两个条件.也就是说,必须同时满足 syntactic-factor 和 syntactic-exception .大括号仍然意味着重复.即使异常为空,也会导致一个或多个满足语法要求.
Note that in the second paragraph (emphasis added), satisfies both of the conditions. That is, both the syntactic-factor and the syntactic-exception must be satisfied. The braces still mean repetition. This results in one or more to satisfy the syntax, even though the exception is empty.
这篇关于如何在EBNF中至少表示一次重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!