Arba Minch University
Arba Minch Institute of Technology
Department of Computer Science & IT
Formal Language and
Automata Theory
Prince M. Thomas
Target Group - G3CS – A/B/C Academic Year – 2019/20
1
Chapter – 03
Regular Expression and Regular Language
Regular expressions - RE to FA - FA to RE - Applications of
RE - Regular grammars and FA - FA for regular grammar -
Connection Between Regular Expressions and Regular
Languages - pumping lemma and non-regular language
grammars
2
Regular Expression
• Describe the regular language by the notation of regular expressions
• Languages accepted by finite automata are easily described by simple
expression is called Regular expression
• Most effective way to represent any language
Formal Definition of Regular Expression:
Construct the expressions from primitive constituents by repeatedly
applying certain recursive rules. This is similar to the way the arithmetic
expressions
Definition1:
3
Regular Expression
Language Associated with regular expression:
Definition2:
The language L is denoted by the any regular expression r
defined by the following rule.
• φ is regular expression for empty set
• ε is regular expression for null string
• For each ‘a’ in ∑, ‘a’ is a regular expression
• If r and s are regular expression denoting the language L1 and
L2 then
– r + s is equivalent to L1 U L2 i.e union
– rs is equivalent to L1L2 i.e concatenation
– r* is equivalent to L1* i.e closure
– r+ is equivalent to L1+ i.e positive closure
4
Cont.,
•
5
Cont .,
Example 3.3: Design regular expression for all the strings containing
any number of a’s and b’s
R = (a+b)*
L = {ε, a, b, aa, ab, ba, bb, aba,…. Any combination of a and b}
(a+b)* means any combination of a and b even null string
Example 3.4: Construct the RE for language accepting all the stings
which are ending with 00 over the input set ∑ = {0, 1}
R = (any combination of 0’s and 1’s) 00
R = (0 + 1)* 00
L = {00, 000, 100, 1000,0100 …. String ending with 00}
Example 3.5 If L = {stating and ending with ‘a’ and having any
combination of b’s in between } what is R?
R = a b* a
6
Cont.,
Example 3.6: Construct the RE for language accepting all the
stings which are having any number of a’s is followed by any
number b’s is followed by any number of c’s over the input set
∑ = {a, b, c}
R = (any number of a) (any number of b) (any number of c)
R = a* b* c*
Example 3.7: write a RE to denote the language L over ∑ ={a, b}
such that all the strings do not contain the substring ab
L = {ε, a, b, bb, ba, baa, …..}
R = b* a*
7
Cont.,
8
Try yourself
1. Design regular expression for all the strings containing any
number of a’s and b’s, except the null string
2. Construct the RE for language accepting all the stings which
are starting with 1 and ending with 00 over the set ∑ = {0, 1}
3. Construct the RE for language accepting all the stings which
are having atleast one a’s is followed by atleast one b’s is
followed by atleast one c’s over the input set ∑ = {a, b, c}
9
Relationship between Regular Expression and Finite Automata
10
RE to FA
Procedures of Finite automata for the Regular Expressions:
R=ε
R=φ
R=a
R = a+b (union of a and b )
R = ab (concatenation of a & b)
11
Cont.,
R = a*
Example 3.8 Construct the FA for the regular expression b + ba*
R = b + ba* = r1 + r2
r1 = b, r2 = ba*
Draw the NFA for r1 = b
r2 = r3r4 (concatenation of r3 and r4)
Draw the NFA for r3 = b
12
Cont.,
Draw the NFA for r4 = a*
Draw the NFA for r2 = r3r4
13
Cont.,
Finally draw the automata for the R = r1 +r2 i.e R = b + ba*
Example 3.9 Construct NFA for the regular expression (01+2*)1
R = (01+2*)1
R = (r1 +r2)r3
r1 = 01; r2 = 2*; r3 = 1
NFA for r1 = 01
14
Cont.,
NFA for r2 = 2*
NFA for r3 = 1
Final NFA will be R = (r1 + r2) r3
15
Try Yourself
1. Construct NFA for the regular expression (0+1)*
2. Construct NFA for the regular expression (01+10)+
16
FA to RE
Arden’s Method:
17
Cont.,
Example 3.10: construct RE from the given DFA
18
Cont.,
19
Cont.,
Example 3.11: Construct the RE for the given DFA
20
Cont.,
21
Rules of RE
22
Try Yourself
1. Construct the RE for the given DFA
2. Construct the RE for the given DFA
23
Application of RE
1. It can be modeled by FSA
2. Effective representation of language
3. Text editor
4. Lexical Analyzer
24
Regular Grammar
Alternative way of specifying languages
Definition:
G = (N, T, P, S)
N – set of non terminals
T – set of terminals
P – production rules
S – start symbol
For example:
G = (N, T, P, S)
N = {S, A} S Start symbol P = { S 0S
T = {0, 1} S 1A
A ε
25
Types
1. Right – linear grammar
If the non terminal symbol appears as rightmost symbol
in each production rules of the regular grammar.
A aB --------- rule 1
A bA --------- rule 2
A ε --------- rule 3
In rule1 non terminal B is appeared in rightmost
In rule2 non terminal A is appeared in rightmost
26
Cont.,
2. Left – linear grammar
If the non terminal symbol appears as leftmost symbol in
each production rules of the regular grammar.
A Ba --------- rule 1
A Ab --------- rule 2
A ε --------- rule 3
In rule1 non terminal B is appeared in leftmost
In rule2 non terminal A is appeared in leftmost
The language is called regular if it is accepted by the either
left or right linear grammar
27
Construction of FA to Regular Grammar
•
28
Cont.,
•
29
Cont.,
•
30
Cont.,
•
31
Cont.,
•
32
Try Yourself
1. Construct the regular grammar for the given DFA
33
Construction of Regular Grammar to FA
•
34
Cont.,
•
35
Non Regular language
Regular language are the language can be represented by the
Finite Automata and Regular expression but some languages
can not represented by regular expression and finite
automata, that language is called as non regular language.
• Non regular language can’t represent by finite automata
• Theorem pumping lemma which is used to find out
whether the given string is accepted by regular expression
or not. In short this lemma tells whether the language is
regular or not.
• Every finite language will be regular
• Every infinite language will not be regular (To accept the
infinite language Finite automata has certain loop, on the
loop there must be pattern otherwise there is no way to
design finite automata)
36
Pumping Lemma
Theorem:
Let L be the 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 that |uv| ≤ n, |v| = 1 and for
all i ≥ 0 uviw in L. furthermore n is should not grater than the number of
states of DFA.
Suppose n states in DFA, Consider the inputs a1, a2, a3, …… am
m ≥ n are accepted by the DFA.
a1,….. ai, aj, aj+1, …… am will be accepted by DFA
z = uviw i > 0
37
Cont.,
Example 3.15 Justify you answer for the following language
L = { 02n | n ≥ 1 }.
From the above language length of the string is always even
because of power 2n.
n = 1 ;L = 00
n = 2 ;L = 00 00 and so on
Let L = uvw
L = 02n
|z| = 2n = uvw
If add 2n to this string length
|z| = 4n = uv.vw
= even length of string
So after pumping 2n also we will get the even string from that
the given language is regular language.
38
Cont.,
•
39
Cont.,
•
40
Cont.,
•
41
Cont.,
L = {ww | w belongs to (a,b)* }
Here this language is infinite language because (a,b)* , it is not possible
to save the infinite input in the finite input tape. Also we cant design
DFA for this language so this language is Non regular language
42