C++ 是上下文无关的还是上下文敏感的? [英] Is C++ context-free or context-sensitive?

查看:24
本文介绍了C++ 是上下文无关的还是上下文敏感的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到声称 C++ 是一种上下文敏感的语言.举个例子:

I often hear claims that C++ is a context-sensitive language. Take the following example:

a b(c);

这是变量定义还是函数声明?这取决于符号 c 的含义.如果c 是一个变量,则ab(c); 定义一个名为b 类型的变量一个.它直接用 c 初始化.但是如果 c 是一个 type,那么 ab(c); 声明一个名为 b 的函数,它接受一个 c 并返回一个 a.

Is this a variable definition or a function declaration? That depends on the meaning of the symbol c. If c is a variable, then a b(c); defines a variable named b of type a. It is directly initialized with c. But if c is a type, then a b(c); declares a function named b that takes a c and returns an a.

如果您查看上下文无关语言的定义,它基本上会告诉您所有语法规则的左侧都必须由一个非终结符组成.另一方面,上下文相关文法允许在左侧使用任意的终结符和非终结符字符串.

If you look up the definition of context-free languages, it will basically tell you that all grammar rules must have left-hand sides that consist of exactly one non-terminal symbol. Context-sensitive grammars, on the other hand, allow arbitrary strings of terminal and non-terminal symbols on the left-hand side.

浏览C++ 编程语言"的附录 A,我找不到一条语法规则除了左侧的单个非终结符之外还有其他任何内容.这意味着 C++ 是上下文无关的.(当然,每个上下文无关语言也是上下文敏感的,因为上下文无关语言构成上下文敏感语言的一个子集,但这不是重点.)

Browsing through Appendix A of "The C++ Programming Language", I couldn't find a single grammar rule that had anything else besides a single non-terminal symbol on its left-hand side. That would imply that C++ is context-free. (Of course, every context-free language is also context-sensitive in the sense that the context-free languages form a subset of the context-sensitive languages, but that is not the point.)

那么,C++ 是上下文无关的还是上下文敏感的?

So, is C++ context-free or context-sensitive?

推荐答案

以下是我(当前)最喜欢的为什么解析 C++ 的演示(可能)图灵完备,因为它显示了一个程序,当且仅当给定整数是素数时,该程序在语法上是正确的.

Below is my (current) favorite demonstration of why parsing C++ is (probably) Turing-complete, since it shows a program which is syntactically correct if and only if a given integer is prime.

所以我断言C++ 既不是上下文无关的,也不是上下文敏感的.

