Regular Expressions
Regular Expressions
Regular Languages
Foundations of Computer
Science Theory
a
(a b)*
abba
Note that a is shorthand for {a}, is shorthand for {},
and (a b)* is shorthand for {a, b}*
R* = R RR RRR
Example: (0 10)* = {, 0, 10, 00, 010, 100,
1010, }
Concatenation is associative
( ) = ( )
Precedence of Operators
Just as with arithmetic operations,
parentheses may be used wherever needed to
influence the grouping of operators
Order of precedence is * (highest), then
concatenation, then (lowest)
R = R = R
R = R =
is the annihilator for concatenation
( 1)( 0) = {, 0, 1, 10}
1* =
Note: concatenating the empty set to any set yields the empty
set
Legal passwords:
((a-z) (A-Z)) ((a-z) (A-Z) (0-9) _) {8,16}
IP addresses:
((0-9){1,3} (\. (0-9){1,3}) {3})
Kleenes Theorem
Kleenes Theorem: if a language is regular then
it is recognized by a finite automaton, and
conversely, if a language is recognized by a
finite automaton then it is regular
This proof of this theorem has two directions:
Given a regular language, we can construct a finite
automaton that recognizes it
Given a finite automaton, we can find a regular
language that recognizes it
We will use a constructive proof to do this
-NFA for R1
-NFA for R2
-NFA for R1
-NFA for R1
-NFA for R2
-NFA for R
-NFA for R*
NFA for a:
NFA for b:
(a bb)
(ba* )
(a bb)* (ba* )
NFA to RE Construction
Goal: Given an NFA called M, find an
equivalent machine M such that:
M has only two states: a start state and an
accepting state, along with a single transition from
the start state to the accepting state
NFA to RE Construction
Procedure: Given an arbitrary NFA M, M will
be built by starting with M and then removing,
one at a time, all of the states that lie inbetween the start state and an accepting state
As each such state is removed, the remaining
transitions will be modified so that the set of
strings that can drive M from its start state to
some accepting state remains unchanged
NFA to RE Construction
For example, let M be:
After
After
After removal
of state 3
After
After removal
of state 2
Before
After
After removal
of state 1
Closure Properties
Recall that a collection of objects is closed
under some operation if applying that
operation to members of the collection
returns an object still in the collection
The class of regular languages is closed under
the union operation
If L1 and L2 are regular languages, then L1 L2 is
also a regular language
Q = Q 1 Q2
is defined as follows: for all (r1, r2) Q and a ,
((r1, r2), a) = (1(r1, a), 2(r2, a))
q0 = {q1, q2}
F = {r1, r2} where (r1 F1 or r2 F2)
0
A
[A,C]
0, 1
[A,D]
1
0
1
C
[B,C]
0
[B,D]
0
A
[A,C]
0, 1
[A,D]
1
0
1
C
[B,C]
0
[B,D]