问题描述
我一直在寻找这个,但这个问题有很多不同的答案在。
I have been looking for this, but there is a lot of different answers to this question on the MSDN forums.
有些人说,所有计算机语言语法是无上下文的,而其他人则认为,任何具有对空格敏感的语法的语言都可能是上下文相关的,因此不是上下文无关的(F#和Python)。
Some people say that "All computer language grammars are context free" and others say that any language that has white-space sensitive syntax is likely context-sensitive and therefore not context-free (F# and Python).
推荐答案
我将C#描述为具有上下文无关的语法,但语言具有不在语法中表达的上下文敏感规则。
从维基百科a href =http://en.wikipedia.org/wiki/Formal_grammar =nofollow>正式语法):
I would describe C# as having a context-free grammar, but the language has context-sensitive rules that are not expressed in the grammar.
From Wikipedia (Formal grammar):
从C#4.0规范第2.2.1节(语法表示法):
From the C# 4.0 specification, section 2.2.1 (Grammar notation):
然而,存在许多规则在规范中是上下文相关的。例如,在获得其值的每个位置必须明确分配局部变量(§5.3)。另外,方法的签名在声明方法的类中必须是唯一的。这些都是任何特定片段的有效性取决于其出现的上下文的情况。
There are, however, many rules in the specification that are context-sensitive. For example, "A local variable must be definitely assigned (§5.3) at each location where its value is obtained." Also, "The signature of a method must be unique in the class in which the method is declared." These are both cases where the validity of any particular fragment depends on the context in which it appears.
当然,许多编程语言有类似的要求,包括C.我怀疑大多数会考虑C语境敏感的语言。我认为回答总结得很好:
Of course, many programming languages have similar requirements, including C. I doubt most would consider C a context-sensitive language. I think this answer summarized it well:
对于Python和F#,我在我的注释,这些语言通常被描述为具有语义(或有时是句法)的空格而不是上下文相关。
As for Python and F#, as I said in my comment, those languages are usually described as having semantic (or sometimes syntactic) whitespace not context-sensitive.
这篇关于是C#被认为是上下文无关语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!