简体   繁体   English

现代编程语言的语法是无上下文还是上下文敏感?

[英]Are the grammars of modern programming languages context-free or context-sensitive?

C ++,C#或Java语言是无上下文还是上下文敏感?

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. C ++既不是上下文也不是上下文敏感的,因为模板系统是Turing-complete并且确定一段C ++代码是否合法C ++是不可判的。 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. 例如,我可以定义一个模拟字符串上模拟TM的模板类,然后创建一个值为1的常量(如果机器接受),如果不接受,则创建0。 If I did that, then the following code would be legal iff the TM halted on the given input: 如果我这样做,那么如果TM在给定输入上停止,则以下代码将是合法的:

int myArray[TMTemplate</* ... args ... */>::value];

Since if the TM rejects, this creates an array of size 0, which is not allowed. 因为如果TM拒绝,这将创建一个大小为0的数组,这是不允许的。

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 ). C#和Java都不是无上下文的,因为已知在整个特定范围内是否正确且一致地使用变量的检查不是无上下文的(证明是复杂的并且依赖于Ogden的引理 )。 However, I'm not sure whether or not they are context-sensitive. 但是,我不确定它们是否与上下文相关。

Hope this gives a partial answer to your questions! 希望这能为你的问题提供部分答案!

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

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