1. Full mark is out of 20
Cairo University Midterm Exam
Faculty of Computers & Information Spring 2013
Dept. of Computer Science Time 1 hour
Compilers DesignCS419
Solve as much as you can. Full mark is out of twenty
Part A: Regular expressions:
1. ∑ ={a, b, c}; Formally describe all words for the language L that start with an 'a'
followed by any number of 'b'’s and then end with 'c'. Give examples for words in
this language. [1 mark]
Solution:
L = a b*c
Give examples for words in L
{ac, abc,abc,abbc, abbbc …..}
2. ∑ ={a, b}; All words with even count of letters. [2 mark]
Solution:
((a+b)(a+b))*
examples for words in L
{ Λ, aa,bb,ab,ba,bb,aaaa,…..}
3. ∑ ={a, b}; Construct a regular expression for:
a. All strings that end in a double letter. [2 mark]
b. All strings that do not end in a double letter. [2 mark]
Solution:
a. (a + b)*(aa + bb)
b. ((a + b)*(ab + ba)) + a + b + Λ
Note: Provide for strings containing zero or one letter too by + Λ
4. ∑ ={a, b};Construct a regular expression for all strings in which the total number
of 'a'’s is divisible by 3 no matter how they are distributed, such as “aabaabbaba”.
[2 mark]
Solution:
(b*ab*ab*ab*)*
5. ∑ ={a, b}, State whether the following pair of regular expressions are equivalent
or not and explain your answer.(a*b*) and (ab)* [2
mark]
Solution:
i. NOT equal
The language defined by the expression on the right contains the word abab, but
the language defined by the expression on the left does not.
2. Full mark is out of 20
b
b
b
a,b
ba
S
S
S3
+
ba
S
S
S2
+
S
ba,b
-+
S
Part B: Finite Automata:
∑ ={a, b}.
6. Build two FAs (with different number of states) that accept only the word "ab".
[1
mark]
Solution:
RE = ab
7. Build FA with one state that accepts anything. [1 mark]
Solution:
RE = (a+b)*
8. Build FA that accepts only words that have an even number of letters.
[2
mark]
Solution:
9. All words with even count of letters where even positions must contain an “a”,
and the first letter is letter number one. (a+b)a((a+b)a)*.
For example
Position 1 2 3 4 5
Example1 a a a a b
Example2 b a b a a
[2 mark]
Solution:
a,b
a,b
-
3. Full mark is out of 20
A
a
1 2 3 4
Part C: NFA:
10. Construct an NFA (with ε transitions) for (ab)*(aab)* [3 mark]
Solution:
Part D: NFA to DFA:
11. For the given NFA, construct the equivalent DFA [3 mark]
Solution:
Closure of 1 ={1,2,4}
Closure of 2 = {2}
Closure of 3 = {2, 3,4}
Closure of 4 = {4}
-
a,
b
aΛ Λ
Λ