Professional Documents
Culture Documents
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.
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
S
a
c
S
A
c
c
d)
d
d
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)
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)*
S AB
A aAb | Ab |
B bBc |
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}
9.
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
cabd
aacccabdddbb, acaabbdb,
aacaabbdbb
ab
abb, abbb, aabb
a)
S AB
A aAbb | abb
B cB | c
b)
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 |
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
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 |
A
B
B
b
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 |
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 |
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
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
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.
5.
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
RG
CFG
CFG
15 | P a g e