简体   繁体   English

创建CFG的技术

[英]Techniques for creating CFG's

I have to create a CFG which generates 我必须创建一个CFG

{a^n (ab)^nc^md^le^k | n>0, k, l, m>=0, k<m, m=l+k}

The first part is easy enough, I came up with 第一部分很简单,我想出了

S -> aS2abS3 S2 -> aS2ab | epsilon

However, the second part is very confusing. 但是,第二部分非常令人困惑。 So far I have 到目前为止,我有

S3 -> S4 | epsilon

The problem I have is how do I possibly keep track of all of these variables? 我的问题是如何跟踪所有这些变量? K has to be less than m, m has to be equal to l + k, and l must be at least 1 by extension. K必须小于m,m必须等于l + k,并且l必须扩展为至少1。 Can someone give me some general tips for approaching these CFG's? 有人可以给我一些使用这些CFG的一般提示吗?

Think inside out (because that's the way CFGs work) and don't get confused by extraneous details. 由内而外思考(因为这是CFG的工作方式),不要被无关的细节弄糊涂。

Here's the tip: CFGs are push-down automata (PDAs) which mean that they have a stack. 提示:CFG是下推自动机(PDA),这意味着它们具有堆栈。 PDAs are good at symmetry but they can't do antisymmetry. PDA擅长对称性,但不能反对称。 So they can do palindromes, but not repetitions. 因此他们可以做回文,但不能重复。 That's the nature of a stack. 这就是堆栈的本质。

So you always need to look for the mirror images. 因此,您始终需要查找镜像。 For example, a m b m is a trivial form of mirror iamge, where the mirror turns a s into b s. 例如, a m b m是镜像iamge的平凡形式,其中镜子将a s变成b s。

This one is just a little more complicated, but if you focus on looking for the symmetry, you'll find it. 这只是稍微复杂一点,但是如果您专注于寻找对称性,就会发现它。

Having said that, here's the solution: 话虽如此,以下是解决方案:

Since m > l , 由于m > l

cmdlek

can be rewritten as: 可以重写为:

cm-lcldlek

And since k = m - l , that's the same as: 由于k = m - l ,因此与:

ckcldlek

From there it's trivial: 从那里是微不足道的:

Sinner → cd | c Sinner d
Souter → Sinner | c Souter e

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

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