问题描述
C ++,C#或Java语言是上下文无关的还是上下文敏感的?
Are the C++, C# or Java languages context-free or context-sensitive?
推荐答案
由于模板系统是图灵完备的,要确定一段C ++代码是否合法C ++很难.例如,我可以定义一个模板类,该模板类在字符串上模拟TM,然后如果机器接受,则创建一个值为1的常量,否则为0.如果我这样做了,那么以下代码将是合法的,前提是TM在给定的输入上停止了:
C++ is neither context-free nor context-sensitive, since the template system is Turing-complete and determining whether a piece of C++ code is legal C++ is undecidably hard. For example, I could define a template class that simulates a TM on a string and then creates a constant with value 1 if the machine accepts and 0 if it does not. If I did that, then the following code would be legal iff the TM halted on the given input:
int myArray[TMTemplate</* ... args ... */>::value];
由于TM拒绝,因此会创建大小为0的数组,这是不允许的.
Since if the TM rejects, this creates an array of size 0, which is not allowed.
C#和Java都不是上下文无关的,因为众所周知,检查变量是否在特定范围内正确且一致地使用并不是上下文无关的(证明很复杂,并且依赖于奥格登的引理).但是,我不确定它们是否上下文相关.
Neither C# nor Java is context-free, because checking of whether a variable is used correctly and consistently throughout a particular scope is known not to be context-free (the proof is complex and relies on Ogden's lemma). However, I'm not sure whether or not they are context-sensitive.
希望这可以部分回答您的问题!
Hope this gives a partial answer to your questions!
这篇关于现代编程语言的语法是上下文无关的还是上下文敏感的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!