109 - Chapter 03 - Regular Expression and Language
109 - Chapter 03 - Regular Expression and Language
Automata Theory
1
Chapter – 03
Regular Expression and Regular Language
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.,
Example 3.1: write the regular expression for the language
accepting all combination of a’s over the input set ∑ = {a}
Regular Expression R = a* (kleen closure)
All combination of a’s means: ‘a’ may be zero times, one times,
two times and three time etc., L = {ε , a, aa, aaa, aaaa, ….}
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.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*
7
Cont.,
8
Relationship between Regular Expression and Finite Automata
9
RE to FA
Procedures of Finite automata for the Regular Expressions:
R=ε
R=φ
R=a
R = ab (concatenation of a & b)
10
Cont.,
R = a*
11
Cont.,
Draw the NFA for r4 = a*
12
Cont.,
Finally draw the automata for the R = r1 +r2 i.e R = b + ba*
13
Cont.,
NFA for r2 = 2*
NFA for r3 = 1
14
Try Yourself
1. Construct NFA for the regular expression (0+1)*
15
FA to RE
Arden’s Method:
16
Cont.,
Example 3.10: construct RE from the given DFA
17
Cont.,
18
Cont.,
Example 3.11: Construct the RE for the given DFA
19
Cont.,
20
Rules of RE
21
Try Yourself
1. Construct the RE for the given DFA
22
Application of RE
1. It can be modeled by FSA
2. Effective representation of language
3. Text editor
4. Lexical Analyzer
23
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 ε
24
Types
25
Cont.,
26
Construction of FA to Regular Grammar
Let M = ({, , …. }, ∑, δ, , F) be a DFA. The equivalent
grammar is constructed from this DFA such that the
transition of DFA is corresponds with production of the
grammar.
The derivation of the string can be terminated by the
terminal given by the production rule.
In FA final state is encountered by terminating the
transition.
Using the above two character, we are going to construct
the FA to Regular grammar.
Let G = ({, , …. }, ∑, δ P, ) P is the set of production rule can
be defined by the following rules:
1. a is a production rule if δ(, a) = where F
2. a and a are the production rules if δ(, a) = where
F 27
Cont.,
Example 3.12: construct the regular grammar for the given
FA
28
Cont.,
Transition from
Transition to - a
Transition to - a
- a (final state)
Transition from
Transition to - a
- a (final state)
Transition to - b
- b (final state)
finally P = { a , a , a, a , a
b , b}
29
Cont.,
Example 3.13: construct the regular grammar for the given FA
Consider state :
b
a
b
a
31
Try Yourself
1. Construct the regular grammar for the given DFA
32
Construction of Regular Grammar to FA
Let G = (N, T, P, ) be a regular grammar. Can construct the
DFA M = ({, , …., }, ∑, δ, , {}) here
i. Starting state corresponds to
ii. δ is defined as
i. Production a : transition from to with label of a
ii. Production a : transition from to with label of a
Example 3.14 construct a FA recognizing L(G) where G is
grammar
S aS | bA | b
A aA |bS | a
Finite Automata M = ({, , }, {a, b}, δ, , {})
33
Where and corresponds to S and A Cont.,
is final state
S aS - transition from to with label a
S bA - transition from to with label b
S b - transition from to with label b
A aA - transition from to with label a
A bS - transition from to with label b
A a - transition from to with label a
34
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)
35
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.
z = uviw i > 0 36
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.
37
Cont.,
Example 3.16: Show that L = { 0n1n+1 | n>0 } is not regular
Assume L is regular
|z|= |uvw|
= n + n + 1 = 2n+1
Length of string z = 2n+1, that means length always odd.
By pumping lemma = |uv.vw|
If u add the 2n+1 to the length of the string it will be 4n+2, that means
it will be even so the language is not regular.
Example 3.17 Find out the listed below languages are regular or non
regular and give reasons.
L = { | n >=1}
L = { | n, m>=1}
L = { | n<}
L = { | n>1}
L = {w | |w|=2 }, ∑ = {a, b}
L = {ww | w belongs to (a,b)* } 38
Cont.,
L = { | n >=1}
L= {a, aa, aaa, aaaa,……}, R(L) =
Here this language is infinite language because n is unbounded . But
we can design DFA and regular expression so the language L will be
the regular language.
L = { | n, m>=1}
R(L) = aa*bb*
Here this language is infinite language because n is unbounded . But
we can design DFA and regular expression so the language L will be
the regular language.
L = { | n<}
Here this language is finite language because n is bounded. So this
language is regular language because every finite language will be a
regular language
39
Cont.,
L = { | n>1}
Here this language is infinite language because n is unbounded. Also
not possible to design DFA for this language. Additionally counting
and checking the inputs is not possible in FA because additional
memory is not available. So this language is non regular language
L = {w | |w|=2 }, ∑ = {a, b}
Here the samples are based on the condition |w|=2
w = aa, ab, ba, bb
= aa, ba, ab,bb
So possible w are
aaaa, abba, baab, bbbb
So L = { aaaa, abba, baab, bbbb } is finite language so we can finalize
this language as regular language because every finite language will
be a regular language
40
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
41