简体   繁体   English

为CFL编写GNF语法

[英]Writing GNF grammar for a CFL

Hello I would like to ask you this question. 您好我想问你这个问题。

I was supposed to compute (manually) a grammar in Greibach Normal Form, that generates the language 我应该用Greibach Normal Form计算(手动)语法,生成语言

L = {a i b j c k | i + j = 2k and k >= 1}

I really have no idea. 我真的不知道。 Can someone please help me? 有人可以帮帮我吗?

Thanks in advance 提前致谢
Chriss CHRISS

Context Free Grammar CFG for your language L a = {a i b j c k | i + j = 2k and k >= 1} 上下文免费语法CFG为您的语言L a = {a i b j c k | i + j = 2k and k >= 1} L a = {a i b j c k | i + j = 2k and k >= 1} . L a = {a i b j c k | i + j = 2k and k >= 1}

Below is answer with language L = {a i b j c k | i + j = k and k >= 1} 以下是语言L = {a i b j c k | i + j = k and k >= 1}答案 L = {a i b j c k | i + j = k and k >= 1} . L = {a i b j c k | i + j = k and k >= 1}

CFG: CFG:

S --> aAc | bBc |
A --> aAc | B   |  ^
B --> bBc | ^ 

What is GNF? 什么是GNF?

One important form of CFG is Greibach Normal Form GNF: CFG的一个重要形式是Greibach Normal Form GNF:

   A --> aα   
   Where α ∈ V* (any number of variables including zero)

Note: nul ^ can't be a symbol on RHS of any production accept start symbol S with constrict that if S --> ^ is a production in grammar then S can't be appear on RHS of any other production in grammar. 注意: nul ^不能是任何生产接受开始符号S RHS上的符号,如果S --> ^是语法生成则S不能出现在语法中任何其他生成的RHS上。

Any CFG can be written in GNF form. 任何CFG都可以用GNF格式编写。

How to convert CFG in GNF? 如何在GNF中转换CFG?

Note in CFG I written above have nul productions A --> ^ and B --> ^ and a Unit production A --> B . 我在上面写的CFG中的注释有n个产品A --> ^B --> ^和一个单位产品A --> B Unit productions and nul productions are not allowed in GNF form. GNF形式不允许单位制作和零制作。 Although other productions can easily written in GNF form by introduction in GNF productions in grammar eg S --> aAc can be rewrite as S --> aAC and C --> c . 虽然其他作品可以通过GNF制作中的语法简单地以GNF形式书写,例如S --> aAc可以重写为S --> aAC and C --> c

So below I am rewriting equivalent CFG for language and removing nul and unit productions called simplified CFG. 所以下面我将重写相当于语言的CFG并删除称为简化CFG的零和单元制作。

Simplified CFG: 简化CFG:

S --> aAc | bBc | ac | bc
A --> aAc | bBc 
B --> bBc | bc

Now this grammar can easily converted into GNF form by introducing new GNF production C --> c and replace c by C in other production rules. 现在,通过引入新的GNF生产C --> c并在其他生产规则中用C替换c ,这个语法很容易转换成GNF形式。

GFN for language L: 语言L的GFN:

S --> aAC | bBC | aC | bC
A --> aAC | bBC 
B --> bBC | bC
C --> c

By mistake I written a wrong grammar I will update answer for language L a 我写错了语法,我会更新语言L a答案

Edit 编辑

L a = {a i b j c k | i + j = 2k and k >= 1} L a = {a i b j c k | i + j = 2k and k >= 1} . L a = {a i b j c k | i + j = 2k and k >= 1}

CFG for L a : CFA for L a

S --> aaAc | bbBc | abBc
A --> aaAc | B    | abBc |  ^
B --> bbBc | ^ 

Simplified CFG: 简化CFG:

S --> aaAc | bbBc | abBc | aac | bbc | abc 
A --> aaAc | bbBc | abBc | aac | abc
B --> bbBc | bbc 

GFN for language L a : GFN语言L a

Add three new production rules: X --> a , Y --> b and Z --> c . 添加三个新的生产规则: X --> aY --> bZ --> c

Change programmer and replace terminal by variables: 更改程序员并按变量替换终端:

S --> aXAZ | bYBZ | aYBZ | aAZ | bYZ | aYZ  
A --> aXAZ | bYBZ | aYBZ | aXZ | aYZ
B --> bYBZ | bYZ
X --> a
Y --> b
Z --> c

About Below answer CFG of La = {ai bj ck | i + j = k and k >= 1} 关于以下答案回答CFG La = {ai bj ck | i + j = k and k >= 1} La = {ai bj ck | i + j = k and k >= 1} is wrong La = {ai bj ck | i + j = k and k >= 1}是错误的

Your Simplified CFG: 您的简化CFG:

S --> aAc | bBc | ac | bc . A --> aAc | bBc . B --> bBc | bc .

Because the above language L = {ai bj ck | i + j = k and k >= 1} 因为上面的语言L = {ai bj ck | i + j = k and k >= 1} L = {ai bj ck | i + j = k and k >= 1} produce the language aabccc but your Simplified CFG not produce it . L = {ai bj ck | i + j = k and k >= 1}生成语言aabccc但您的Simplified CFG不生成它。

Right CFG is 对,CFG是

Simplified CFG 简化的CFG

S --> aAc | bBc | ac | bc A --> aAc | bBc | bc | ac B --> bBc | bc

Correct it, Second language has the same problem. 纠正它,第二语言有同样的问题。

Thanks! 谢谢!

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

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