Formal Languages And Automata Theory 1
UNIT-III
Objective:
To familiarize how to employ regular expressions.
Syllabus:
Regular sets, regular expressions, identity rules, construction of finite Automata
for a given regular expressions and its inter conversion, pumping lemma of regular
sets, closure properties of regular sets (proofs not required),applications of regular
languages.
Learning Outcomes:
Students will be able to:
understand the regular sets and how to represent the regular expressions.
construct finite Automata for a given regular expression and viceversa.
list closure properties of regular languages.
understand the different applications of regular languages.
3.Learning Material
3.1 Regular set:
A language is a regular set (or just regular) if it is the set accepted by some finite
automaton.
Example:
L= {0, 1, 10, 00, 01, 11, 000, 101, .................} is a regular set representing any no
of 0’s and any no of 1’s.
3.2 Regular expression:
The languages accepted by finite automata are easily described by simple
expressions called regular expressions.
Let Σ be an alphabet. The regular expressions over Σ and the sets that they denote
are defined recursively as follows.
1) Ø is a regular expression and denotes the empty set.
2) ε is a regular expression and denotes the set { ε }.
3) For each a in Σ, a is a regular expression and denotes the set {a}.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 2
4) If r and s are regular expressions denoting the languages R and S, respectively,
then
(r + s), (rs), and (r*) are regular expressions that denote the sets R U S, RS, and
R*, respectively.
3.2.1Some Examples on Regular expressions
1. Write regular expressions for each of the following languages over ∑={0, 1}.
a) The set representing {00}.
00
b) The set representing all strings of 0's and 1's.
(0+1)*
c) The set of all strings representing with at least two consecutive 0’s.
(0 + 1)*00(0 + 1)*
d) The set of all strings ending in 011.
(0 + 1)*011
e) The set of all strings representing any number of 0's followed by any number
of 1’s followed by any number of 2's.
0*1*2*
f) The set of all strings starting with 011.
011 (0 + 1)*
2. Write regular expressions for each of the following languages over ∑={a, b}.
a) The set of all strings ending with either a or bb.
(a+b)* (a + bb)
b) The set of strings consisting of even no. of a’s followed by odd no. of b’s.
(aa)*(bb)*b
c) The set of strings representing even number of a’s.
(b* a b* a b*) * + b*
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 3
3.3 Identity Rules Related to Regular Expressions
Given r, s and t are regular expressions, the following identities hold:
∅* = ε
ε* = ε
r+ = rr* = r*r
r*r* = r*
(r*)* = r*
r+s=s+r
(r + s) + t = r + (s + t)
(rs)t = r(st)
r(s + t) = rs + rt
(r + s)t = rt + st
(ε + r)* = r*
(r + s)* = (r*s*)* = (r* + s*)* =(r+s*)*
r+∅=∅+r=r
rε=εr=r
∅L=L∅=∅
r+r=r
ε + rr* = ε + r*r = r*
3.4 Construction of Finite automata for a given regular expression
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 4
Equivalence of Finite Automata and Regular Expressions
The languages accepted by finite automata are precisely the languages
denoted by regular expressions.
For every regular expression there is an equivalent NFA with ε -
transitions.
For every DFA there is a regular expression denoting its language.
Let r be a regular expression. Then there exists an NFA with ε -transitions
that accept L(r).
Zero operators:
The expression r must be ε, Ø, or a for some a in Σ. The NFA’s for zero operators
are
One or more operators:
Let r have i operators. There are three cases depending on the form of r.
Case 1: Union (r = rl + r2.)
There are NFA’s M1 = (Q1, ∑1, δ1, q1, {f1}) and M2=(Q2, ∑2, δ2, q2, {f2}) with
L(M1) = L(r1) and L(M2) = L(r2).
Construct
M = (Q1 ∪ Q2 ∪ {q0, f0}, ∑1 ∪ ∑2, δ, q0, {f0}) where δ is defined by
i) δ (q0, ε) = {q1,q2}
ii) δ (q, a) = δ1(q ,a) for q in Q1-{f1} and a in ∑1 ∪ { ε }
iii) δ (q, a) = δ2(q ,a) for q in Q2-{f2} and a in ∑2 ∪ { ε }
iv) δ (f1, ε) = δ1(f2, ε) = { f0 }
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 5
L(M) = L(M1) ∪ L(M2)
Case 2: Concatenation (r = r1 r2).
Let M1 and M2 be as in Case 1 and construct M = (Q1 ∪ Q2, ∑1 ∪ ∑2, δ, q1, {f2})
where δ is defined by
i) δ (q, a) = δ1(q ,a) for q in Q1-{f1} and a in ∑1 ∪ { ε }
ii) δ (f1, ε) = {q2}
iii) δ (q, a) = δ2(q ,a) for q in Q2 and a in ∑2 ∪ { ε }
L(M) ={xy| x is in L(M1) and y is in L(M2)} and L(M) = L(M1)L(M2)
Case 3: Closure (r = r1*)
Let M1 = (Q1, ∑1, δ1, q1, {f1}) and L(M1) = r1.
Construct M = (Q1 ∪ {q0,f0}, ∑1 , δ, q0, {f0}), where δ is defined by
i) δ (q0, ε) = δ (f1, ε) = {q1,f0}
ii) δ (q, a) = δ1(q ,a) for q in Q1-{f1} and a in ∑1 ∪ { ε }
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 6
Example:
1. Construct an NFA for the regular expression 01*+1
Regular expression is of the form r1 + r2, where r1 =01* and r2 = 1.
The automaton for r2 is
Express r1 as r3 and r4, where r3=0 and r4= 1*
The automaton for r3 is
r4 is r5* where r5=1
The NFA for r5 is
To construct an NFA for r4 = r5* use the construction of closure. The resulting
NFA for r4 is
Then, for r1 = r3 r4 use the construction of concatenation.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 7
Finally, use the construction of union to find the NFA for r = r1 + r2
3.5 Construction of regular expressions for the given finite Automata:
Arden’s Theorem
Let P and Q be two regular expressions over Σ,and if P does not contain epsilon,
then R=Q+RP has a unique solution R=QP*.
Procedure:
Assume the given finite automata should not contain any epsilons.
Step 1: Find the reachability for each and every state in given Finite automata.
Reachability of a state is the set of states whose edges enter into that state.
Step 2: For the initial state of finite automata ,add epsilon to the reachability
equation.
Step 3: Solve the equations by using Arden’s Theorem.
Step 4: Substitute the results of each state equation into the final state
equation,to get the regular expression for the given DFA.
Example:
1. Construct regular expression for the given finite automaton.
The given Finite Automata is not having any ’s( epsilons).
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 8
Step 1: Find the reachability for each and every state in given Finite automata.
Reachability of a state is the set of states whose edges enter into that state.
q0=q0 0 1
q1= q0 1 + q10 + q2 1 2
q2= q1 1 + q20 3
Step 2: For the initial state of finite automata, add epsilon to the reachability
equation.
q0=q0 0 +
Step 3: Solve the equations by using Arden’s Theorem.
After applying arden’s theorem for equation 3
q2=q1 10* 4
Substitute equation 4 in equation 2
q1= q0 1 + q10+q1 10*
q1= q0 1 + q1(0+10*) 5
Apply arden’s theorem on equation 5
q1= q01 (0+10*)* 6
Apply arden’s theorem on equation 1
q0=q0 0 +
q0= 0* 7
Substitute equation 7 in equation 6
q1= 0* 1 (0+10*)* 8
Step 4: Substitute the results of each state equation into the final state equation,
to get the regular expression for the given DFA.
q2= 0* 1 (0+10*)* 10*
Therefore, the regular expression for the given DFA is 0* 1 (0+10*)* 10*.
3.6 Pumping Lemma for Regular Sets:
Pumping lemma, which is a powerful tool for proving certain languages non-
regular.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 9
It is also useful in the development of algorithms to answer certain
questions concerning finite automata, such as whether the language
accepted by a given FA is finite or infinite.
Lemma
Let L be a regular set. Then there is a constant n such that if z is any word in L,
and |z| > n, we may write z=uvw in such a way that |uv| ≤ n, v ≥ 1, and for all i>
0, uviw is in L. Furthermore, n is no greater than the number of states of the
smallest FA accepting L.
Example:
The set L = {0i2 | i is an integer, i ≥ 1], which consists of all strings of 0’s
whose length is a perfect square, is not regular.
Assume L is regular and let n be the integer in the pumping lemma.
Let z = 0n2.
By the pumping lemma, 0n2may be written as uvw, where 1 ≤ |v| ≤ n and uv iw is
in L for all i. Let i = 2, n2 < |uv2w| < n2 + n < (n+1)2.
That is, the length of uv2w lies properly between n2 and (n +1)2, and is thus not a
perfect square.
Thus uv2w is not in L, a contradiction.
We conclude that L is not regular.
3.7 Closure Properties of Regular Sets:
The regular sets are closed under union, concatenation, and Kleene closure.
The class of regular sets is closed under complementation. That is, if L is a
regular set and L ⊆ ∑*, then ∑* - L is a regular set.
The regular sets are closed under intersection.
The class of regular sets is closed under substitution.
The class of regular sets is closed under homomorphism and inverse
homomorphism.
The class of regular sets is closed under quotient with arbitrary sets.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 10
3.8 Applications of Regular Languages:-
Efficient string searching .
Pattern matching with regular expressions (example: Unix grep utility)
Lexical analysis (a.k.a. scanning, tokenizing) in a compiler.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 11
UNIT - III
Assignment-Cum-Tutorial Questions
SECTION-A
Objective Questions
1. The languages accepted by finite automata are easily described by simple
expressions called___________________.
2. A language is a ____________________ if it is the set accepted by some finite
automaton.
3. What is the solution for equation R=Q+RP (if P and Q are RE and P does not
contain ε)? [ ]
(a) R=QP* (b) R=QP (c) R=PQ* (d) R=P*Q*
4. ∅ + R =_____________.
5. ∅* =__________________.
6. ε*=_______________.
7. ε + r r*=r* [ True / False]
8. Pumping lemma is generally used for proving [ ]
A) a given grammar is regular
B) a given grammar is not regular
C) whether two given regular expressions are equivalent
D) none of the above
9. Regular sets are closed under [ ]
A) Union B) concatenation
C) Kleene closure D) All of the above
10. a + b denotes the set_____________________. [ ]
A) {a, b} B) {ab} C) { a } D) {b}
11. The set of all strings of {0, 1} having exactly two 0’s is [ ]
* *
A) 1 01 01 * *
B) {(0+1) } C) {11+0}* D) {00+11}*
12. The regular expression to represent all strings with length atmost 2 over
{a,b} is_______. [ ]
A) ε B) ε+(a+b)+(a+b).(a+b) C) (a+b) D) (a+b).(a+b)
13. Which one of the following languages over the alphabet {0 ,1} is described by
the regular expression: (0+1)*0(0+1)*0(0+1)*? [ ]
A) The set of all strings containing the substring 00.
B) The set of all strings containing atmost two 0’s.
C) The set of all strings containing atleast two 0’s.
D) The set of all strings that begin and end with either 0 or 1.
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 12
14. Consider the languages L1 = and L2 = {0}. Which one of the following
represents L1 L2 * + L1* [ ]
A) { } B) Ø C) 0* D) { ,0}
15. What is the regular expression for the given DFA? [ ]
.
(a) (0+1)* (b) 0(0+1)* (c) 0 (d) (0+1)*0
16. Which of the following languages are not regular? [ ]
(a) L= an | n>=1 (b) L=anbm | n,m>=1 (c) anbn | n>=1 (d) a2n | n>=0
17. What is the regular expression for the given DFA? [ ]
(a) 0*1+ (b) 0*1* (c) 1*0* (d) 1*0+
SECTION-B
Descriptive questions
1. Define regular set and regular expression.
2. State Arden’s Theorem.
3. List the closure properties of Regular Languages.
4. Explain in detail the procedure for converting Finite automata into regular
expression with an example.
5. Write regular expressions for each of the following languages over ∑={0, 1}
(i) The set of all strings representing with at least two consecutive 0’s
(ii) The set of all strings ending in 011
(iii) The set of all strings starting with 101
(iv) The set of all strings containing 011 as substring
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 13
(v) The set of all strings that start and end with same symbol
6. Design a ε-NFA for the regular expression a*bc | ab* |c*.
7. Construct NFA with ε-moves for the regular expression 10+(0+11)0*1
8. Construct Finite automata for the regular expression 1(01+10)*00.
9. Construct finite automation to accept the regular expression (0+1)* (00+11)(0+
1)*.
10. Define Pumping Lemma. Apply pumping lemma and show that the language L
= { anb2n | n > 0 } is not regular.
11. What are the applications of pumping lemma? Show that the language
L={anbncn | n>=1 } is not regular.
12. Using pumping lemma, show the following language is not regular:
L= {w {0,1}*|the number of 0’s in w is a perfect square}
13. Applying Arden’s Theorem construct regular expression for the DFA given
below
14. Construct regular expression for the following DFA.
15. What is the regular expression for the given DFA?
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 14
SECTION-C
Gate Questions:
1. The number of states in the minimum sized DFA that accepts the language
defined by the regular expression (0+1)*(0+1)(0+1)* is__________.
[GATE 2016 Set-B]
2. Which of the regular expressions given below represent the following DFA?
[ ] [GATE 2014 Set-1]
I) 0*1(1+00*1)*
II) 0*1*1+11*0*1
III) (0+1)*1
A) I and II only B) I and III only
C) II and III only D) I, II and III only
3. Consider the languages L1 = Ø and L2 = {a}. Which one of the following
represents L1 L2 * U L1* [ ] [GATE 2013]
A) { } B) Ø C) a* D) { ,a}
4. Let L = {w ∈ (0 + 1)*|w has even number of 1s}, i.e. L is the set of all bit strings
with even number of 1s. Which one of the regular expressions below represents
L? [ ] [GATE 2010]
A) (0*10*1)* B) 0*(10*10*) * C) 0*(10*1*)*0* D) 0*1(10*1)*10*
5. The language accepted by this automaton is given by the regular expression
[ ] [GATE 2007]
A) b*ab*ab*ab* B) (a+b)* C) b*a(a+b)* D) b*ab*ab*
6. Consider the language L=(111+11111)*.The minimum number of states in any
DFA accepting this language is: [ ] [GATE 2006]
II.B.Tech-II-Semester 2019-20 CSE
Formal Languages And Automata Theory 15
A)3 B) 5 C) 8 D) 9
7. The number of states in the minimal deterministic finite automaton
corresponding to the regular expression (0 + 1)*(10) is [GATE 2010]
A) 2 B) 3 C) 4 D) 5
II.B.Tech-II-Semester 2019-20 CSE