是C#被认为是上下文无关语言? [英] Is C# considered a context free language?

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

问题描述

我一直在寻找这个,但这个问题有很多不同的答案在 MSDN论坛

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节(语法表示法):

A context-free grammar is a grammar in which the left-hand side of each production rule consists of only a single nonterminal symbol.


词法和句法语法使用语法生产。

From the C# 4.0 specification, section 2.2.1 (Grammar notation):



The lexical and syntactic grammars are presented using grammar productions. Each grammar production defines a non-terminal symbol and the possible expansions of that non-terminal symbol into sequences of non-terminal or terminal symbols.

然而,存在许多规则在规范中是上下文相关的。例如,在获得其值的每个位置必须明确分配局部变量(§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:


语法正确的程序集几乎所有语言都是上下文无关的。

The set of programs that are syntactically correct is context-free for almost all languages. The set of programs that compile is not context-free for almost all languages.

对于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#被认为是上下文无关语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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