# Construct a pair of languages by using CFG

## Problem

Consider the following context-free grammars (CFG) and find the pairs of languages which can be generated by Gl and G2 respectively.

## Solution

Consider the following CFG −

G1 : S->aS|B , B->b l bB

G2: S->aA | bB , A->aA| B | ε , B->bB | ε

Now, we can generate the language as follows. First consider G1 as shown below

Consider G1: S->aS|B
B->b|bB
Using S->B
->b b can be generated
Using S->B
->bB
->bb bb can be generated
Using S->aS
->aB
->ab ab can be generated
Using S->aS
->aB
->abB
->abb abb can be generated

As we can see, the number of a's can be zero or more but the number of b's is always greater than zero.

Therefore, the result will be as follows −

L(G1)= {ambn | m>=0 & n>0 }

Now, consider G2 as shown below −

Consider G2: S->aA|bB
A->aA|B| ε
B->bB| ε
Using S->aA
->a a can be generated
Using S->bB
->b b can be generated
Using S->aA
->aaA
->aa aa can be generated
Using S->bB
->bbB
->bb bb can be generated
Using S->aA
->aB
->abB
->abb abb can be generated

As we can see, either a or b must be greater than zero.

Therefore, the result will be as follows −

L(G2)= {aman |m>0 or n>0}

Updated on: 12-Jun-2021

136 Views