lecture 5: finite automata

Post on 31-Dec-2015

54 Views

Category:

Documents

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

Lecture 5: Finite Automata. 虞台文. 大同大學資工所 智慧型多媒體研究室. Content. Alphabets and Languages Deterministic Finite Automata Nondeterministic Finite Automata Equivalence of DFA &NFA Properties of Languages Accepted by FA’s Finite Automata & Regular Expressions Languages Not Accepted by FA’s. - PowerPoint PPT Presentation

TRANSCRIPT

Lecture 5: Finite Automata

虞台文大同大學資工所智慧型多媒體研究室

Content Alphabets and Languages Deterministic Finite Automata Nondeterministic Finite Automata Equivalence of DFA &NFA Properties of Languages Accepted by FA’s Finite Automata & Regular Expressions Languages Not Accepted by FA’s

Lecture 5: Finite Automata

Alphabetsand

Languages

大同大學資工所智慧型多媒體研究室

Alphabets

Alphabet a finite set of symbols Examples:

1 0,1 binary alphabet

3 , , ,a b z Roman alphabet

2 0,1, ,9 decimal alphabet

4 , , , Greek alphabet

5 , , , , , , , , a b c d x y z Z my alphabet

Strings String over an alphabet () a finite

sequence of symbols from * the set of all strings over Examples:

1 0,1

3 , , ,a b z

2 0,1, ,9

4 , , ,

*1001100, 1000011110

*200799, 123789000

*3 ,taxi ixat

*4 ,

*

More on Strings

String length # symbols in a string, e.g.,

A string, w, can be considered as a function , e.g.,

Some string operations:– Concatenation

– Reversal

– Duplication

00110| 0| 6 | | 0 :{1, ,| |}w w

" "w hello(1) ' ', (2) ' ', (3) (4) ' ', (5) ' '.w h w e w w l w o

" ", " "x Hello y World " "x y xy Hello World

" "w Hello " "Rw olleH

" "w Hello 3 " "w HelloHelloHello

More on Strings

Substring Suffix Prefix

" "w HelloWorld

" "u World

" "t Hello

" "v oWo

a prefix of w and a substring of w.

a suffix of w and a substring of w.

a substring of w.

Countability of *

* is countably infinite for any finite alphabet .

Example: {0,1}

0,1

00,01,10,11

000,001,010,011,100,101,110,111

* can be enumerated by:

Languages

Language L over any subset of * i.e., L *

How many possible languages over ?

How to specify a language?

*

2

* : L w w P has property

uncountably infinite

Operations of Languages

Complement

Union

Intersection

*{ : , }L w w w L

1 2L L L

1 2: or L w w L w L

1 2L L L

1 2: and L w w L w L

Operations of Languages

Complement, Union and Intersection Concatenation

Closure or Kleene Star

Positive Closure

1 2 1 2L L L L L

1 2: ,L w xy x L y L

*1 2 1 2: 0, , ,k kL w w w w k w w w L

*1 2 1 2: 1, , ,k kL LL w w w w k w w w L

Example: Kleene Star

01,1,100L

110001110011w

100011000010v

*L ?

*L ?

1909 – 1994

Example: Kleene Star

01,1,100L

110001110011w

100011000010v

*L ?

*L ?

1909 – 1994

Example: Kleene Star

01,1,100L

110001110011w *L ? *

1 2 1 2: 1, , ,k kL LL w w w w k w w w L

L ?

1909 – 1994

Language Recognition Device

A machine or algorithm to answer the question

?w L

Lecture 5: Finite Automata

Deterministic Finite Automata

大同大學資工所智慧型多媒體研究室

Finite State Machine

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

Definition Deterministic Finite Automaton

A deterministic finite automaton is a 5-tuple

(K, , , s, F)K : a finite set of states : a finite set of symbols

: the transition function K Ks K : the initial state

F K : the set of final states

Reading Head

I nputtape

q0q1

q3

q2

Finitecontrol

q5

q4

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aaaa bb aa aa bb bb aa bb aa aa

Example

K={q0, q1, q2, q3} ={0, 1}

q0

q1

q2

q3

0 1q2 q1

q3 q0

q0 q3

q1 q2

symbol

state

s=q0 F= {q0}q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

(K, , , s, F)

Operations of a DFA

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

Operations of a DFA

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

Strings are fed into the device by means of an input tape.

Strings are fed into the device by means of an input tape.

Main part of the machine• Initially,

• putting reading head at the leftmost• in state s = q0

• Sense input symbol• Change state based on • Move head right one cell• Give answer when head reaches end

Main part of the machine• Initially,

• putting reading head at the leftmost• in state s = q0

• Sense input symbol• Change state based on • Move head right one cell• Give answer when head reaches end

DFA As a Language Acceptor

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

The input string is accepted by the machine if the reading head ends at a final state.

Otherwise, it is rejected.

Accepted

Rejected JFLAP

DFA As a Language Acceptor

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

The input string is accepted by the machine if the reading head ends at a final state.

Otherwise, it is rejected.

What language the

machine accepts?What language the

machine accepts?

DFA As a Language Acceptor

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

The input string is accepted by the machine if the reading head ends at a final state.

Otherwise, it is rejected.

What language the

machine accepts?What language the

machine accepts?

q3

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

K *

*

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

u w

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

K *

*

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

u wDoes not effect the result.

Does not effect the result.

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

( , )q aw

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

( , )q aw

Memory Configuration of a DFA

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

u w

2( , )q abbabaa

Yields in One Step M M

( , )q aw M M ( , )q w

if ( , )q a q

M M

Yields

( , )q w ( , )q w

if M M

*

M M*

M M*

( , )q w M M M M ( , )q w

in some (including zero) steps

String Acceptance by a DFA

M = (K, , , s, F)

w* is said to be accepted by M iff

( , )s w ( , )q M M* M M

*

such that q F

Language Defined by a DFA

M = (K, , , s, F)

*( ) : , ( , ) ( , ),L M w w s w q q F M M*

M M*

Language L(M) denotes the set of all

strings accepted by M, i.e.,

Example

q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

w=0100100110

(q0, 0100100110)

M M(q2, 100100110)

M M(q3, 00100110)

M M(q1, 0100110)

M M(q3, 100110)

M M(q2, 00110)

M M(q0, 0110)

M M(q2, 110)

M M (q3, 10)

M M (q2, 0)

M M(q0, )

M

wL(M)?

0q F

Example

q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

w=0100100110

(q0, 0100100110)

M M(q2, 100100110)

M M(q3, 00100110)

M M(q1, 0100110)

M M(q3, 100110)

M M(q2, 00110)

M M(q0, 0110)

M M(q2, 110)

M M (q3, 10)

M M (q2, 0)

M M(q0, )

M

wL(M)?

0q F

( , )s w 0( , )q M M*

M M*

Example

q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

M *{0,1} and has even( )

numbers of 0's and 1's.

w wL M w

Example

q0 q1 q2 q3> b b b

ba a

a

a

*{ , } and does not( )

contain three consecutive 's.

w a b wL M w

b

( ) ?L M

Exercises

Describe informally the languages accepted by the deterministic finite automata shown below:

a

ba

a, b

a, b

b

> >

a

b

ab

a, b

a, b

a, b

(a) (b)

Exercises

Construct deterministic finite automata accepting each of the following languages

a) {w{a, b}*: each a in w is immediately preceded and immediately followed by a b}.

b) {w{a, b}*: w has abab as a substring}.c) {w{a, b}*: w has neither aa nor bb as a substrin

g}.d) {w{a, b}*: w has both ab and ba as a substring}.

Lecture 5: Finite Automata

Nondeterministic

Finite Automata

大同大學資工所智慧型多媒體研究室

Review Deterministic Finite Automaton

A deterministic finite automaton is a 5-tuple

(K, , , s, F)K : a finite set of states : a finite set of symbols

: the transition function K Ks K : the initial state

F K : the set of final states

: the transition function K K2K : the transition function K 2K

A deterministic finite automaton is a 5-tuple

(K, , , s, F)

non,

A nondeterministic finite automaton is a 5-tuple

(K, , , s, F)

Definition Nondeterministic Finite Automaton

K : a finite set of states : a finite set of symbols

s K : the initial state

F K : the set of final states

Transition Functions of FA’s

: K 2K: K K

p

a

qK

p

a

Q K

Transition Functions of FA’s

: K 2K: K K

p

a

qK

p

a

Q K

