Regular Expressions

Download Report

Transcript Regular Expressions

Regular Expressions
Costas Busch - LSU
1
Regular Expressions
Regular expressions
describe regular languages
Example:
(a  b  c) *
describes the language
a , bc *   , a , bc , aa , abc , bca ,... 
Costas Busch - LSU
2
Recursive Definition
Primitive regular expressions:  ,  , 
Given regular expressions r1 and r2
r1  r2
r1  r2
r1 *
Are regular expressions
 r1 
Costas Busch - LSU
3
Examples
A regular expression:
 a  b  c  * ( c   )
Not a regular expression:
Costas Busch - LSU
a  b  
4
Languages of Regular Expressions
L  r  : language of regular expression r
Example
L  ( a  b  c ) *    , a , bc , aa , abc , bca ,... 
Costas Busch - LSU
5
Definition
For primitive regular expressions:
L    
L      
L  a   a 
Costas Busch - LSU
6
Definition (continued)
For regular expressions r1 and r2
L  r1  r2   L  r1   L  r2 
L  r1  r2   L  r1  L  r2 
L  r1 *    L  r1   *
L   r1    L  r1 
Costas Busch - LSU
7
Example
Regular expression:  a  b   a *
L  a  b   a *   L  a  b  L  a * 
 L a  b  L a *
  L  a   L b   L  a  *
 a   b  a  *
 a , b   , a , aa , aaa ,... 
 a , aa , aaa ,..., b , ba , baa ,... 
Costas Busch - LSU
8
Example
Regular expression
r   a  b  *  a  bb 
L  r   a , bb , aa , abb , ba , bbb ,... 
Costas Busch - LSU
9
Example
r   aa  *  bb  * b
Regular expression
L r   {a
2n 2m
b
b:
Costas Busch - LSU
n , m  0}
10
Example
Regular expression
r  ( 0  1) * 00 ( 0  1) *
L (r ) = { all strings containing substring 00 }
Costas Busch - LSU
11
Example
Regular expression
r  (1  01 ) * ( 0   )
L (r ) = { all strings without substring 00 }
Costas Busch - LSU
12
Equivalent Regular Expressions
Definition:
Regular expressions r1 and r2
are equivalent if
L ( r1 )  L ( r2 )
Costas Busch - LSU
13
Example
L = { all strings without substring 00 }
r1  (1  01 ) * ( 0   )
r2  (1 * 011 *) * ( 0   )  1 * ( 0   )
r1 and r2
are equivalent
regular expressions
L ( r1 )  L ( r2 )  L
Costas Busch - LSU
14
Regular Expressions
and
Regular Languages
Costas Busch - LSU
15
Theorem
Languages
Generated by
Regular Expressions

Costas Busch - LSU
Regular
Languages
16
Proof:
Languages
Generated by
Regular Expressions

Regular
Languages
Languages
Generated by
Regular Expressions

Regular
Languages
Costas Busch - LSU
17
Proof - Part 1
Languages
Generated by
Regular Expressions

Regular
Languages
For any regular expression r
the language L (r ) is regular
Proof by induction on the size of r
Costas Busch - LSU
18
Induction Basis
Primitive Regular Expressions:  ,  , 
Corresponding
NFAs
L ( M 1 )    L ( )
L ( M 2 )  { }  L (  )
a
regular
languages
L ( M 3 )  {a}  L ( a )
Costas Busch - LSU
19
Inductive Hypothesis
Suppose
that for regular expressions r1 and r2 ,
L ( r1 ) and L ( r2 ) are regular languages
Costas Busch - LSU
20
Inductive Step
We will prove:
L  r1  r2 
L  r1  r2 
L  r1 * 
Are regular
Languages
L   r1  
Costas Busch - LSU
21
By definition of regular expressions:
L  r1  r2   L  r1   L  r2 
L  r1  r2   L  r1  L  r2 
L  r1 *    L  r1   *
L   r1    L  r1 
Costas Busch - LSU
22
By inductive hypothesis we know:
L ( r1 ) and L ( r2 ) are regular languages
We also know:
Regular languages are closed under:
Union
L  r1   L  r2 
Concatenation L  r1  L  r2 
Star
 L  r1   *
Costas Busch - LSU
23
Therefore:
L  r1  r2   L  r1   L  r2 
L  r1  r2   L  r1  L  r2 
Are regular
languages
L  r1 *    L  r1   *
L (( r1 ))  L ( r1 )
is trivially a regular language
(by induction hypothesis)
Costas Busch - LSU
End of Proof-Part 1
24
Using the regular closure of operations,
we can construct recursively the NFA M
that accepts L ( M )  L ( r )
Example: r  r1  r2
L ( M1 )  L ( r1 )
L (M )  L (r )

L ( M2 )  L ( r2 )

Costas Busch - LSU
25
Proof - Part 2
Languages
Generated by
Regular Expressions

Regular
Languages
For any regular language L there is
a regular expression r with L ( r )  L
We will convert an NFA that accepts L
to a regular expression
Costas Busch - LSU
26
Since L is regular, there is a
NFA M that accepts it
L(M )  L
Take it with a single accept state
Costas Busch - LSU
27
From M construct the equivalent
Generalized Transition Graph
in which transition labels are regular expressions
Example:
Corresponding
Generalized transition graph
M
a
c
a
c
ab
a,b
Costas Busch - LSU
28
b
b
Another Example:
a
q1
q0
a,b
q2
b
Transition labels
are regular
expressions
a
q0
b
b
q1 a  b
q2
b
Costas Busch - LSU
29
Reducing the states:
a
q0
b
b
q1 a  b
q2
b
Transition labels
are regular
expressions
bb * a
q0
b
bb * ( a  b )
Costas Busch - LSU
q2
30
Resulting Regular Expression:
bb * a
q0
b
bb * ( a  b )
q2
r  ( bb * a ) * bb * ( a  b )  b *
L (r )  L (M )  L
Costas Busch - LSU
31
In General
Removing a state:
e
c
d
qi
qj
q
a
b
2-neighbors
ae * d
ce * b
ce * d
qj
qi
ae * b
Costas Busch - LSU
32
e
c
d
qi
q
q
a
g
f
j
b
qk
This can be generalized
to arbitrary number
of neighbors to q
3-neighbors
ae * d
ce * b
ce * d
qj
qi
ae * b
ge * b
ge * d
ae * f
ce * f
qk
ge * f
Costas Busch - LSU
33
By repeating the process until
two states are left, the resulting graph is
Initial graph
Resulting graph
r1
r4
r3
q0
r2
qf
The resulting regular expression:
r  r1 * r2 ( r4  r3 r1 * r2 ) *
L (r )  L (M )  L
Costas Busch - LSU
End of Proof-Part 2
34
Standard Representations
of Regular Languages
Regular Languages
DFAs
NFAs
Costas Busch - LSU
Regular
Expressions
35
When we say:
We mean:
We are given
a Regular Language L
Language L is in a standard
representation
(DFA, NFA, or Regular Expression)
Costas Busch - LSU
36