简体   繁体   English

语言的CFG

[英]CFG for language

I'm trying to create a cfg generating following language:我正在尝试创建一个生成以下语言的 cfg:

foo+bar

Is This language context-free and could be generated by a cfg?这种语言是否与上下文无关并且可以由 cfg 生成? if yes , how could the grammar generating this language be created?如果是,如何创建生成这种语言的语法?

I'm not experienced so much in creating cfg's for cfl's.我在为 cfl 创建 cfg 方面没有太多经验。 I would be glad if any help or solution is given如果提供任何帮助或解决方案,我会很高兴

To start you off, do you know how to create a CFG for the language {a^nd^t | n = t}首先,您知道如何为语言{a^nd^t | n = t}创建 CFG 吗? {a^nd^t | n = t} ? {a^nd^t | n = t} ? This will be your starting point.这将是您的起点。

S -> aSd | epsilon

From here, you need to extend what happens when there are more a s than d s, or more d s than a s.从这里开始,您需要扩展当a s 多于d s 或d s 多于a s 时发生的情况。 The goal will always be to make sure that for every a or b consumed on the left, a c or d is consumed on the right.目标始终是确保对于在左侧消耗的每个ab ,在右侧消耗cd

S -> aSd | aAc | bDd | epsilon

A is the case where there are more a s than d s, D is the other case. Aa s 比d s 多的情况, D是另一种情况。 In each of these, you you need to consume a left character for every right character.在其中的每一个中,您需要为每个右侧字符消耗一个左侧字符。

There's an additional case, which is the case that either or both of n and t are 0 .还有一种情况,即nt中的一个或两个都是0 I'll leave it to you to determine how to handle that.我会把它留给你来决定如何处理。

Then you'll have to deal with running out of one side's character.然后你将不得不处理耗尽一方的角色。 For example, if you're in state A , you expect to match a s with c s until the a s run out and turn into b s.例如,如果您处于状态A ,您希望将a s 与c s 匹配,直到a s 用完并变成b s。 Something like,就像是,

A -> aAc | bCc

I'll leave the rest to you to figure out.剩下的就交给你去想办法了。

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

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