gpt4 book ai didi

repeat - 如何在EBNF中表示至少一个重复?

转载 作者:行者123 更新时间:2023-12-04 14:20:34 24 4
gpt4 key购买 nike

https://en.wikipedia.org/wiki/Extended_Backus–Naur_form

上面的文章提到花括号表示任意次数的重复(包括零次),而方括号表示最多重复一次。

然而,我想要的是至少一次重复——也就是说,终端或非终端必须至少出现一次。

好吧,我可以这样描述它:

production = nonterminal, { nonterminal };

但我认为 EBNF 优于 BNF 的重点是避免需要这种“黑客”。

维基百科文章还提到:

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.



但是 EBNF 是否提供了描述至少一次重复的语法?

最佳答案

在最后一个大括号后放置一个减号(除符号)。

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.



请注意,在第二段中(强调),满足两个条件。也就是说,必须同时满足句法因素和句法异常(exception)。大括号仍然意味着重复。这导致一个或多个满足语法,即使异常为空。

关于repeat - 如何在EBNF中表示至少一个重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55629053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com