Change to state q

deterministically.

Change to state q

deterministically.

Change to one of state in Q nondeterministically.

Change to one of state in Q nondeterministically.

Yields in One Step M M

( , )q aw M M ( , )q w

if ( , )q q a

M M

M = (K, , , s, F)

Yields

( , )q w ( , )q w

M M*

M M*

M M*

in some (including zero) steps

if is possible.( , )q w M M M M ( , )q w ( , )q w M M M M ( , )q w

M = (K, , , s, F)

String Acceptance by a NFA

M = (K, , , s, F)

w* is said to be accepted by M iff

( , )s w ( , )q M M* M M

*

such that q F

Language Defined by an NFA

M = (K, , , s, F)

*( ) : , ( , ) ( , ),L M w w s w q q F M M*

M M*

Language L(M) denotes the set of all

strings accepted by M, i.e.,

Example: L=(ab+aba)*

a

q0 q1 q2 q3a b a

b>

DFANFA

q4

abb

a,b

q1

a

q0>b

q2

ba

Extension: -Move & Multi-Move

NFA

q1

a

q0>b

q2

ba

q1aq0>

ba

q2

L=(ab+aba)*

Extension: -Move & Multi-Move

NFA

q1

a

q0>b

q2

ba

q1aq0>

b

q2

L=(ab+aba)*

a,

q1

ab

q0>a,

> q0

ab

aba

: the transition function K* 2K

A nondeterministic finite automaton is a 5-tuple

(K, , , s, F)

Definition Nondeterministic Finite Automaton

K : a finite set of states : a finite set of symbols

s K : the initial state

F K : the set of final states

Yields in One Step M M

( , )q w M M ( , )q w

* , ( , )u w uw q q u iff st. and

M M

M = (K, , , s, F)

Yields

( , )q w ( , )q w

M M*

M M*

M M*

in some (including zero) steps

if is possible.( , )q w M M M M ( , )q w ( , )q w M M M M ( , )q w

M = (K, , , s, F)

String Acceptance by a NFA

M = (K, , , s, F)

w* is said to be accepted by M iff

( , )s w ( , )q M M* M M

*

such that q F

Language Defined by an NFA

M = (K, , , s, F)

*( ) : , ( , ) ( , ),L M w w s w q q F M M*

M M*

Language L(M) denotes the set of all

strings accepted by M, i.e.,

Lecture 5: Finite Automata

Equivalence of

DFA &NFA

大同大學資工所智慧型多媒體研究室

Equivalence of Finite Automata

Finite automata M1 and M2 are said to be equivalent iff

1 2( ) ( )L M L M

Machine Simulation

NFA DFA

?

?

Of CourseOf Course

To be discussedTo be discussed

Lemma

For each NFA, there is an equivalent NFA

finite automaton without multi-move on

the transition function.

q q’1 k

q p11 p2 pk1 q’2 k

Example

q1

bab

q0>

a

q2ba

a

b

q1

b

q0>

a

q2b

a

b

p1 p2

p3a

a

b

In the following, we will assume the NFA being discussed is one without multi-move unless otherwise stated.

-Closure

M = (K, , , s, F)

In the following, we will assume the NFA being discussed is one without multi-move unless otherwise stated.

( ) : ( , ) ( , )q p K q w p w M M* -Closure of state q

without consumeany input

Example

q0

q1

q2

q3

q4

a

b

a

b

a

>

M ( ) ?L M * *( )a a b bb a

0 ?( )q

1 ?( )q

2 ?( )q

3 ?( )q

4 ?( )q

0 1 2 3, , ,q q q q

1 2 3, ,q q q

2q

3q

3 4,q q

To be used in the next example.

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf) Let M = (K, , , s, F) be an NFA.

To prove the theorem, we must be able to build a DFA M’ = (K’, ’, , s’, F’) such that L(M)=L(M’).

?K ?

?s ?F

?

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf) Let M = (K, , , s, F) be an NFA.

To prove the theorem, we must be able to build a DFA M’ = (K’, ’, , s’, F’) such that L(M)=L(M’).

?K ?

?s ?F

? 2K

( )s 2 :KQ Q F

?

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf)

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

Next, we have to show that ( ) ( ).L M L M

?

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf)

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

Next, we have to show that ( ) ( ).L M L M

