简体   繁体   English

如何在 BNF 中表示否定?

[英]How to represent negation in BNF?

Does BNF or ABNF support negation. BNF 或 ABNF 是否支持否定。 That is exclude certain members of the set?那是排除集合中的某些成员吗? I did not see any such negation operator in its syntax.我在它的语法中没有看到任何这样的否定运算符。

For example, suppose S is the set of all alphanumeric strings that are not equal to "foo" What is the BNF for S ?例如,假设S不等于"foo"的所有字母数字字符串的集合。 S的 BNF 是什么?

Context free grammars are not closed under "difference" or "complements".上下文无关文法不会在“差异”或“补语”下关闭。 So while you might decide to add an operator "subtract" to your BNF, the result will not be a context free grammar even if it has a simple way to express it.因此,虽然您可能决定向 BNF 添加运算符“减法”,但结果不会是上下文无关文法,即使它有一种简单的表达方式。 Consequence: people don't allow such operators in BNF grammars used to express context-free grammars.结果:人们不允许在用于表达上下文无关文法的 BNF 文法中使用此类运算符。

While not in BNF, EBNF does have the except-symbol (typically defined as "-").虽然不在 BNF 中,但 EBNF 确实有异常符号(通常定义为“-”)。 In your case, the syntax would be:在您的情况下,语法将是:

alphaNum="a"|"b"|...|"z"|"0"|"1"|...|"9"|"A"|...|"Z";
S= (alphaNum,{alphaNum}) - "foo";

Or if you want it to be case insensitive:或者,如果您希望它不区分大小写:

foo="f"|"F","o"|"O","o"|"O";
alphaNum="a"|"b"|...|"z"|"0"|"1"|...|"9"|"A"|...|"Z";
S= (alphaNum,{alphaNum}) - foo;

This results in a slightly different acceptance criteria than was done in the comments which would be equivalent to:这导致与评论中所做的接受标准略有不同,这相当于:

alphaNum="a"|"b"|...|"z"|"0"|"1"|...|"9";
S= alphaNum - "f", {alphaNum} 
  |"f", alphaNum - "o", {alphaNum}
  |"f", "o", alphaNum - "o", {alphaNum};

This leaves out the strings "f" and "fo".这遗漏了字符串“f”和“fo”。

However, it's important to note as Ira Baxter has in their answer, allowing anything as the excepted (negated) factor would cause problems.但是,重要的是要注意 Ira Baxter 在他们的回答中,允许任何例外(否定)因素都会导致问题。 This is also pointed out in the ISO standard : ISO标准中也指出了这一点:

4.7 Syntactic exception 4.7 语法异常

A syntactic-exception consists of a syntactic-factor subject to the restriction that the sequences of symbols represented by the syntactic-exception could equally be represented by a syntactic-factor containing no meta-identifiers.一个句法异常由一个句法因子组成,该限制是由该句法异常表示的符号序列同样可以由不包含元标识符的句法因子表示。

NOTE - If a syntactic-exception is permitted to be an arbitrary syntactic-factor, Extended BNF could define a wider class of languages than the context-free grammars, including attempts which lead to Russell-like paradoxes, eg注意- 如果允许句法异常是任意句法因素,扩展 BNF可以定义比上下文无关文法更广泛的语言类别,包括导致类似罗素悖论的尝试,例如

xx = "A" - xx;

Is "A" an example of xx? “A”是 xx 的一个例子吗? Such licence is undesirable and the form of a syntactic-exception is therefore restricted to cases that can be proved to be safe.这种许可是不可取的,因此语法异常的形式仅限于可以证明是安全的情况。 Thus whereas a syntactic-factor is in general equivalent to some context-free grammar, a syntactic-exception is always equivalent to some regular grammar.因此,句法因素通常等同于某些上下文无关文法,而句法异常总是等同于某些常规文法。 It may be shown that the difference between a context-free grammar and a regular grammar is always another context-free grammar;可以证明,上下文无关文法和正则文法之间的区别总是另一种上下文无关文法; hence a syntactic-term (and hence any grammar defined according to this standard) is equivalent to some context-free grammar.因此,句法术语(以及根据该标准定义的任何文法)等价于一些上下文无关文法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM