You are on page 1of 15

3 Context-Free

Grammar
Answer for Exercises (Page 94, Theory of Computer Science: Definitions and Examples. 4th
Ed.)
Part A
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.

Multiple Choices Questions


B
B
D
C
D
D
D
C
D
A
A
C
C
B
C
B
A
A
D
D
B
D
B

24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.

C
C
A
D
A
D
C
B
D
C
A
B
B
B
A
A
A
A
B
C
C
D
D

1|P a g e

3 Context-Free
Grammar
Part B

Structured Questions

1.
a)

S SAB
SABAB
ABAB
aBAB
abBAB
abbBAB
abbAB
abbaAB
abbaaB
abbaabB
abbaab
abbaab

b)

S SAB
AB
aAB
aaB
aa
aa

S SAB
SABAB
ABAB
aBAB
aAB
aaB
aa
aa

c)
S
S

A
a

B
A

a
S
S

d)

(a*ab*)*

2|P a g e

3 Context-Free
Grammar
2.
b)

c)

Derivation
S abSc
ababScc
ababAcc
ababcAdcc
ababccddcc

Rule
S abSc
S abSc
SA
A cAd
A cd

The derivation tree corresponding to the preceding derivation is


S
a

S
a

c
S

A
c

c
d)

d
d

L(G) = {(ab)ncmdmcn | n 0; m > 0}

3.
a)

Derivation
S ASB
aAbSB
aaAbbSB
aabbSB
aabbB
aabbba
aabbba

b)

Derivation
S ASB
ASbBa
ASbbaa
AASBbbaa
AASbBabbaa
AASbabbaa
AAASBbabbaa
AAASbabbaa
AAAbabbaa

Rule
S ASB
A aAb
A aAb
A
S
B ba

Rule
S ASB
B bBa
B ba
S ASB
B bBa
B
S ASB
B
S
3|P a g e

3 Context-Free
Grammar
AAaAbbabbaa A aAb
AAaaAbbbabbaa
A aAb
AAaabbbabbaa
A
AaAbaabbbabbaa
A aAb
Aabaabbbabbaa
A
abaabbbabbaa
A
abaabbbabbaa
c)
S
A

B
b

S
A

A
a

S Bb

B a

a
a

d)

L(G) = {an1bn1 ankbnk bm1am1 bmlaml | ni, mi > 0; k, l 0; k l}

4|P a g e

3 Context-Free
Grammar
4.
a) The following is a leftmost derivation of abaabb:
S XY
aXY
abXY
abaY
abaYb
abaYbb
abaabb

b)
c)

(a + b)*aa(a + b)*

5. The grammar is:

S AB
A aAb | Ab |
B bBc |

6. G = (V, T, S, P), where V = {S, A, B}; T = {a, b};

P=

S aA
A aA | aB |
B bA

7.
a)

b)

S AB
aAB
aaB
aaAB
aaaB
aaab
S AB
AAB
AaB
Aab
aAab
aaab

5|P a g e

3 Context-Free
Grammar
8.
Set notations

{a2ibib*, i 0} or
{(aa)ibj | i > 0, j i}
{} or {a2ibj | i > 0,
j i} {}.

{anc+b2n , n 0} or
{ancm(bb)n | n 0, m
> 0} or {ancmb2n | n
0, m > 0}.

{(ab)n(cd)m(ba)m(dc)n
, n 1, m 0}

{ancmapbpdmbn, m
1, n 0, p 1}

{akbj | 0 < k j 2k}

9.

Shortest string and 3


other strings

Descriptions
The S rules S aaSB and S generate an equal
number of leading aa's and trailing B's. Each B is
transformed into one or more b's. Since at least one
application of the rule S aaSB is necessary to generate
b's, strings consisting solely of b's are not in the language.
The language of the grammar is {(aa)ibj | i > 0, j i} {}
or we can write {a2ibj | i > 0, j i} {}.
The recursion of S rules S aSbb generate an equal
number of leading a's and trailing bb's. The recursion is
terminated by an application of the rule S A. Each A is
transformed into one or more c's. The language of the
grammar is {ancm(bb)n | n 0, m > 0} or we can write
{ancmb2n | n 0, m > 0}.
Repeated applications of the recursive rule S abSdc
produce a string of the form (ab)iS(dc)i, i 0. The recursion
is terminated by an application of the rule S A. The A
rules produce strings of the form (cd)j(ba)j, j 0. The
language generated by the rules is {(ab)i(cd)j(ba)j(dc)i | i
0, j 0}.

aab
aabb, aaaabb, aaaabbb

c
aacbbbb accbb acccbb

abdc / cdba
abcdbadc, abcdcdbabadc,
ababcdcdbabadcdc

Repeated applications of the recursive rule S aSb


produce a string of the form amSbm, m 0. The recursion is
terminated by an application of the rule S A. The A
rules produce strings of the form cnAdn, n > 0. While the B
rules produce strings of the form aiBbi, i > 0. The language
generated by the rules is {amcnaibidnbm | m 0; n, i > 0}

cabd
aacccabdddbb, acaabbdb,
aacaabbdbb

The rule S aSB | ab generates strings of the form akSBk,


by applying the first option of the rewrite rule k times, or
akabBk by applying the second option to replace S. The
rule B b | bb, applied k times to the strings generated
above will eventually yield akab(b bb)k. This can be
written as L = {akbj | 0 < k j 2k}.

ab
abb, abbb, aabb

a)

The grammar is:

S AB
A aAbb | abb
B cB | c

b)

The grammar is:

S AB
A aAbb |
B cB |

6|P a g e

3 Context-Free
Grammar
10. The grammar is:

S AB
A aA |
B bBcc |

11. The grammar is:

S AB
A aAbb | abb
B cB | c

12. The language consisting of the set of strings {anbmc2n+m | m, n > 0} is generated by
S aScc | aAcc
A bAc | bc
For each leading a generated by the S rules, two c's are produced at the end of the string.
The rule A rules generate an equal number of b's and c's.
13. This grammar can be constructed by first generating as many a's as c's to the left and right.
Subsequently generate as many b's as c's in the middle. This gives us as many a's and b's as c's.
Then we create possibly more c's.
S aSc | B
Generate anBcn, n 0 by applying left option n times.
B bBc | C
Use anBcn and this rule to generate anbmCcn+m, n, m 0.
C cC |
Use anbmCcn+m and this rule to get anbmcjcn+m, n, m, j 0}.
This gives us the language L = {anbmcjcn+m | n, m, j 0}. Let i = j + n + m to get: {anbmci | 0 n +
m i}.
14.

S AAaSb|
A a |

15.

S LR
L aLb |
R bLa |

or

S ASb|
A a | aa | aaa

16. The grammar of this language is


S SaSbS | SbSaS |
17. An a can be specifically placed in the middle position of a string using the rules
A aAa | aAb | bAa | bAb | a
The termination of the application of recursive A rules by the rule A a inserts the symbol a
into the middle of the string. Using this strategy, the grammar
S aAa | aAb | bBa | bBb | a | b
A aAa | aAb | bAa | bAb | a
B aBa | aBb | bBa | bBb | b
generates all strings over fa; bg with the same symbol in the beginning and middle positions.
If the derivation begins with an S rule that begins with an a, the A rules ensure that an a
occurs in the middle of the string. Similarly, the S and B rules combine to produce strings
with a b in the first and middle positions.

7|P a g e

3 Context-Free
Grammar
18.
a)
b)

(a + b)*aa(a + b)*
S AaaA
A aA | bA |

a)
b)

a*bb
S Abb

19.
A aA |

20. S AA aBA abBA abbBA abbA abbaB abba abba


S
A
a

A
B

B
b

21. S SSS aXbSS abbabSS abbabaXbS abbabaabbbS abbabaabbbaXb


abbabaabbbabbab
S
S

22.

S U111

ba

b b

U 0U | 1U |

23.
a)

S SAB
SABAB
ABAB
aABAB
aaBAB
aabBAB
aabAB

8|P a g e

3 Context-Free
Grammar
aabaAB
aabaaAB
aabaaaB
aabaaabB
aabaaabbB
aabaaabb
aabaaabb
b)
S
S
S

A
a

B
A

a
B

B
A

b
A

B
b

c)
S
S
S
S

a
B

B
A

b
A
a

B
b

d) (a*ab*)*
24.
a)

b)

Analysis: the language will produce strings with exactly 3 as and end with a. in
between of the as can have as many bs as we like. The pattern of the string will
be : BaBaBa.
S bS | aA
A bA | aB
B bB | a
Analysis: the language will produce strings with at least one a and one b. The
pattern of the string will be : AbBBaA.
S aS | bA
A bbA | aB
B aB |

