edited by
1,445 views
1 votes
1 votes
Find CFG for the following language

L = $\left \{ a^{n}b^{m}: n \neq 2m \right \}$
edited by

1 Answer

Best answer
1 votes
1 votes

Here ,  L = {anbm : n ≠ 2m , n ≥ 0 , m ≥ 0}

Now , To write grammar , first we analyze the language and based on this , we will write the grammar... 

Here ,  We have 2 parts in the language :-

1) n > 2m

if m = 0  then accepted strings will be a,a2,a3,a4 ...

if m = 1  then accepted strings will be a3b , a4b , a5b,...

if m  = 2 then accepted strings will be a5b2 , a6b2 , a7b2 , ...

if m = 3 then accepted strings will be a7b3 , a8b3 , ..

if m =4 then accepted strings will be a9b4 , a10b4 ,...

Now ,for strings  a,a2,a3,a4 , We can write grammar as S ---> aS | a

Now observe 1st string of m=1,2,3,4,.. Similarly 2nd string of m=1,2,3,4...Similarly for 3rd,4th...strings...There is a pattern , a is incrementing 2 times and b is incrementing 1 time every time...Since pattern is starting with aaab.. So , after analyzing the pattern , we can write grammar for this as S ---> aaaAb ;   A---->aaAb | aA

So ,overall grammar for 1st part wil be :-

S ---> aS | aaaAb | a

A ----> aaAb | aA | ɛ

Now ,2nd part of the language wil be  :-

2) n < 2m

For this part ,

n=0 , strings will be b,bb,bbb,.....So, Grammar will be S --> bX  ; X ---> bX | ɛ

now , m = 1 , accepted string is ab (Total 1 string)

m= 2 ,   accepted strings are ab2 , a2b2 ,a3 b(Total 3 strings)

m= 3 , accepted strings are ab3 , a2b3 , a3 b3 , a4 b3 ,a5b(Total 5 strings)

m = 4 ,  accepted strings are ab4 , a2b, a3 b4 , a4b4 , a5b4 , a6 b4 , a7b4 (Total 7 strings)

Now , again there is a pattern ..So, grammar for this , S ---> aBb ;   B ---> Bb | aBb | ɛ

So, Now , After combining both parts , Grammar will be :-

S ----> S1 | S2

S1 ---> aS1 | aaaAb | a

A ----> aaAb | aA | ɛ

S2 ---> bX | aBb

X  ---> bX | ɛ

B ----> Bb | aBb | ɛ

selected by

Related questions