Exercises:

1.Referring to the textbook to complete the proof in detail.

2.To present the proof using PowerPoint.Exercises:

1.Referring to the textbook to complete the proof in detail.

2.To present the proof using PowerPoint.

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M ( ) ?L M * *( )a a b bb a

0 ?( )q

1 ?( )q

2 ?( )q

3 ?( )q

4 ?( )q

0 1 2 3, , ,q q q q

1 2 3, ,q q q

2q

3q

3 4,q q

?M

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?K ?

?s ?F

?

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?K ?

?s ?F

? ,a b0 4{ , , }2 q q

0 1 2 3, , ,q q q qThere are 32 possible subsets. However, most of them are useless.

There are 32 possible subsets. However, most of them are useless.

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?K ?

?s ?F

? ,a b0 4{ , , }2 q q

0 1 2 3, , ,q q q q 4: :Q K q Q

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?s 0 1 2 3, , ,q q q q

0 1 2 3, , ,q q q q>M’

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

0 1 2 3, , ,q q q q> 0 4( ) ( )q q a

b

2 4( ) ( )q q

M’

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

0 1 2 3, , ,q q q q> 0 4( ) ( )q q a

b

2 4( ) ( )q q

0 1 2 3 4, , , ,q q q q q

2 3 4, ,q q q

b

a

3 4,q qa, b

a

b

a, b

M’

Example

q0

q1

q2

q3

q4

a

b

a

b

a

>

M

0 1 2 3, , ,q q q q> 0 4( ) ( )q q a

b

2 4( ) ( )q q

0 1 2 3 4, , , ,q q q q q

2 3 4, ,q q q

b

a

3 4,q qa, b

a

b

a, b

M’

* *( ) ( )L M a a b bb a ( )L M

JFLAP

Exercises Construct deterministic finite automata equivalent

to the nondeterministic automata shown below:

a, b

b

ab

b

a

a

>q0

q1

q2 q3

q4

>

bba

abaq0

(a) (b)

Lecture 5: Finite Automata

Properties of

Languages Accepted by

Finite Automata

大同大學資工所智慧型多媒體研究室

The Chomsky Hierarchy

Chomsky Hierarchy

Languages Grammars Automaton

Type 0 Recursively enumerable

unrestricted Turing Machine

Recursive unrestricted Decider

Type 1 Context-Sensitive Context-Sensitive

Linear-Bounded Automaton

Type 2 Context-Free Context-Free Push-Down Automaton

Type 3 Regular Regular NFA or DFA

The Chomsky Hierarchy

Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1L 2L

1L

2L1 2L L

1 2L L*1L

*2L

1 2L L

2 1L L

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

>M1 >M2

>

accepts

1 2( ) ( )L M L M

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

>M2

accepts 1 2( ) ( )L M L M

>M1

>

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

>M2

accepts *1( )L M

>M1

>

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

( , , , , )

( , , , , )

M K s F

M K s K F

( )M L M accepts

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1 2L L 1 2L L

1 2L L

Theorem

There are algorithms to answer the following

questions about finite automata:

a) w L(M)?

b) L(M) = ?

c) L(M) = *?

d) L(M1) L(M2) ?

e) L(M1) = L(M2) ?

Theorem

There are algorithms to answer the following

questions about finite automata:

a) w L(M)?

b) L(M) = ?

c) L(M) = *?

d) L(M1) L(M2) ?

e) L(M1) = L(M2) ?

Feed w into M.

Check any final state is reachable Check L(M) = ?

Check L(M1) L(M2) = ? Check L(M1) L(M2) ? L(M2) L(M1) ?

Exercises

Draw state diagrams for nondeterministic finite automata accepting these languages:

a) (ab)*(ba)*aa*

b) ((abaab)*a*)*

c) ((a*b*a*)*b)*

d) (bab)*(bba)*

Lecture 5: Finite Automata

Finite Automata&

Regular Expressions

大同大學資工所智慧型多媒體研究室

The Languages Accepted by FA’s

The languages accepted by FA’s are called regular sets.

How to describe a regular set?– Regular Expression

Definition Regular Expressions

A regular expression is defined inductively over the alphabet { (, ), , , +, *} as follows:

, , and each is a regular expression If and are regular expressions, then

– ( + )

