How do I figure out the language generated by this context-free grammar?

时光怂恿深爱的人放手 提交于 2019-12-25 12:26:14

问题


I am dealing with the following grammar:

G = ( {S, A}, {a, b}, P, S ) 
P = { S -> aAb, S -> bAa, 
A -> aSa, 
A -> S, 
A -> epsilon}

I need to find out L(G). The thing is, I figured out that the words in the grammar are of the form: starts with a and ends with b, or starts with b and ends with a, and between these letters one of the combinations : ab, ba, aaba, abaa; then the next word is formed by inserting one of these 4 combinations between the a and b in the middle..but how can I express this formally? I mean, as far as I could tell, L(A) = a^n S a^n and if w belongs to L(G), then w reversed also belongs to L(G). I tried to express it as a regular expression but failed...could anyone please help?

Thank you.


回答1:


You see that L is not regular, to prove you can use Pumping lemma or Myhill–Nerode theorem, so regular expression can't be discussed

You can notice, that since L consists just of {a,b} you can use it's power We see that language is in the form of aAb or bAa or aAa with exception that aAa can't be located at begin-end of the word

So let's use this, the only thing we miss is the combination of bAb A can generate almost everything (words |w| = 2k, and |w|>=2) but words where position of b matches position of b from the reverse

Formally

Sorry for my tex skills and my formal expression

there must be some error, because I didn't have so much time to think about this, but it can be some way how to continue, it's homework so it's fine, think about it! :)




回答2:


I tried to express it as a regular expression but failed

This language is 'probably' not regular. Context-free languages are more complex/powerful than regular expressions.

Try generating a few words and see if you can come up with a name, properties for the language. Or try to find words that are not in the language.

Some hints, a few properties you can easily see:

  1. The smallest string is at least size 2.

  2. The size of the string is even.

  3. The count of b's is <= than a's.

If you take out A -> aSa, and compare a word with its reversed version, a pattern should be visible. If you include the left out rule, the pattern changes slightly...




回答3:


You are asked to find the language generated by the grammar G with productions

S → aAb | bAa
A → aSa | S | λ

First, consider small derivations beginning from the start symbol S

S ⇒1 aAb ⇒1 aaSab | aSb | ab

S ⇒1 bAa ⇒1 baSaa | bSa | ba

The difficult step is dealing with the recursion generated by the rules AaSa, SaAb and SbAa. The clue to dealing with this difficulty is revealed by considering an inductive definition of the language generated by G:

1. ab ∈ L4
2. ba ∈ L4
3. w ∈ L4 → awb ∈ L4
4. w ∈ L4 → bwa ∈ L4
5. w ∈ L4 → awa ∈ L4

The rules (3)-(5) correspond to the rules AaAa, SaAb and SbAa in G. It is easy to see that the inductive definition and the rules of G define the same language. The inductive definition shows that the language of G can be build incrementally in steps. Starting with the smallest strings generable in G, we build larger and larger sets of strings corresponding to the problematic rules:

L(1)     = {ab, ba}
L(n + 1) = {awb, bwa, awa : w ∈ L(n)}

The set L(1) contains the smallest strings generable in G. The set L(n + 1) contains strings awb, bwa and awa for each string wL(n). That is, the strings in L(n + 1) correspond to the strings obtained by applying the rules SaAb, SbAa and AaAa once to strings in L(n). All that remains is to construct the union of the L(n), which is a set:

L = ⋃ {L(n) : n ∈ ℕ}

To see that L is equivalent to the language generated by the grammar G, you can argue by induction on the length of derivations in G. Starting with the smallest strings genreable in G (i.e, ab and ba), working backwards using appropriate induction hypotheses.




回答4:


the language generated is : (a)n(b)m Were n>=m



来源:https://stackoverflow.com/questions/8123485/how-do-i-figure-out-the-language-generated-by-this-context-free-grammar

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