Вы находитесь на странице: 1из 8

Solutions of the exercises on Grammars and Regular Expressions

Olena Gryn

olg@itu.dk

December, 2006 updated

Exercise 1 on slide 6

What is the language generated by G 1 = ({a, b, S, T, U }, {a, b}, S, P ) if P is altered to:

S T

S bSb

T aT

Solution

L(G 1 ) = {b m a n b m : n 0, m 0}

T

Exercise 2 on slide 6

Make a grammar generating {a n b 2n : n 0}

Solution

G 1 = ({a, b, S}, {a, b}, S, P ) with P deﬁned by:

S aSbb|

Exercise on slide 12

Why is the (inductive) deﬁnition of a regular expression a proper deﬁnition (it seems to be recursively deﬁned)?

Solution

It is because such deﬁned regular set can be generated by a regular grammar. This follows from the fact, that every regular set deﬁned by such deﬁnition can be recognized by a ﬁnite-state automaton. And this can be proved from the fact, that {a},{ }, are recognized by a ﬁnite-state automaton, and A , (A B), (AB) are recognized by a ﬁnite-state machine whenever A and B are (see pp. 657-658 in Discrete mathematics and Its Applications, by Rosen).

1

Exercise 1 on slide 13

For each of the following regular expressions give two strings that are members of the language it represents and give two that are not:

 1) a ∗ b ∗ 2) a(ba) ∗ a. Solution 1) aaabb and a are members, aba and ba are not members. 2) aa and abababaa are members, a and ababa are not members.

Exercise 2 on slide 13

Give a regular expression for the intersection, union, and concatenation respectively of the two languages: A = {w ∈ {0, 1} : w begins with 11} and B = {w ∈ {0, 1} : w ends with 00}

Solution

a) = 11(1 0) 00

b) = (11(1 0) ) ((1 0) 00)

a) AB = 11(1 0) 00

A B

A B

Exercise 3 on slide 13

Give a regular expression for decimal digits.

Solution

(0 (1 (2 (3 (4 (5 (6 (7 (8 9)))))))))

Exercise 4 on slide 13

Let R be a regular expression over some set.

a) Do (R ∪ ∅) and (R ) denote the same set?

b) What set does (R ) represent?

c) What set does (R) represent?

Solution

a) (R ∪ ∅) is R since does not add anything to their union, and (R ) means appending nothing

to all strings in R which is also R, hence they are the same (by Jens Christian Godskesen).

b) (R ) represents the set R and empty string.

c) (R) represents R.

2 a,b
a,b
a
qS
qT
qacc
a,b
b
qU

Figure 1. NFA N 1 for grammar G 1

Exercise 1 on slide 19

Construct a NFA N 1 from the grammar G 1 slide 6: G 1 = ({a, b, S, T, U }, {a, b}, S, P ), where P is:

S a|b|aT |aU |bT |bU

T a

U b

Solution

1. Create a state for each non-terminal and add a single accepting state q acc .

2. Add transitions q S q T for S aT , q S q U for S aU , q S q T for S bT , q S q U

a

a

b

b

for S bU , q S q acc for S a, q S q acc for S b, q T q acc for T a, q U q acc for U b (See Figure 1.)

a

b

a

b

Exercise 2 on slide 19

Argue why L(G 1 ) =

L(N 1 ).

Solution

To prove that L(G 1 ) = L(N 1 ), let us deﬁne all words that leads to a ﬁnal state in N 1 which is L(N 1 )(1), and all words that can be generated in G 1 which is L(G 1 )(2):

(1) L(N 1 ) = {w : w is concatenated symbols from the transitions in a path from start state to a ﬁnal state} (2) L(G 1 ) = {w : w is concatenated symbols from the productions in a path from start symbol to a terminal}. Since each transition in N 1 corresponds to a certain production from G 1 , and transitions are connected by the states that correspond to certain non-terminals, which connect corresponding productions, L(G 1 ) = L(N 1 ). For this particular example L(G 1 ) = {a, b, ab, bb, aa, ba} = L(N 1 ).

Exercise 1 on slide 20

Construct a regular grammar from the FA M 1 (See Figure 2.) on slide 4 from the lecture about FA.

3 1
0
0
1
1
q1
q2
0
q3

Figure 2. FA M 1

Solution

1. Select a non-terminal for each state in M 1 , selecting a start symbol for the initial state: S for

q 1 , A for q 2 and B for q 3 .

2.

q 3 , and

B

A 0S for q 2 q 1 , A 1B and A 1 for q 2

Add productions corresponding to all transitions: S 0S for q 1 q 1 , S 1A for q 1 q 2 ,

0

0B and B 0 for q 3

1

q 3 , B 1B and B 1 for q 3

0 q 3 , since q 3 is an accepting state.

1

0

1

G

= ({0, 1, S, A, B}, {0, 1}, S, P ), where P is:

S 1A|0S

A 1B|0S|1

B 0B|1B|0|1

Exercise 2 on slide 20

Argue why L(M ) = L(G) for the regular grammar G constructed from M in the proof sketch above.

Solution

See Exercise 2 on slide 19.

Exercise 3 on slide 20

Argue that any FA M is equivalent to a NFA where the initial state has no incoming transition.

Solution

Any FA M has an equivalent NFA M where the initial state has no incoming transitions, if:

