简体   繁体   English

识别最大类型的正式语言

[英]recognise max type of formal language

Currently I´m trying to learn and understand formal languages and grammatics. 目前我正在努力学习和理解正式语言和语法。 I understand the Chomsky hierarchy but I found an task where I don´t know how they got the solution. 我理解乔姆斯基的等级制度,但我找到了一个我不知道如何得到解决方案的任务。 The task is: 任务是:

G=({S},{a,b},S,P)
P={S->epsilon, S->aS, S->Sb}
  1. What is the maximal type of this grammar? 这个语法的最大类型是什么?
  2. What is the maximal type of L(G) ? L(G)的最大类型是什么?

I know that the grammar is type 2, but in the answer was written that L(G) is type 3. 我知道语法是类型2,但在答案中写道L(G)是类型3。

It seems that there is also a type 3 grammar describing this language, but how do you know which is the maximal type of a formal language? 似乎还有一种描述这种语言的类型3语法,但是你怎么知道哪种形式语言的最大类型? Is there some trick? 有一些技巧吗?

I don't think there's some easy, algorithmic way to always tell what class of language L(G) is; 我不认为有一种简单的算法方法可以总是说出L(G)是什么类的语言; I mean to say, in general, I think there are cases that simply cannot be proven one way or the other. 我的意思是说,总的来说,我认为有些情况根本无法以某种方式证明。 This from incompleteness/undecidability, given that unrestricted grammars can encode arithmetic. 鉴于不受限制的语法可以编码算术,这来自不完整性/不可判定性。 This is very hand-wavy. 这是非常手工波浪状的。

Heuristically, you can try to understand what your language is, you can transform the grammar to try to force the grammar to be simpler, etc., but these do not guarantee success. 启发式地,您可以尝试理解您的语言是什么,您可以转换语法以尝试强制语法更简单等,但这些并不能保证成功。

In this particular case, it's not too bad to figure out what the language is, recognize it is regular, and write down a grammar for it. 在这种特殊情况下,弄清楚语言是什么,识别它是常规的,并为它写下语法并不算太糟糕。

S -> e
S -> aS
S -> Sb

Any string derived from S can begin with any number of a s by S -> aS , and can end with any number of b s by S -> Sb . 衍生自任何字符串S可以与任何数量的开始a由s S -> aS ,和任何数量的罐端部b由s S -> Sb We might hypothesize that the language is a*b* , and then prove it by showing (1) L(G) is a subset of a b and (2) a b is a subset of L(G). 我们可以假设语言是a*b* ,然后通过显示(1)L(G)是b的子集并且(2)a b是L(G)的子集来证明它。

(1) The proof is by induction on the number of applications of S -> aS and S -> Sb . (1)证据是通过归纳S -> aSS -> Sb的申请数量。 Base case: if neither of these is applied, only the string e is derivable. 基本情况:如果这两个都没有应用,只有字符串e是可派生的。 e is in a*b* , so the base case is confirmed. ea*b* ,因此基本情况得到确认。 Induction hypothesis: assume any string derived by up to and including k applications of S -> aS and S -> Sb is in a*b* . 归纳假设:假设由S -> aSS -> Sb k应用得出的任何字符串都在a*b* Induction step: we show that any string derived using k+1 applications of these productions is also in a*b* . 归纳步骤:我们显示使用这些产品的k+1应用得出的任何字符串也在a*b* Any string derived in k+1 productions has one derived in k productions by skipping the k+1 st production. k+1作品中派生的任何字符串都有一个通过跳过第k+1作品而在k作品中派生的字符串。 This is because the productions keep the nonterminal sets the same. 这是因为制作使非终结组保持不变。 We already know this shorter string derived in k applications is in a*b* . 我们已经知道k应用程序中派生的这个较短的字符串是a*b* We have two cases to consider: 我们有两种情况需要考虑:

  1. S -> aS : this adds one a to the front of the shorter string in a*b* , keeping it in a*b* . S -> aS :这会在a*b*中的较短字符串的前面添加一个a ,使其保持在a*b*
  2. S -> Sb : this adds one b to the end of the shorter string in a*b* , keeping it in a*b* . S -> Sb :这会在a*b*中的短字符串的末尾添加一个b ,使其保持在a*b*

Since both cases keep the string in a*b* , all strings derived in k+1 applications of the productions are in a*b* and, by induction, all strings generated by the grammar are. 由于两种情况都将字符串保存在a*b* ,因此在产品的k+1应用程序中派生的所有字符串都在a*b*并且通过归纳,语法生成的所有字符串都是。

(2) Consider any string a^nb^m in a*b* . (2)考虑a*b*a*b*任何字符串a^nb^m It is generated by the grammar as follows: n applications of A -> aS , m applications of B -> Sb , and one application of S -> e . 它由语法生成如下: n应用A -> aSm应用B -> Sb ,一个应用S -> e Thus all strings in a*b* are generated by the grammar. 因此, a*b*中的所有字符串都是由语法生成的。

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

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