9|P a g e

3 Context-Free
Grammar
25.

a)

S aA
A aA | aB |
B bA

b)
S aA aaA aaaA aaaaB aaaabA aaaabaB aaaababA aaaababaA
aaaababa.
26. L(G1) = {bmanbm : n 0, m 0}
S aSbb |

27. G1 = ({a, b, S}, {a, b}, S, P) with P defined by:

S bA bbS bbaS failed.


S aS abA abaA ababS ababbA ababb
S bA baA baaA baabS baabaS baababA baabab

28. a)

b)
S
a

S
S

b
A

A
a

A
b

A
a

S
b

A
b

S
a

S
b

29.
a)
b)
c)
d)
30.

a(a + b)*b
aa*bb* or a+b+
a*bba*
(a* + ba*b)*

S aS | aA |
A bA | bB |
B cB |

31. The language (a b)*aa(a b)*bb(a b)* is generated by


G1:
S1 aS1 | bS1 | aA
A aB

10 | P a g e

3 Context-Free
Grammar
B aB | bB | bC
C bD
D aD | bD |
G2 generates the strings (a b)*bb(a b)*aa(a b)*
G2:
S2 aS2 | bS2 | bE
E bF
F aF | bF | aG
G aH
H aH | bH |
A grammar G that generates
(a b)*aa(a b)*bb(a b)* (a b)*bb(a b)*aa(a b)*
can be obtained from G1 and G2. The rules of G consist of the rules of G1 and G2 augmented
with S S1 | S2 where S is the start symbol of the composite grammar. The alternative in
these productions corresponds to the in the definition of the language.
While the grammar described above generates the desired language, it is not regular. The
rules S S1 | S2 do not have the form required for rules of a regular grammar. A regular
grammar can be obtained by explicitly replacing S1 and S2 in the S rules with the right-hand
sides of the S1 and S2 rules. The S rules of the new grammar are
S aS1 | bS1 | aA
S aS2 | bS2 | bE
The strategy used to modify the rules S S1 | S2 is an instance of a more general rule
modification technique known as removing chain rules.
32. S aS | bS | aA
A aB
B aB | bB | aC
C aD
D aD | bD |
33. G1 generates the strings (a b)*ab(a b)*ba(a b)*
G1:
S1 aS1 | bS1 | aA
A bB
B aB | bB | bC
C aD
D aD | bD |
G2 generates the strings (a b)*ba(a b)*ab(a b)*
G2:
S2 aS2 | bS2 | bE
E aF
F aF | bF | aG
G bH
H aH | bH |
Thus a grammar G that generates (a b)*ab(a b)*ba(a b)* (a b)*ba(a b)*ab(a
b)*
S aS1 | bS1 | aA
S aS2 | bS2 | bE
S1 aS1 | bS1 | aA
11 | P a g e

3 Context-Free
Grammar
A bB
B aB | bB | bC
C aD
D aD | bD |
S2 aS2 | bS2 | bE
E aF
F aF | bF | aG
G bH
H aH | bH |
G:

or

S aS | bS | aA | bE
A bB
B aB | bB | bC
C aD
D aD | bD |
E aF
F aF | bF | aG
G bH
H aH | bH |

34. S bS | aA |
A bA | aB
B bB | aS
35. The language ((a )b(a ))* is generated by the grammar
S aA | bB |
A bB
B aS | bB |
This language consists of all strings over {a, b} in which every a is preceded or followed by a
b. An a generated by the rule S aA is followed by a b. An a generated by B aS is
preceded by a b.
36. When we start and we read a b then we are no closer to reading aba and we remain in the
same state. However, if we read an a, we may encounter ba next, so we proceed to a state A,
where we may not read ba next. If we read an a in this state A there still might follow Ba, so we
remain in state A. On the other hand, when we read a B in state A, we end up in a state where
we may not read an a because we have just read ab. If the character we read then is a b we are
back at the initial state where the string to follow may not be aba. Of course, we are always
allowed to stop reading and accept that word in any state. This yields the following grammar
S bS | aA |
string to follow may not be aba
A aA | bB |
string to follow may not be ba
B bS |
string to follow may not be a
37.

G:

S bS | aA | aC
A aB
B bB | bD |
C bS
12 | P a g e

3 Context-Free
Grammar
D aB
38.

