Exercises #1 (Solutions)
Exercises #1 (Solutions)
1 Regular Languages
Exercise 1.
You are given the following state transition diagram for the automaton N1 :
1 1
q0 1 q1 0 q2
q3
N1
1. Construct an NFA N2 that recognizes the language L(N2 ) = L(N1 )∗ .
2. Draw the corresponding state-transistion diagram of N2
Solution.
1
1
q ε q q
4 0 1
ε
0 0
ε
q3 q2 1
N2
3. Give a formal description of N2 as a 5-tuple.
Solution.
N2 = ({q0 , q1 , q2 , q3 , q4 }, {0, 1}, δ, q4 , {q1 , q2 , q4 })
1
0 1 ε
q0 {q3 } {q0 , q1 } ∅
q {q2 } ∅ {q0 }
δ= 1
q2 ∅ {q2 } {q0 }
q3 ∅ ∅ ∅
q4 ∅ ∅ {q0 }
Exercise 2.
You are given the following state transition diagram for the automaton N :
1 0
0, 1 0 1
q0 q1 q2 q3
N
1. Give a regular expression that describes the language L(N ).
Solution.
R = (0 ∪ 1)1∗ 0+ 1
2. Try to describe L(N ) in your own words.
Solution.
A string in L(N ) has to be of at least length 3. It can start with either 1
or 0 but has to end in 1. The last symbol has to be immediately preceded
by at least one 0. The first symbol can be followed by any number of 1s.
Exercise 3.
ε
q3 q4 q5 q6
1 1 0
2
2. Determine which of the following strings are matched by R:
ε, 01, 110, 010, 001, 0101, 0001010, 1111111
Solution.
Accept: ε, 01, 110, 001, 0101
Reject: 010, 0001010, 1111111
2 Context-Free Languages
Exercise 4.
Suppose you have a context-free language that only includes strings which are
palindromes over the alphabet Σ = {x, y}:
ε, ε → ε; x, ε → ε; y, ε → ε
q3 q2 x, x → ε; y, y → ε
ε, $ → ε
Exercise 5.
Prove that the derivation of any string of length n ≥ 1 in CNF takes exactly
2n − 1 steps.
Solution.
Since the context-free grammar is in Chomsky normal form, each step of a
derivation either adds exactly one non-terminal variable or changes exactly one
non-terminal variable to a terminal one (or to ε). Since n ≥ 1, the rule S → ε
can never occur in any derivation of the string, and there are exactly n steps
changing a non-terminal variable to a terminal one. All these non-terminal
variables are added one by one starting from the starting variable S resulting
in another n − 1 operations, in total 2n − 1.
3
Exercise 6.
Describe the language of the context-free grammar G = ({A, B}, {0, 1, 2}, R, A)
with rules:
A → 00A1 | B
B → ε | 22B
Solution.
{02n 22m 1n | n, m ≥ 0}