– ( )

– *

are regular expressions

basic

inductive

Definition Regular Expressions

A regular expression is defined inductively over the alphabet { (, ), , , +, *} as follows:

, , and each is a regular expression If and are regular expressions, then

– ( + )

– ( )

– *

are regular expressions

basic

inductive

union, or concatenation

Kleene closure

Writing a Regular Expression

For omitting parentheses, the precedence of ‘operators’ are assigned as follows:

* > • > +((0(1*))+0) 01*+0

What is this language?

What is this language?

Examples

1. 00

2. (0+1)*

3. (0+1)* 00 (0+1)*

4. (0+) (1+10)*

5. (0+1)*011

6. 0*1*2*

7. 00*11*22*

What do the following regular expressions represent?

More ExamplesIs the following language L an r.e. over =[a-z]?

L={what, which, when, where}.

Write L as an r.e.

L = what + which + when + where

L = (wh)(at + ich + en + ere)

L = (wh)(at + ich) + (whe) (n + re)

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”

“”

Pf)

Given a regular expression, how to construct an FA to accept the language described by it?

Given an FA, what is its corresponding regular expression?

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a regular expression r, we can construct an NFA to accept the language described it inductively based on the number of `operators' it possesses as follows.

Basis (zero operators)Case 1: r =

Case 2: r =

Case 3: r = a

q0>

qfq0>

qfq0a>

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a regular expression r, we can construct an NFA to accept the language described it inductively based on the number of `operators' it possesses as follows.

Basis (zero operators)

trueHypothesis

Assumed true for r.e.’s few than m operatorsInduction

To be shown true for r.e.’s with m+1 operators

Case 1: r = r1+r2

Case 2: r = r1r2

Case 3: r = r1*

