问题
https://en.wikipedia.org/wiki/Extended_Backus–Naur_form
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.
Well I can describe it like that:
production = nonterminal, { nonterminal };
But I thought the point of EBNF over BNF was to avoid the need of this kind of "hacks".
The Wikipedia article also mentions:
EBNF also provides, among other things, the syntax to describe repetitions (of a specified number of times), to exclude some part of a production, and to insert comments in an EBNF grammar.
But does EBNF provide the syntax to describe at least one repetition?
回答1:
Place a minus (except-symbol) after the final brace.
production = { nonterminal }-;
ISO/IEC 14977 : 1996(E)
5.8 Syntactic-term
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) it is a sequence of symbols represented by the syntactic-factor,
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.
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:
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.NOTE — {"A"}- represents a sequence of one or more A’s because it is a syntactic-term with an empty 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.
来源:https://stackoverflow.com/questions/55629053/how-to-denote-at-least-one-repetition-in-ebnf