简体   繁体   English

Chomsky层次结构-类型1上下文相关的语言

[英]Chomsky hierarchy - Type-1 context-sensitive languages

I try to unterstand the Chomsky hierarchy with the different levels. 我试图以不同的层次来理解乔姆斯基体系。

I checked some examples and here is one I don't really understand. 我检查了一些示例,这是我不太了解的示例。 Maybe someone knows why this one is not a context-sensitive language: 也许有人知道为什么这种语言不是上下文相关的语言:

S → aA 
A → aA | ε
B → bA

The grammar G that you provide is, beyond any doubt, context free (each rule has one non-terminal in the LHS and a string of terminals and non-terminals in the RHS). 您提供的语法G毫无疑问是上下文无关的(每个规则在LHS中具有一个非终结符,在RHS中具有一系列终结符和非终结符)。 Therefore, the language L that it generates is context free. 因此,它生成的语言L是上下文无关的。 Therefore, as the category of context-free languages is a proper subset of that of context-sensitive languages, language L is context sensitive . 因此,由于上下文无关语言的类别是上下文相关语言的适当子集,因此语言L是上下文相关的 (I don't know where you read that language L is not context sensitive. Either you misread this or what you read is simply wrong.) (我不知道您在哪里读到的语言L并不是上下文相关的。您可能是读错了语言,或者您读的东西根本就错了。)

I will make a guess about the reason of this confusion. 我将猜测造成这种混乱的原因。 Let us suppose that you had claimed that grammar G (not language L ) is not context sensitive. 让我们假设您声称语法 G (不是语言 L )不是上下文相关的。 Now, perhaps curiously enough, if a language is context sensitive this does not mean that all grammars producing this language are context sensitive (and the same is true for the other categories as well, except of course the unrestricted one). 现在,也许很奇怪,如果一种语言是上下文相关的,这并不意味着所有产生这种语言的语法都是上下文相关的(其他类别也是如此,当然,除了不受限制的类别以外)。 If a language is context sensitive, this means that there is a context-sensitive grammar producing it. 如果一种语言是上下文相关的,则意味着存在上下文相关的语法。 So, even if L is context sensitive, this does not necessarily mean that G is also context sensitive. 因此,即使L是上下文相关的,也不一定意味着G也是上下文相关的。

It would however be weird if G , a context-free grammar, were not context sensitive. 但是,如果G (上下文无关的语法)对上下文不敏感,那将很奇怪。 Whether this is or is not true depends on your exact definition of a context-sensitive grammar. 这是否成立取决于上下文敏感语法的确切定义。 As you can read, eg, in the related Wikipedia article , there are at least two alternative definitions for context-sensitive grammars: 如您所读,例如,在相关的Wikipedia文章中 ,上下文敏感语法至少有两个替代定义:

  1. A grammar where all rules are of the form αAβ → αγβ, where A is a non-terminal symbol and α, β, γ are strings of terminals and non-terminals. 一种语法,其中所有规则的形式为αAβ→αγβ,其中A是非终结符,而α,β,γ是终结符和非终结符的字符串。
  2. A grammar where all rules are of the form α → β, where α and β are strings of terminals and non-terminals, but the length of α is not larger than the length of β. 所有规则都为α→β形式的语法,其中α和β是末端和非末端的字符串,但是α的长度不大于β的长度。 As an exception, there can be a rule S → ε for the start symbol S (which would otherwise be forbidden). 作为例外,起始符号S可以有规则S→ε(否则将被禁止)。

According to definition 1, grammar G is context sensitive (the context strings α and β being trivially empty). 根据定义1,语法G是上下文相关的(上下文字符串α和β很少为空)。 According to definition 2, however, grammar G is not, because of the empty production rule for non-terminal A, which is not the start symbol. 但是,根据定义2,语法G不是,因为非结尾A的生成规则为空,这不是起始符号。

It is however possible to provide an equivalent grammar G' , which is context sensitive according to both definitions and produces the same language L . 但是,可以提供一个等效的语法G' ,根据两个定义,上下文对上下文敏感,并且产生相同的语言L One such grammar can be constructed as follows: 一种这样的语法可以构造如下:

A produces strings of zero or more "a". A产生零个或多个“ a”的字符串。 We can replace its definition by: 我们可以将其定义替换为:

A → A' | ε
A' → a | aA'

where A' produces strings of one or more "a". 其中A'产生一个或多个“ a”的字符串。 Notice that the rules for A are not recursive. 注意,A的规则不是递归的。 We can substitute the productions for A in the rules for S and B and then eliminate the non-terminal A. This gives us: 我们可以在S和B的规则中用A的产生式代替,然后消除非末端A。这使我们:

S → aA' | a
A' → a | aA'
B → bA' | b

This grammar (which can be further simplified by noticing that A' and S produce the same language) is context sensitive according to both definitions above. 根据以上两个定义,该语法(可以通过注意A'和S产生相同的语言来进一步简化)是上下文相关的。

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

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