Lecture05 RegularExpression&FA
Lecture05 RegularExpression&FA
Text Book:
“Theory of Computation an Introduction" by James L. Hein, Jones & Bartlett
Publishers 1996; ISBN: 0-86720-497-4.
1
Regular Expressions
A regular language is a language that can be defined by a regular
expression. A regular expression is often described by means of an
algebraic expression called a regular expression notation .
2- If r1 and r2 are regular expressions, so are r1 + r2, r1 . r2, r1*, and ( r1).
2
Examples
3
Languages of Regular Expressions
◼ Example
4
Definition
L( ) =
L( ) =
L(a ) = a
5
Recursive Definition
, , a
Primitive regular expressions:
Given regular expressions r1 and r2
r1 + r2
r1 r2
Are regular expressions
r1 *
(r1 )
6
Regular Expressions
◼ Regular expressions
❑ describe regular languages
◼ Example:
describes the language (a + b c) *
L((r1 )) = L(r1 )
8
Example
◼ Regular expression: (a + b ) a *
9
Example
L(r ) = {a b
2n 2m
b : n, m 0}
10
Properties of Regular Languages
Union: L1 L2
Languages
Generated by
Regular Expressions
= Regular
Languages
12
Equivalent Regular Expressions
◼ Definition:
are equivalent if
L(r1) = L(r2 )
13
Example
r1 = (1 + 01) * (0 + )
r2 = (1* 011*) * (0 + ) + 1* (0 + )
r1 and r2
L(r1) = L(r2 ) = L are equivalent
regular expr.
14
Example
◼ Regular expression r = (0 + 1) * 00 (0 + 1) *
15
Regular Expression and FA
16
Regular Expression and FA
regular
L( M 2 ) = {} = L( )
languages
a
L( M 3 ) = {a} = L(a)
17
Union
◼ NFA for L1 L2 M1
M2
18
Incorrect Union
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b
a
a
AB?
b
No: this NFA accepts aab
b
19
Correct Union
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b
a
a
AB
b
b
20
Example
◼
NFA for L1 L2 = {a b} {ba}
n
L1 = {a nb}
a
b
L2 = {ba}
b a
21
Concatenation
M1 M2
22
Example
a
M1
L1 = {a b}
n
b
M2
L2 = ba b a
23
Example
L1 = {a b}
n
a L2 = {ba}
b b a
24
Incorrect Concatenation
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b
a b
a b
{xy | x A and y B} ?
No: this NFA accepts abbaab
25
Correct Concatenation
a
A = {an | n is odd}
a
b
B = {bm | m is odd}
b
a b
a b
{xy | x A and y B}
26
Star Operation
◼ NFA for L1 *
L1 *
M1
27
Example
L1 = {a b} n
a
b
28
Example
Words that begin and end with the same letter. a(a+b)*a + b(a+b)*b
a,b
a a
b b
a,b a,b a,b
aba
Words that contain aba.
(a+b)*aba(a+b)*
29
Example
a b
a,b
a,b a,b
aba
a b a,b
(a+b)*aba(a+b)* + a(a+b)*a + b(a+b)*b
30
Example
b b
a,b
(a(a+b)*a + b(a+b)*b)((a+b)*aba(a+b)*)
31
Example
a
a
– +
–
b b a,b
a,b
a,b aba
((a+b)*aba(a+b)*)* +
32
Regular Expressions to Finite Automata
NFA
Regular
expressions DFA
33
Regular Expression and FA
34
◼ Describe the language of the following regular
expressions, and construct the NFA for E
◼ E = b* + ab + c*b
◼ E = (a*c). (ad)
35
Answer for the language only:
36
Main Purpose
4. From Regular Expression To DFAs
◼ Study an algorithm:
❑ Translating a regular expression into a DFA via
NFA.
Regular Program
NFA DFA
Expression
37
The Idea of Thompson’s Construction
4.1 From a Regular Expression to an NFA
◼ Use ε-transitions
❑ to “glue together” the machine of each piece of a regular
expression
❑ to form a machine that corresponds to the whole expression
◼ Basic regular expression
❑ The NFAs for basic regular expression of the form a, ε,or φ
a
4.1 From a Regular Expression to an NFA
The Idea of Thompson’s Construction
◼ Concatenation: to construct an NFA equal to rs
❑ To connect the accepting state of the machine of r to the
start state of the machine of s by an ε-transition.
❑ The start state of the machine of r as its start state and the
accepting state of the machine of s as its accepting state.
❑ This machine accepts L(rs) = L(r)L(s) and so corresponds to
the regular expression rs.
r s
… …
4.1 From a Regular Expression to an NFA
The Idea of Thompson’s Construction
s
…
4.1 From a Regular Expression to an NFA
The Idea of Thompson’s Construction
◼ Repetition: Given a machine that corresponds to r,
Construct a machine that corresponds to r*
❑ To add two new states, a start state and an accepting state.
❑ The repetition is afforded by the newε-transition from the
accepting state of the machine of r to its start state.
❑ To draw an ε-transition from the new start state to the new
accepting state.
❑ This construction is not unique, simplifications are possible in the
many cases.
r
…
4.1 From a Regular Expression to an NFA
Examples of NFAs Construction
Example 1.12: Translate regular expression ab|a into NFA
a
a b
a b
a
4.1 From a Regular Expression to an NFA
Examples of NFAs Construction
Example 1.13: Translate regular expression letter(letter|digit)* into NFA
letter
letter
digit
letter
letter
letter
letter
letter
letter
44