(a)

q 0 remains in the M

(b) and the same outgoing transitions as q 0 has, were added to q

this M was made from M by adding a new initial state q 0 instead of the old one q 0 , where

0 .

4

Exercises on pages 638-639 in Discrete mathematics and Its Ap- plications

Exercise 4

Let G = ({S, A, B, a, b}, {a, b}, S, P ), where P consist of:

a) AB, A ab, B bb

S

b) aA, A a, B ba

S AB, S

c) AA, A aB, A ab, B b

S AB, S

d) B, A aaA, A aa, B bB, B b

e) S AB, A aAb, B bBa, A λ, B λ.

Find the languages generated by G.

S AA, S

Solution

a) {abbb}

b) {aa, aba}

c) {abab, abb}

d) {a 2m , b n }, where m 2, n 1

e) {a m b m b n a n }, where m, n 0

Exercise 7

Construct a derivation of 0 2 1 4 using the grammar G 1 (a) and G 2 (b) in Example 6.

Solution

a) S 0S 00S 00S1 00S11 00S111 00S1111 001111.

b) S 0S 00S 001A 0011A 00111A 001111.

Exercise 8

Show that the grammars G 1 (a) and G 2 (b) in Example 6 generate the set {0 m 1 n |m, n =

 0, 1, 2, }. Solution
 a) m = 0, n = 0, S → λ m = 0, n ≥ 1, S → S1 → → S1 n → 1 n m ≥ 1, n = 0, S → 0S → → 0 m S → 0 m m ≥ 1, n ≥ 1, S → 0S → → 0 m S → 0 m S1 → → 0 m S1 n → 0 m 1 n b) m = 0, n = 0, S → λ m = 0, n = 1, S → 1 m = 0, n ≥ 2, S → 1A → → 1 n−1 A → 1 n−1 1 → 1 n m ≥ 1, n = 0, S → 0S → → 0 m S → 0 m m ≥ 1, n = 1, S → 0S → → 0 m S → 0 m 1

5

m 1, n 2, S 0S

0 m S 0 m 1A

0 m 1 n1 A 0 m 1 n1 1 0 m 1 n

Exercise 11

Find a phrase-structure grammar for each of the following languages.

Solution

 a) G = ({0, 1, S}, {0, 1}, S, P ), where P consist of S → 00S, S → λ b) G = ({0, 1, S, A, B}, {0, 1}, S, P ), where P consist of S → 1A, A → 0B, B → 00B, B → λ d) G = ({0, 1, S, A}, {0, 1}, S, P ), where P consist of S → 0000000000A, A → 0A|λ

Exercise 14

Find a context-free grammar that generates the set of all palindromes over the alphabet {0, 1}

Solution

G = ({0, 1, S}, {0, 1}, S, P ), where P consist of S 0S0, S 1S1, S 0, S 1, S λ.

Exercise 15

Let G 1 and G 2 be context-free grammars. generating the language L(G 1 ) and L(G 2 ) respec- tively. Show that there is a context-free grammar generating each of the following sets:

a) L(G 1 ) L(G 2 )

b) L(G 1 )L(G 2 )

c) L(G 1 )

Solution

Let G 1 = ({w 1 ,

G 2 = ({q 1 ,

a) Grammar G 3 , that generates the set L(G 1 ) L(G 2 ), is

G 3

P 2 (S S 1 |S 2 )).

w k , S 1 , A 1 ,

,

A m }, {w 1 ,

w k }, S 1 , P 1 ) and

,

, q k , S 2 , B 1 ,

,

q k , w 1 ,

, B m }, {q 1 ,

, , q k }, S 2 , P 2 ).

, A m , B 1 ,

=

({q 1 ,

,

w k , S, S 1 , S 2 , A 1 ,

 , B m }, {q 1 , , q k , w 1 , , w k }, S, P 1 ∪ , B m }, {q 1 , , q k , w 1 , , w k }, S, P 1 ∪

b) Grammar G 3 , that generates the set L(G 1 )L(G 2 ), is

G 3

P 2 (S S 1 S 2 )).

=

({q 1 ,

,

q k , w 1 ,

,

w k , S, S 1 , S 2 , A 1 ,

, A m , B 1 ,

c) Grammar G 3 , that generates the set L(G 1 ) , is

G 3 = ({w 1 ,

,

w k , S, S 1 , A 1 ,

,

A m }, {w 1 ,

,

w k }, S, P 1 (S λ|S 1 S)).

6

Exercise 18

Let G = ({a, b, c, S}, {a, b, c}, S, P ), where P consists of S abS|bcS|bbS|a|cb. Construct derivation trees for a) bcbba, b) bbbcbba, c) bcabbbbbcb.

Solution

a) S

bc

S

bb

b) S

bb

S

bc

S

a

S

bb

c) S

bc

S

ab

S

bb

S

a

S

bb

S

cb

Exercise 22

a) Explain what the productions are in a grammar if Backus-Naur form for productions is as follows?

7

< expression >::= (< expression >)| < expression > + < expression > | < expression >

< expression > | < variable >

< variable >::= x|y

b) Find a derivation tree for (x y) + x in this grammar.

Solution

a) S (S)|S + S|S S|A, A x|y.

b)

(

S

A

x

S

S

S

*

 + S ) A S x A y

8