Chapter 03 - Regular Expression and Language
Chapter 03 - Regular Expression and Language
Prince M. Thomas
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.,
•
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*
7
Cont.,
8
Try yourself
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 = ab (concatenation of a & b)
11
Cont.,
R = a*
12
Cont.,
Draw the NFA for r4 = a*
13
Cont.,
Finally draw the automata for the R = r1 +r2 i.e R = b + ba*
14
Cont.,
NFA for r2 = 2*
NFA for r3 = 1
15
Try Yourself
1. Construct NFA for the regular expression (0+1)*
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
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
26
Cont.,
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.
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