Você está na página 1de 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 G1 = ({a, b, S, T, U }, {a, b}, S, P ) if P is altered to:

S→T S → bSb T → aT T →

Solution
L(G1 ) = {bm an bm : n ≥ 0, m ≥ 0}

Exercise 2 on slide 6
Make a grammar generating {an b2n : n ≥ 0}

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

S → aSbb|

Exercise on slide 12
Why is the (inductive) definition of a regular expression a proper definition (it seems to be
recursively defined)?

Solution
It is because such defined regular set can be generated by a regular grammar. This follows from
the fact, that every regular set defined by such definition can be recognized by a finite-state
automaton. And this can be proved from the fact, that {a},{}, ∅ are recognized by a finite-state
automaton, and A∗ , (A ∪ B), (AB) are recognized by a finite-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) A ∩ B = 11(1 ∪ 0)∗ 00
b) A ∪ B = (11(1 ∪ 0)∗ ) ∪ ((1 ∪ 0)∗ 00)
a) AB = 11(1 ∪ 0)∗ 00

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 N1 for grammar G1

Exercise 1 on slide 19
Construct a NFA N1 from the grammar G1 slide 6: G1 = ({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 qS → qT for S → aT , qS → qU for S → aU , qS → qT for S → bT , qS → qU
a a b b

for S → bU , qS → qacc for S → a, qS → qacc for S → b, qT → qacc for T → a, qU → qacc for


a b a b

U → b (See Figure 1.)

Exercise 2 on slide 19
Argue why L(G1 ) = L(N1 ).

Solution
To prove that L(G1 ) = L(N1 ), let us define all words that leads to a final state in N1 which is
L(N1 )(1), and all words that can be generated in G1 which is L(G1 )(2):
(1) L(N1 ) = {w : w is concatenated symbols from the transitions in a path from start state to a
final state}
(2) L(G1 ) = {w : w is concatenated symbols from the productions in a path from start symbol
to a terminal}.
Since each transition in N1 corresponds to a certain production from G1 , and transitions are
connected by the states that correspond to certain non-terminals, which connect corresponding
productions, L(G1 ) = L(N1 ).
For this particular example L(G1 ) = {a, b, ab, bb, aa, ba} = L(N1 ).

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

3
1
0 0

1 1
q1 q2 q3
0

Figure 2. FA M 1

Solution
1. Select a non-terminal for each state in M1 , selecting a start symbol for the initial state: S for
q1 , A for q2 and B for q3 .
2. Add productions corresponding to all transitions: S → 0S for q1 → q1 , S → 1A for q1 → q2 ,
0 1

A → 0S for q2 → q1 , A → 1B and A → 1 for q2 → q3 , B → 1B and B → 1 for q3 → q3 , and


0 1 1

B → 0B and B → 0 for q3 → q3 , since q3 is an accepting state.


0

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 0 where the initial state has no incoming transitions, if:
(a) this M 0 was made from M by adding a new initial state q00 instead of the old one q0 , where
q0 remains in the M 0
(b) and the same outgoing transitions as q0 has, were added to q00 .

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) S → AB, A → ab, B → bb
b) S → AB, S → aA, A → a, B → ba
c) S → AB, S → AA, A → aB, A → ab, B → b
d) S → AA, S → B, A → aaA, A → aa, B → bB, B → b
e) S → AB, A → aAb, B → bBa, A → λ, B → λ.
Find the languages generated by G.

Solution
a) {abbb}
b) {aa, aba}
c) {abab, abb}
d) {a2m , bn }, where m ≥ 2, n ≥ 1
e) {am bm bn an }, where m, n ≥ 0

Exercise 7
Construct a derivation of 02 14 using the grammar G1 (a) and G2 (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 G1 (a) and G2 (b) in Example 6 generate the set {0m 1n |m, n =
0, 1, 2, ...}.

Solution
a) m = 0, n = 0, S → λ
m = 0, n ≥ 1, S → S1 → ... → S1n → 1n
m ≥ 1, n = 0, S → 0S → ... → 0m S → 0m
m ≥ 1, n ≥ 1, S → 0S → ... → 0m S → 0m S1 → ... → 0m S1n → 0m 1n

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

5
m ≥ 1, n ≥ 2, S → 0S → ... → 0m S → 0m 1A → ... → 0m 1n−1 A → 0m 1n−1 1 → 0m 1n

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 G1 and G2 be context-free grammars. generating the language L(G1 ) and L(G2 ) respec-
tively. Show that there is a context-free grammar generating each of the following sets:
a) L(G1 ) ∪ L(G2 )
b) L(G1 )L(G2 )
c) L(G1 )∗

Solution
Let G1 = ({w1 , ..., wk , S1 , A1 , ..., Am }, {w1 , ..., wk }, S1 , P1 ) and
G2 = ({q1 , ..., qk , S2 , B1 , ..., Bm }, {q1 , ..., qk }, S2 , P2 ).
a) Grammar G3 , that generates the set L(G1 ) ∪ L(G2 ), is
G3 = ({q1 , ..., qk , w1 , ..., wk , S, S1 , S2 , A1 , ..., Am , B1 , ..., Bm }, {q1 , ..., qk , w1 , ..., wk }, S, P1 ∪
P2 ∪ (S → S1 |S2 )).

b) Grammar G3 , that generates the set L(G1 )L(G2 ), is


G3 = ({q1 , ..., qk , w1 , ..., wk , S, S1 , S2 , A1 , ..., Am , B1 , ..., Bm }, {q1 , ..., qk , w1 , ..., wk }, S, P1 ∪
P2 ∪ (S → S1 S2 )).

c) Grammar G3 , that generates the set L(G1 )∗ , is


G3 = ({w1 , ..., wk , S, S1 , A1 , ..., Am }, {w1 , ..., wk }, S, P1 ∪ (S → λ|S1 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 @ S

a
b)
S
@
@
bb @ S
@
@
bc @ S
@
@
bb @ S

a
c)
S
@
@
bc @ S
@
@
ab @ S
@
@
bb @ 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
PP
  P PP
 P
S + S
P P
  P PP
 P
( S ) A
P P
 PP
 PP
S * S x

A A

x y

Você também pode gostar