You are on page 1of 3

Homework 3 solutions

For each of the languages below, give a context-free grammar that will generate it.

1. L1 = {an bm ck | n + m = k }

Must add a ‘c’ for each ‘a’ and ‘b’.

Production Rules

S → aSc
S → S1
S→
S1 → bS1 c
S1 → 

2. L2 = {an bm ck | n + k = m }

Must add a ‘b’ for each ‘a’ and ’c’.

Production Rules

S → S1 S2
S1 → aS1 b
S1 → 
S2 → bS2 c
S2 → 

3. L3 = {an bm | m ≤ n ≤ 2m }

Must add 1 or 2 a’s for each additional ‘b’...

Production Rules

S → aSb
S → aaSb
S→

4. L4 = { w1 cw2 | w1 , w2 ∈ {a, b}*, length(w1 )=length(w2 ) }

Production Rules

1
S → aSb | aSa | bSa | bSb | c

5. L5 = { w1 can bm ai bj w2 | w1 , w2 ∈ {a, b}*, length(w1 )=length(w2 ), j = 2i, n ≤ m }

Production Rules

S → aSb | aSa | bSa | bSb | S1


S1 → cS2 S3
S2 → aS2 b | S2 b | 
S3 → aS3 bb | 

6. L6 = {an bm ck | n 6= m or m 6= k }

There are four ‘cases’ to consider:


1. More a’s than b’s (w/ any number of c’s).
2. More b’s than a’s (w/ any number of c’s).
3. More b’s than c’s (w/ any number of a’s).
4. More c’s than b’s (w/ any number of a’s).

Production Rules

S → S1 S3 | S2 S3 | S4 S5 | S4 S6

Each of the four ‘cases’ are accounted for (from left to right in the above production).

S1 → aS1 b | aS1 | a
S2 → aS2 b | S2 b | b
S3 → S3 c | 
S4 → aS4 | 
S5 → bS5 c | bS6 | b
S6 → bS6 c | S6 c | c

7. L7 is the complement of language denoted by a∗ b∗ c∗ . Here the alphabet set is Σ = {a, b, c}.

In the complement of the language denoted by a∗ b∗ c∗ , there must be ‘b’ followed later by an ‘a’,
or a ‘c’ followed later by a ‘b’ or an ‘a’...these conditions cause the string to no longer be in a∗ b∗ c∗ ,
since there are characters ‘out of order’ of that grammar...

Note that there can be characters in-between, the ‘following’ character mentioned above does not need to
directly follow the first.

2
Production Rules

S → S1 bS1 aS1 | S1 cS1 bS1 | S1 cS1 aS1


S1 → aS1 | bS1 | cS1 | 

8. L8 = L6 ∪ L7 . What is the complement of L8 ?

L8 comp = (L6 ∪ L7 )comp = L6 c ∩ L7 c


L7 comp = ((a∗ b∗ c∗ )comp )comp = a∗ b∗ c∗
L6 comp = (an bm ck | n 6= m or m 6= k)comp = (an bm ck | n == m and m == k) = (an bn cn | n ≥ 0)

This language is not context-free, so cannot write a grammar for it.

You might also like