r : m+1 operatorsr1: m operatorsr2: m operators

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a regular expression r, we can construct an NFA to accept the language described it inductively based on the number of `operators' it possesses as follows.

Basis (zero operators)

trueHypothesis

Assumed true for r.e.’s few than m operatorsInduction

To be shown true for r.e.’s with m+1 operators

Case 1: r = r1+r2

Case 2: r = r1r2

Case 3: r = r1*

r : m+1 operatorsr1: m operatorsr2: m operators

The class of languages accepted by fi nite automata is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1L 2L

1L

2L1 2L L

1 2L L*1L

*2L

1 2L L

2 1L L

The class of languages accepted by fi nite automata is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1L 2L

1L

2L1 2L L

1 2L L*1L

*2L

1 2L L

2 1L L

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a DFA, we will determine its corresponding regular expression also inductively.

Consider DFA M = ({q1, q2,…,qn}, , , q1, F).

qi qj

ql

x

y

l k

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

*( , ) ( , ) ( , )

i l jkij

q x δ q y qR x

l k y x y

satisfi es if and

M M*

M M* M M

* M M

*

Define

qi qj

ql

x

y

l k

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a DFA, we will determine its corresponding regular expression also inductively.

Consider DFA M = ({q1, q2,…,qn}, , , q1, F).

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

*( , ) ( , ) ( , )

i l jkij

q x δ q y qR x

l k y x y

satisfi es if and

M M*

M M* M M

* M M

*

Define

kijR is regular for

any i, j, and k.

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a DFA, we will determine its corresponding regular expression also inductively.

Consider DFA M = ({q1, q2,…,qn}, , , q1, F).

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

*( , ) ( , ) ( , )

i l jkij

q x δ q y qR x

l k y x y

satisfi es if and

M M*

M M* M M

* M M

*

Define

kijR is regular for

any i, j, and k.1( ) n

ff FL M R

( )L M is reguar.

Equivalence FA & RE

FA RE

The Chomsky Hierarchy

Chomsky Hierarchy

Languages Grammars Automaton

Type 0Recursively enumerable

unrestricted Turing Machine

Recursive unrestricted Decider

Type 1 Context-SensitiveContext-Sensitive

Linear-Bounded Automaton

Type 2 Context-Free Context-Free Push-Down Automaton

Type 3 Regular Regular NFA or DFA

FA

The Chomsky Hierarchy

Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular

Example

Find an r.e. to represent the following FA.

M: q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 11 0 0 0 0

11 11 11 11 11( )*r r r r r 1 0 0 0 0

12 11 11 12 12( )* 0r r r r r 1 0 0 0 0

13 11 11 13 13( )* 1r r r r r 1 0 0 0 021 21 11 11 21( )*r r r r r 01 0 0 0 022 21 11 12 22( )* 00r r r r r 1 0 0 0 023 21 11 13 23( )* 1 01r r r r r 1 0 0 0 031 31 11 11 31( )*r r r r r 1 0 0 0 032 31 11 12 32( )* 0 1r r r r r 1 0 0 0 033 31 11 13 33( )*r r r r r

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

0( , )l i l jl

ij

a δ q a qr

otherwise

1 1 1 1( )*k k k k kij ik kk kj ijr r r r r

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 10

1

0

+00

1+01

0+1

2 1 1 1 111 12 22 21 11( )* 0( 00)*0r r r r r 2 1 1 1 1

12 12 22 22 12( )* 0( 00)*( 00) 0r r r r r 2 1 1 1 1

13 12 22 23 13( )* 0( 00)*(1 01) 1r r r r r 2 1 1 1 1

21 22 22 21 21( )* ( 00)( 00)*0 0r r r r r 2 1 1 1 1

22 22 22 22 22( )* ( 00)( 00)*( 00) ( 00)r r r r r 2 1 1 1 1

23 22 22 23 23( )* ( 00)( 00)*(1 01) (1 01)r r r r r 2 1 1 1 1

31 32 22 21 31( )* (0 1)( 00)*0r r r r r 2 1 1 1 1

32 32 22 22 32( )* (0 1)( 00)*( 00) (0 1)r r r r r 2 1 1 1 1

33 32 22 23 33( )* (0 1)( 00)*(1 01)r r r r r

k = 2(00)*

0(00)*

0*1

0(00)*

(00)*

0*1

(0+1)(00)*0

(0+1)(00)*

+(0+1)0*1

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 10

1

0

+00

1+01

0+1

k = 2(00)*

0(00)*

0*1

0(00)*

(00)*

0*1

(0+1)(00)*0

(0+1)(00)*

+(0+1)0*1

3 2 2 2 212 13 33 32 12( )*

0*1( (0 1)0*1)*(0 1)(00)* 0(00)*

0*1((0 1)0*1)*(0 1)(00)* 0(00)*

r r r r r

3 2 2 2 213 13 33 33 13( )*

0*1( (0 1)0*1)*( (0 1)0*1) 0*1

0*1((0 1)0*1)*

r r r r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 10

1

0

+00

1+01

0+1

k = 2(00)*

0(00)*

0*1

0(00)*

(00)*

0*1

(0+1)(00)*0

(0+1)(00)*

+(0+1)0*1

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

3 2 2 2 212 13 33 32 12( )*

0*1( (0 1)0*1)*(0 1)(00)* 0(00)*

0*1((0 1)0*1)*(0 1)(00)* 0(00)*

r r r r r

3 2 2 2 213 13 33 33 13( )*

0*1( (0 1)0*1)*( (0 1)0*1) 0*1

0*1((0 1)0*1)*

r r r r r

3 312 13 0*1((0 1)0*1)*( (0 1)(00)*) 0(00)*r r 3 3

12 13 0*1((0 1)0*1)*( (0 1)(00)*) 0(00)*r r

Lecture 5: Finite Automata

Languages Not Accepted by

Finite Automata

大同大學資工所智慧型多媒體研究室

What FA’s can? What FA’s cannot?

1 { | {0, ,9}* 2 | ( ) 3 | ( )}L w w val w val w st. and

2

{ , }* | |w a b wL w

w a

st. is and

contains number of

odd

even 's

3 0n nL a b n

4 nL a n is prime

Which of the following languages are regular?

The Pumping Theory

Let L be an infinite regular set. Then there are strings u,v, and w st. |v|>0 and uvnwL for all n 0.

Pf)Let L is accepted by a DFA M = (K, , , q0 , F) with m states.

Consider input w = 1 2… l, l m.

1 20( , , , , )lq M M 21( , , , )lq M M M M 1( , )llq M M ( , )lq . . .

Then,

By pigeonhole, there exists j, k with 0 j < k l st. qj = qk.

qj = qk qmu=1… j

v= j+1… k

w= k+1… mq0>

The Pumping Theory

Let L be an infinite regular set. Then there are strings u,v, and w st. |v|>0 and uvnwL for all n 0.

Pf)Let L is accepted by a DFA M = (K, , , q0 , F) with m states.

Consider input w = 1 2… l, l m.

1 20( , , , , )lq M M 21( , , , )lq M M M M 1( , )llq M M ( , )lq . . .

Then,

By pigeonhole, there exists j, k with 0 j < k l st. qj = qk.

v= j+1… k

qj = qk qmu=1… j w= k+1… mq0>

It is then seen that if qmF, then

uw L, uvw L, uv2w L, . . ., uvnwL for all n 0.

ExampleShow that the language L={anbn | n0} is not regular.

Pf) Suppose that L is regular.By pumping theorem,

u v wCase 1: z = a a … a b b … b

uvmwL for all m 0

z = anbn =uvwL st. uvmwL for all m 0.

u v wCase 2: z = a a … a b b … b

u v wCase 3: z = a a … a b b … b

The Chomsky Hierarchy

Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular

L={anbn | n0}L={anbn | n0}

Lecture 5: Finite Automata

Implementation of DFA

大同大學資工所智慧型多媒體研究室

Example Input a 0/1 sting. If the numbers of 0 and 1 in the

string are both even, then it is legal; otherwise, it is illegal.– 0011001001(legal)– 11001001001(illegal)

Writing a C program to do so.

q0 q1

q2 q3

0

0

0

0

1 1 1 1

Examples:

00100011<CR>

01101011<CR>

001010101<CR>

0010010101<CR>

Finite State Machine

Finite State Machine

q0 q1

q2 q3

0

0

0

0

1 1 1 1

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

Implementation (I)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

Implementation (I)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

int parser[4][3]={q1, q2, fini,q0, q3, fini,q3, q0, fini,q2, q1, fini

};

int state=q0;int event;char* str;

int parser[4][3]={q1, q2, fini,q0, q3, fini,q3, q0, fini,q2, q1, fini

};

int state=q0;int event;char* str;

Implementation (I)

void ToEvent(char c){

if(c == ’0’) event = 0;else if(c == ’1’) event = 1;else event = 2;

}

void ToEvent(char c){

if(c == ’0’) event = 0;else if(c == ’1’) event = 1;else event = 2;

}

Event (or Message) EncodingEvent (or Message) Encoding

Implementation (I)

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);EventHandler(event);

}}

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);EventHandler(event);

}} Event (or Message) LoopEvent (or Message) Loop

Implementation (I)

void EventHandler(int event){

int next_state = parser[state][event];switch(next_state){ case fini:

printf(”%s\n”, state==q0 ? ”ok” : ”err”); default:

state = next_state; //change state}

}

void EventHandler(int event){

int next_state = parser[state][event];switch(next_state){ case fini:

printf(”%s\n”, state==q0 ? ”ok” : ”err”); default:

state = next_state; //change state}

}

Event HandlerEvent Handler

Implementation (II)

q0 q1

q2 q3

0

0

0

0

1 1 1 1

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

void pq0(), pq1(), pq2(), pq3();void ok(), err();void pq0(), pq1(), pq2(), pq3();void ok(), err();

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

void pq0(){

state = q0;}

void pq1(){

state = q1;}

void pq0(){

state = q0;}

void pq1(){

state = q1;}

void pq2(){

state = q2;}

void pq3(){

state = q3;}

void pq2(){

state = q2;}

void pq3(){

state = q3;}

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

void ok(){

printf(”ok\n”);state = fini;

}

void err(){

printf(”error\n”);state = fini;

}

void ok(){

printf(”ok\n”);state = fini;

}

void err(){

printf(”error\n”);state = fini;

}

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

typedef void (*FUNCTION)();

FUNCTION parser[4][3]={pq1, pq2, ok,pq0, pq3, err,pq3, pq0, err,pq2, pq1, err

};

typedef void (*FUNCTION)();

FUNCTION parser[4][3]={pq1, pq2, ok,pq0, pq3, err,pq3, pq0, err,pq2, pq1, err

};

Implementation (II)

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);(*parser[state][event])();

}}

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);(*parser[state][event])();

}} Event (or Message) LoopEvent (or Message) Loop

top related