Creating a grammar out of a regular language

断了今生、忘了曾经 提交于 2021-01-29 16:03:41

问题


I have some problems to figure out a grammar for this special language, hope you could help: The language is: Σ={x,y,z} A = { w | w ∈ Σ^∗ ∧ |w|_x mod 2 >= |w|_y mod 2 }

Because this one is so hard i tried first to put all properties together in one grammar, so |w|_x mod 2 >= |w|_y mod 2 and w ∈ Σ^∗ , but without getting all combination like cacbcacb etc

What I get is something like: ccccc...caa...abcbbbcc and than i use ac -> ca etc to change the combination and to get every word I want.

But could we do some context free grammar too?

My solucion

S → G | U | c | cS | ɛ
G → AGB | ɛ | cG
A → ɛ | a | cA
B → ɛ | bb | cB
U → ab | DaUbE | cU
D → a
E → b
ab → ba
ba → ab
ac → ca
ca → ac
bc → cb
cb → bc

Very ungly.


回答1:


As you say, this language is regular, so there is clearly no need for a context-sensitive grammar.

Constructing a regular expression is tedious and not particularly useful for the task of finding a context-free grammar. It's easier to work directly from the state machine, particularly so in this case because there are only four states.

Converting a state machine into a CFG is almost trivial. Each state becomes a non-terminal, and you can read the productions off the state transitions. If state P has a transition to state Q on symbol a, the CFG has the production Q -> P a. The start symbol then has a unit production to each final state. And that's it.



来源:https://stackoverflow.com/questions/54233186/creating-a-grammar-out-of-a-regular-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!