如果在任何产生式的两侧都允许任意符号序列,则在 乔姆斯基层次结构,比上下文相关的语法更强大;无限制语法是图灵完备的.上下文敏感(Type-1)语法允许在产生式的左侧出现多个上下文符号,但相同的上下文必须出现在产生式的右侧(因此名称为上下文敏感").[1] 上下文相关语法相当于线性有界图灵机.

If you allow arbitrary symbol sequences on both sides of any production, you produce an Type-0 grammar ("unrestricted") in the Chomsky hierarchy, which is more powerful than a context-sensitive grammar; unrestricted grammars are Turing-complete. A context-sensitive (Type-1) grammar allows multiple symbols of context on the left hand side of a production, but the same context must appear on the right hand side of the production (hence the name "context-sensitive"). [1] Context-sensitive grammars are equivalent to linear-bounded Turing machines.

在示例程序中,素数计算可以由线性有界图灵机进行,因此并不能完全证明图灵等价,但重要的部分是解析器需要进行计算才能进行句法分析.它可以是任何可表达为模板实例化的计算,并且有充分的理由相信 C++ 模板实例化是图灵完备的.例如,参见 Todd L. Veldhuizen 2003 年的论文.

In the example program, the prime computation could be performed by a linear-bounded Turing machine, so it does not quite prove Turing equivalence, but the important part is that the parser needs to perform the computation in order to perform syntactic analysis. It could have been any computation expressible as a template instantiation and there is every reason to believe that C++ template instantiation is Turing-complete. See, for example, Todd L. Veldhuizen's 2003 paper.

不管怎样,C++可以被计算机解析,当然也可以被图灵机解析.因此,不受限制的语法可以识别它.实际上编写这样的语法是不切实际的,这就是标准不尝试这样做的原因.(见下文.)

Regardless, C++ can be parsed by a computer, so it could certainly be parsed by a Turing machine. Consequently, an unrestricted grammar could recognize it. Actually writing such a grammar would be impractical, which is why the standard doesn't try to do so. (See below.)

某些表达的歧义"问题主要是一个红鲱鱼.首先,歧义是特定语法的特征,而不是语言.即使可以证明一种语言没有明确的语法,如果它可以被上下文无关文法识别,那么它就是上下文无关的.同样,如果它不能被上下文无关文法识别但可以被上下文敏感文法识别,则它是上下文敏感的.歧义是不相关的.

The issue with "ambiguity" of certain expressions is mostly a red herring. To start with, ambiguity is a feature of a particular grammar, not a language. Even if a language can be proven to have no unambiguous grammars, if it can be recognized by a context-free grammar, it's context-free. Similarly, if it cannot be recognized by a context-free grammar but it can be recognized by a context-sensitive grammar, it's context-sensitive. Ambiguity is not relevant.

但无论如何,就像下面程序中的第 21 行(即 auto b = foo>::typen<1>();),表达式不是模棱两可;它们只是根据上下文进行不同的解析.在这个问题的最简单表达中,某些标识符的句法类别取决于它们的声明方式(例如类型和函数),这意味着形式语言必须认识到以下事实:两个任意长度的字符串在相同的程序是相同的(声明和使用).这可以通过复制"语法建模,该语法识别同一个单词的两个连续的精确副本.用抽引引理很容易证明这种语言不是上下文无关的.这种语言的上下文敏感语法是可能的,并且在这个问题的答案中提供了一个 Type-0 语法:https://math.stackexchange.com/questions/163830/context-sensitive-grammar-for-the-copy-language .

But in any event, like line 21 (i.e. auto b = foo<IsPrime<234799>>::typen<1>();) in the program below, the expressions are not ambiguous at all; they are simply parsed differently depending on context. In the simplest expression of the issue, the syntactic category of certain identifiers is dependent on how they have been declared (types and functions, for example), which means that the formal language would have to recognize the fact that two arbitrary-length strings in the same program are identical (declaration and use). This can be modelled by the "copy" grammar, which is the grammar which recognizes two consecutive exact copies of the same word. It's easy to prove with the pumping lemma that this language is not context-free. A context-sensitive grammar for this language is possible, and a Type-0 grammar is provided in the answer to this question: https://math.stackexchange.com/questions/163830/context-sensitive-grammar-for-the-copy-language .

如果有人试图编写一种上下文敏感(或不受限制)的语法来解析 C++,它很可能会用涂鸦填满整个宇宙.编写一个图灵机来解析 C++ 将是一项同样不可能的任务.甚至编写 C++ 程序也很困难,据我所知,没有一个程序被证明是正确的.这就是为什么该标准不尝试提供完整的形式语法,而选择用技术英语编写一些解析规则的原因.

If one were to attempt to write a context-sensitive (or unrestricted) grammar to parse C++, it would quite possibly fill the universe with scribblings. Writing a Turing machine to parse C++ would be an equally impossible undertaking. Even writing a C++ program is difficult, and as far as I know none have been proven correct. This is why the standard does not attempt to provide a complete formal grammar, and why it chooses to write some of the parsing rules in technical English.

在 C++ 标准中看起来像形式语法的东西并不是 C++ 语言语法的完整形式定义.它甚至不是预处理后语言的完整正式定义,这可能更容易形式化.(虽然这不是语言:标准定义的 C++ 语言包括预处理器,并且预处理器的操作是通过算法描述的,因为在任何语法形式主义中都很难描述.它在该部分描述词法分解的标准,包括必须多次应用的规则.)

What looks like a formal grammar in the C++ standard is not the complete formal definition of the syntax of the C++ language. It's not even the complete formal definition of the language after preprocessing, which might be easier to formalize. (That wouldn't be the language, though: the C++ language as defined by the standard includes the preprocessor, and the operation of the preprocessor is described algorithmically since it would be extremely hard to describe in any grammatical formalism. It is in that section of the standard where lexical decomposition is described, including the rules where it must be applied more than once.)

