Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

Give a grammar to accept this language: L={ w \in {a,b}* | n _{a}(w) = 2*n _{b}(w) } , (na(w) means the number of a's in w, nb(w) means number of b's in w.) , need an explanation on how to approach this kind of problems

OpenStudy (anonymous):

\[L={ w \in {a,b}* | n _{a}(w) = 2*n _{b}(w) }\]

OpenStudy (anonymous):

ok, what's you thinking on this? Where did you get stuck specifically?

OpenStudy (anonymous):

I dont know how to do it..

OpenStudy (anonymous):

What does that language definition mean? Can you explain it to me in English basically :)

OpenStudy (anonymous):

w is {a,b}* where the number of a is 2 * number of b

OpenStudy (anonymous):

great, so let's come up with 3-4 examples of words that satisfy that definition: here is one aabbbb -- will it adhere to the rule?

OpenStudy (anonymous):

i think so, abb, aaabbbbbb are other examples?

OpenStudy (anonymous):

yes, how what about abbabb? does that example fit the language def?

OpenStudy (anonymous):

yes? i think since it didnt mention anything about how a's and b's arranged in the language

OpenStudy (anonymous):

yep. ok, so let's try to reverse engineer grammar from those examples. so for every n number of a's you need to have 2n number of b's I'll help you with the first part: S=a^n|n>=0 gives you grammar that produces aaaaaaa type of words how do you add b's to it?

OpenStudy (anonymous):

\[S _{1} = b^n | n^2 ?\]

OpenStudy (anonymous):

no, n*2 instead of n^2

OpenStudy (anonymous):

well you'd want it to be combined with the first, so actually: S = a^nb^2n|n>=0 But that only gives you coverage for the cases such as aabbb What about the other case? can you come up with that?

OpenStudy (anonymous):

\[S= a^n S b^{2n} | b^{2n} S a^n | \epsilon \] not sure if this will work

OpenStudy (anonymous):

what is | in that equation?

OpenStudy (anonymous):

i mean S can be any of the three things after =

OpenStudy (anonymous):

Almost there. You might want to simplify it a bit and rewrite with different termination rule (say to terminate on b) but overall it's almost there.

OpenStudy (anonymous):

\[S= a^n S _{1} | b^{2n}S _{1} | S _{1} a^n | S _{1} b^{2n} | S _{1}\] \[S _{1}= a^nS | b^{2n}S | Sa^n | Sb^{2n} | a^{n} | b^{2n} | \epsilon\] still not sure if this is coorect

OpenStudy (anonymous):

Wait, we want the number of `a`s to be twice the number number of `b`s?

OpenStudy (anonymous):

I think ultimately we can start of with the empty string, and then we can add onto the left and right of the empty string... What we add has to be an `a` and two `b`s every time.

OpenStudy (anonymous):

I don't think you need to do \(\circ^n\) because we can do things recursively.

OpenStudy (anonymous):

\[ \begin{array}{rcl} S &=& \epsilon\\ &|& aSbb\\ &|& bSab\\ &|& bSba\\ &|& abSb\\ &|& baSb\\ &|& bbSa\\ \end{array} \]

OpenStudy (anonymous):

First cases is empty string Next three cases are each combination where one letter is on the left, two letters on the right. Final tree cases are just the reverse of previous three cases. It's interesting to ponder whether they are even necessary. What do you think?

OpenStudy (anonymous):

is it better to split them or it will get more complicated ?

OpenStudy (anonymous):

Actually there are some rules that I might still be missing here, believe it or not. Such as: \[ | Sabb\\ | Sbab \\ | Sbba \\ | abbS\\ | babS \\ | bbaS \\ \]

OpenStudy (anonymous):

Hmm, I think if you create a sub-expression, it could make things easier.

OpenStudy (anonymous):

For example, those six rules I just wrote? They are the same as saying: \[ |\quad SS \]

OpenStudy (anonymous):

I really enjoy questions like these.

OpenStudy (anonymous):

it is fun, sometimes..

OpenStudy (anonymous):

Here is an example of how we could use a sub-expression. In this case, \(T\) is just any combination of \(a\) and \(b\):\[ \begin{array}{rcl} T &=& ab\\ &|&ba\\ \\ S &=& \epsilon\\ &|& SS \\ &|& aSbb\\ &|& bbSa\\ &|& bST\\ &|& TSb\\ \end{array} \]

OpenStudy (anonymous):

Here is another idea:\[ \begin{array}{rcl} R &=& aSb\\ &|&bSa\\ \\ S &=& \epsilon\\ &|& SS \\ &|& bR\\ &|& Rb\\ \end{array} \]This one is very clean.

OpenStudy (anonymous):

In this case \(R\) forms a string where \(n_a(w) = 2 n_b(w) -1\). We balance it out to \(n_a(w)= 2 n_b(w) \) by adding a \(b\) to \(R\).

OpenStudy (anonymous):

Are you getting any ideas now?

OpenStudy (anonymous):

Yea thanks i think i am getting the idea, now i just need to practice it

OpenStudy (anonymous):

With these problems, you almost always want to avoid things like \(\circ^n\) because it is like using a for loop, when we really want to be using recursion.

OpenStudy (anonymous):

Looking at your solution. I could come to the conclusion that \(w=aaaaa\) is a proper solution, which I don't believe is right.

OpenStudy (anonymous):

yea i see my mistake now, ill try to avoid using \[0^n\]

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!