G:

S aS | aA | bD | bG
A bB
B aB | bC |
C aC |
D aE
E aE |bF |
F aF |
G aG | bH
H aI |
I aI |

39. The variables of the grammar indicate whether an even or odd number of ab's has been
generated and the progress toward the next ab. The interpretation of the variables is
Variable
S
A
B
C

Parity
even
even
odd
odd

Progress toward ab
none
a
none
a

The rules of the grammar are


S aA | bS
A aA | bB
B aC | bB |
C aC | bS |
A derivation may terminate with a -rule when a B or a C is present in the sentential form
since this indicates that an odd number of ab's have been generated.
40. The objective is to construct a grammar that generates the set of strings over {a, b} containing
an even number of a's or an odd number of b's. In the grammar,
S aOa | bEa | bOb | aEb |
Ea aOa | bEa |
Oa aEa | bOa
Ob aOb | bEb |
Eb aEb | bOb
the Ea and Oa rules generate strings with an even number of a's. The derivation of a string
with a positive even number of a's is initiated by the application of an S rule that generates
either Ea or Oa. The derivation then alternates between occurrences of Ea and Oa until it is
terminated with an application of the rule Ea .
In a like manner, the Ob and Eb rules generate strings with an odd number of b's. The string
aab can be generated by two derivations; one beginning with the application of the rule S
aOa and the other beginning with S aEb.
41.
a)

{anbmbn | n 0, m > 0} or we can write {ambn | n > m 0}.

13 | P a g e

3 Context-Free
Grammar
b)
c)

{anb2md2ma2n | n 0, m > 0}
{anambn | n, m 0}

a)
b)

(a+b+)+
The rules S aS and S Sb allow the generation of leading a's or trailing b's in any
order. Two leftmost derivations for the string aabb are
S aS
S Sb
aSb
aSb
aabb
aabb
The derivation trees corresponding to the derivations of (b) are
S
S

42.

c)

S
S
a

S
b

b
S

43. The regular expression for this language is a+b+b .


44. {amanbn | m > 0, n 0} or we can write {ambn | m > n 0}.
45.
a)
b)
c)
d)
e)
f)

The language {a, b}*


The set {a, b}*{a}
The set {ba}*{b}
The set {b*ab*}
{a}*{b}{a}*{b}{a}*, the set of strings in *{a, b} containing exactly two bs
The set of strings over {a, b} that are not palindromes but could be made into
palindromes by changing one a to b or vice versa
g) The set of even-length strings in {a, b}*
h) The set of odd-length strings in {a, b}*
46.
a)
b)
c)
d)
e)
f)

S aSb | Sb |
S aSb | Sb | b
S aSbb |
S aSb | aSbb |
S aSbb | aSb | aS |
S aAb | aA;
A aAbb | aAb | aA |

14 | P a g e

3 Context-Free
Grammar
47.
Table 1.

Table 2.

No.

Language

No.

1.

(a + b)*

2.

(a + b)+

3.

(a + b)

4.

(ab)

10

5.

a*bb

6.

aa*bb*

7.

a*bba*

8.

a(a + b)*b

9.

(a + b)*bb(a + b)*

10.

(a + b)*aa(a + b)*

Grammars

S aA
A aA | bB
B bB |
S Abb
A aA |

CFG or RG

RG

RG

S aS | bS | a | b
S AaaA
A aA | bA |
S aA
Ab
S aS | bS | bA
A bB
B aB | bB |
S aA
A aA | bA | b
S aS | bS |
S aS | bA
A bB
B aB |

RG

Sa|b

RG

CFG
RG
RG
RG
RG
RG

48.
Table 3.

Table 4.

No.

1.
2.

Language

L = {wwR : w (a + b)*}
n 2m n

L = {a b c | n, m > 0}

No.

4
3

3.

L = (bb*a + ab*b)*

4.

Let L be the language of all


strings over {a, b} that have the
same number of as as bs

5.

L = {anbn | n > 0} {bnan | n > 0}

Grammars

CFG
or RG

S aSb | bSa | SS |
S bA | aB |
A bA | aS
B bB | bS
SX|Y
X aXb | ab
Y bYa | ba
S ASC | B
Aa
Cc
B bbB | bb

CFG

S aSa | bSb |

CFG

Generate 3
possible
strings

ab, ba, aaba

RG

CFG

CFG

15 | P a g e

You might also like