各种语法(用于词法分析的两个重叠语法,一个发生在预处理之前,另一个,如有必要,在之后,加上句法"语法)在附录 A 中收集,并附有此重要说明(强调):

The various grammars (two overlapping grammars for lexical analysis, one which takes place before preprocessing and the other, if necessary, afterwards, plus the "syntactic" grammar) are collected in Appendix A, with this important note (emphasis added):

此 C++ 语法摘要旨在帮助理解.这不是语言的确切陈述.特别是,这里描述的语法接受一个有效 C++ 结构的超集.必须应用消歧规则(6.8、7.1、10.2)来区分表达式和声明.此外,必须使用访问控制、歧义和类型规则来清除语法上有效但无意义的结构.

This summary of C++ syntax is intended to be an aid to comprehension. It is not an exact statement of the language. In particular, the grammar described here accepts a superset of valid C++ constructs. Disambiguation rules (6.8, 7.1, 10.2) must be applied to distinguish expressions from declarations. Further, access control, ambiguity, and type rules must be used to weed out syntactically valid but meaningless constructs.

最后,这是承诺的程序.当且仅当 IsPrime 中的 N 是素数时,第 21 行在语法上是正确的.否则,typen 是一个整数,而不是一个模板,所以 typen<1>() 被解析为 (typen<1)>() 这在语法上是不正确的,因为 () 不是一个语法上有效的表达式.

Finally, here's the promised program. Line 21 is syntactically correct if and only if the N in IsPrime<N> is prime. Otherwise, typen is an integer, not a template, so typen<1>() is parsed as (typen<1)>() which is syntactically incorrect because () is not a syntactically valid expression.

template<bool V> struct answer { answer(int) {} bool operator()(){return V;}};

template<bool no, bool yes, int f, int p> struct IsPrimeHelper
  : IsPrimeHelper<p % f == 0, f * f >= p, f + 2, p> {};
template<bool yes, int f, int p> struct IsPrimeHelper<true, yes, f, p> { using type = answer<false>; };
template<int f, int p> struct IsPrimeHelper<false, true, f, p> { using type = answer<true>; };

template<int I> using IsPrime = typename IsPrimeHelper<!(I&1), false, 3, I>::type;
template<int I>
struct X { static const int i = I; int a[i]; }; 

template<typename A> struct foo;
template<>struct foo<answer<true>>{
  template<int I> using typen = X<I>;
};
template<> struct foo<answer<false>>{
  static const int typen = 0;
};

int main() {
  auto b = foo<IsPrime<234799>>::typen<1>(); // Syntax error if not prime
  return 0;
}

<小时>

[1] 更专业地说,上下文相关语法中的每个产生式都必须采用以下形式:


[1] To put it more technically, every production in a context-sensitive grammar must be of the form:

αAβ&右箭头;αγβ

其中 A 是非终结符,αβ 可能是语法符号的空序列,而 γ 是一个非空序列.(语法符号可以是终结符也可以是非终结符).

where A is a non-terminal and α, β are possibly empty sequences of grammar symbols, and γ is a non-empty sequence. (Grammar symbols may be either terminals or non-terminals).

这可以读作 A →γ 仅在上下文 [α, β] 中.在上下文无关(类型 2)语法中,αβ 必须为空.

This can be read as A → γ only in the context [α, β]. In a context-free (Type 2) grammar, α and β must be empty.

事实证明,您还可以使用单调"限制来限制语法,其中每个产生式都必须采用以下形式:

It turns out that you can also restrict grammars with the "monotonic" restriction, where every production must be of the form:

<代码>α&右箭头;β 其中 |α|≥|β|>0  (|α| 表示α"的长度)

α → β where |α| ≥ |β| > 0  (|α| means "the length of α")

可以证明单调文法识别的语言集与上下文相关文法识别的语言集完全相同,而且通常情况下,基于单调文法进行证明更容易.因此,将上下文敏感"用作单调"的情况是很常见的.

It's possible to prove that the set of languages recognized by monotonic grammars is exactly the same as the set of languages recognized by context-sensitive grammars, and it's often the case that it's easier to base proofs on monotonic grammars. Consequently, it's pretty common to see "context-sensitive" used as though it meant "monotonic".

这篇关于C++ 是上下文无关的还是上下文敏感的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