Lecture 5: Finite Automata 虞台文 大同大學資工所 智慧型多媒體研究室.

Slides:



Advertisements
Similar presentations
CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
Advertisements

YES-NO machines Finite State Automata as language recognizers.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 Languages. 2 A language is a set of strings String: A sequence of letters Examples: “cat”, “dog”, “house”, … Defined over an alphabet: Languages.
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY (For next time: Read Chapter 1.3 of the book)
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Regular.
1 Languages and Finite Automata or how to talk to machines...
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.
1 Regular Languages Finite Automata eg. Supermarket automatic door: exit or entrance.
CSC 361Finite Automata1. CSC 361Finite Automata2 Formal Specification of Languages Generators Grammars Context-free Regular Regular Expressions Recognizers.
Costas Busch - LSU1 Non-Deterministic Finite Automata.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Nondeterminism (Deterministic) FA required for every state q and every symbol  of the alphabet to have exactly one arrow out of q labeled . What happens.
Great Theoretical Ideas in Computer Science.
Regular Languages A language is regular over  if it can be built from ;, {  }, and { a } for every a 2 , using operators union ( [ ), concatenation.
Formal Language Finite set of alphabets Σ: e.g., {0, 1}, {a, b, c}, { ‘{‘, ‘}’ } Language L is a subset of strings on Σ, e.g., {00, 110, 01} a finite language,
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
Introduction to CS Theory Lecture 3 – Regular Languages Piotr Faliszewski
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
Class Discussion Can you draw a DFA that accepts the language {a k b k | k = 0,1,2,…} over the alphabet  ={a,b}?
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
Chapter 6 Properties of Regular Languages. 2 Regular Sets and Languages  Claim(1). The family of languages accepted by FSAs consists of precisely the.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
CS 203: Introduction to Formal Languages and Automata
Chapter 3 Regular Expressions, Nondeterminism, and Kleene’s Theorem Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction.
Lecture 3: Count Programs, While Programs and Recursively Defined Functions 虞台文 大同大學資工所 智慧型多媒體研究室.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Lecture 2: Limiting Models of Instruction Obeying Machine 虞台文 大同大學資工所 智慧型多媒體研究室.
Algorithms for hard problems Automata and tree automata Juris Viksna, 2015.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
Finite Automata Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture 20Mar 30, 2010Carnegie Mellon.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 12 Mälardalen University 2007.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
Regular Languages Chapter 1 Giorgi Japaridze Theory of Computability.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
Finite Automata A simple model of computation. 2 Finite Automata2 Outline Deterministic finite automata (DFA) –How a DFA works.
Lecture 6: Context-Free Languages
Lecture 7: Turning Machines 虞台文 大同大學資工所 智慧型多媒體研究室.
Lecture #5 Advanced Computation Theory Finite Automata.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
Chapter 2 Regular Languages & Finite Automata. Regular Expressions A finitary denotation of a regular language over . ØL Ø = Ø aL a = {a} where a ∈ 
Languages.
Non Deterministic Automata
PROPERTIES OF REGULAR LANGUAGES
Chapter 2 Finite Automata
CSE 105 theory of computation
REGULAR LANGUAGES AND REGULAR GRAMMARS
Hierarchy of languages
Properties of Regular Languages
CS 154, Lecture 3: DFANFA, Regular Expressions.
Non-Deterministic Finite Automata
Instructor: Aaron Roth
Chapter 1 Regular Language
Finite-State Machines with No Output
Lecture 5: Turning Machine
CSCI 2670 Introduction to Theory of Computing
CSCE 355 Foundations of Computation
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 Alphabets and Languages 大同大學資工所 智慧型多媒體研究室

Alphabets Alphabet  a finite set of symbols Examples:  binary alphabet  Roman alphabet  decimal alphabet  Greek alphabet  my alphabet

Strings String over an alphabet (  )  a finite sequence of symbols from   *  the set of all strings over  Examples:

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

More on Strings Substring Suffix Prefix  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:  * can be enumerated by:

Languages Language L over   any subset of  * i.e., L   * How many possible languages over  ? How to specify a language?  uncountably infinite

Operations of Languages Complement Union Intersection

Operations of Languages Complement, Union and Intersection Concatenation Closure or Kleene Star Positive Closure

Example: Kleene Star ?? 1909 – 1994

Example: Kleene Star ??  1909 – 1994

Example: Kleene Star ? ? 1909 – 1994

Language Recognition Device A machine or algorithm to answer the question

Lecture 5: Finite Automata Deterministic Finite Automata 大同大學資工所 智慧型多媒體研究室

Finite State Machine Reading Head Input tape q0q0 q1q1 q3q3 q2q2 Finite control q5q5 q4q4 a a b b a a a a b b b b a a b b a a a a

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   K s  K : the initial state F  K : the set of final states

Example K={q 0, q 1, q 2, q 3 }  ={0, 1}  q0q0 q1q1 q2q2 q3q3 01 q2q2 q1q1 q3q3 q0q0 q0q0 q3q3 q1q1 q2q2 symbol state s=q 0 F= {q0}F= {q0} q0q0 q2q2 q1q1 q3q > (K, , , s, F)

Operations of a DFA q0q0 q2q2 q1q1 q3q > q0q0 q1q1 q3q3 q2q2 Finite control Reading Head Input tape

Operations of a DFA q0q0 q2q2 q1q1 q3q > q0q0 q1q1 q3q3 q2q2 Finite control Reading Head 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 = q 0 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 = q 0 Sense input symbol Change state based on  Move head right one cell Give answer when head reaches end

DFA As a Language Acceptor q0q0 q2q2 q1q1 q3q > q0q0 q1q1 q3q3 q2q2 Finite control Reading Head Input tape 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 q0q0 q2q2 q1q1 q3q > q0q0 q1q1 q3q3 q2q2 Finite control Reading Head Input tape 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?

DFA As a Language Acceptor q0q0 q2q2 q1q1 q3q > q0q0 q1q1 q3q3 q2q2 Finite control Reading Head Input tape 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? q3q3

Memory Configuration of a DFA M = (K, , , s, F) Reading Head Input tape q0q0 q1q1 q3q3 q2q2 Finite control q5q5 q4q4 a a b b a a a a b b b b a a b b a a a a u w

Memory Configuration of a DFA M = (K, , , s, F) Reading Head Input tape q0q0 q1q1 q3q3 q2q2 Finite control q5q5 q4q4 a a b b a a a a b b b b a a b b a a a a u w Does not effect the result.

Memory Configuration of a DFA M = (K, , , s, F)

Memory Configuration of a DFA M = (K, , , s, F)

Memory Configuration of a DFA Reading Head Input tape q0q0 q1q1 q3q3 q2q2 Finite control q5q5 q4q4 a a b b a a a a b b b b a a b b a a a a u w

Yields in One Step if

Yields if * in some (including zero) steps

String Acceptance by a DFA M = (K, , , s, F) w  * is said to be accepted by M iff such that

Language Defined by a DFA M = (K, , , s, F) Language L(M) denotes the set of all strings accepted by M, i.e.,

Example q0q0 q2q2 q1q1 q3q > w= (q 0, ) (q 2, ) (q 3, ) (q 1, ) (q 3, ) (q 2, 00110) (q 0, 0110) (q 2, 110) (q 3, 10) (q 2, 0) (q 0,  ) M wL(M)?wL(M)?

Example q0q0 q2q2 q1q1 q3q > w= (q 0, ) (q 2, ) (q 3, ) (q 1, ) (q 3, ) (q 2, 00110) (q 0, 0110) (q 2, 110) (q 3, 10) (q 2, 0) (q 0,  ) M wL(M)?wL(M)?

Example q0q0 q2q2 q1q1 q3q > M

q0q0 q1q1 q2q2 q3q3 > b b b b a a a a

Exercises Describe informally the languages accepted by the deterministic finite automata shown below: a b a a, b 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 substring }. 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   K s  K : the initial state F  K : the set of final states

 : the transition function K   K 2K2K  : the transition function K   2 K  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   2 K  : K   K  p a q KK  p a Q  K K

Transition Functions of FA’s  : K   2 K  : K   K  p a q KK  p a Q  K K Change to state q deterministically. Change to one of state in Q nondeterministically.

Yields in One Step if M = (K, , , s, F)

Yields * in some (including zero) steps if is possible. M = (K, , , s, F)

String Acceptance by a NFA M = (K, , , s, F) w  * is said to be accepted by M iff such that

Language Defined by an NFA M = (K, , , s, F) Language L(M) denotes the set of all strings accepted by M, i.e.,

Example: L=(ab+aba) * a q0q0 q1q1 q2q2 q3q3 aba b > DFA NFA q4q4 a bb a,ba,b q1q1 a q0q0 > b q2q2 b a

 Extension:  -Move & Multi- Move NFA q1q1 a q0q0 > b q2q2 b a q1q1 a q0q0 > b a q2q2 L=(ab+aba) *

Extension:  -Move & Multi- Move NFA q1q1 a q0q0 > b q2q2 b a q1q1 a q0q0 > b q2q2 L=(ab+aba) * a,  q1q1 ab q0q0 > a,  > q0q0 ab aba

 : the transition function K   *  2 K 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 = (K, , , s, F)

Yields * in some (including zero) steps if is possible. M = (K, , , s, F)

String Acceptance by a NFA M = (K, , , s, F) w  * is said to be accepted by M iff such that

Language Defined by an NFA M = (K, , , s, F) 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 M 1 and M 2 are said to be equivalent iff

Machine Simulation NFA DFA ? ? Of Course To be discussed

Lemma For each NFA, there is an equivalent NFA finite automaton without multi-move on the transition function. q q’q’ qp1p1 p2p2 pk1pk1 q’q’

Example q1q1 bab q0q0 >  a q2q2 ba a b q1q1 b q0q0 >  a q2q2 b a b p1p1 p2p2 p3p3 a 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. *  -Closure of state q without consume any input

Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M 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 ’ ).

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 ’ ).

Theorem For each nondeterministic finite automaton, there is an equivalent deterministic finite automaton. Pf) Next, we have to show that

Theorem For each nondeterministic finite automaton, there is an equivalent deterministic finite automaton. Pf) Next, we have to show that 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 q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M

q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M

q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M There are 32 possible subsets. However, most of them are useless.

Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M

q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M > M’M’

q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M > a b M’M’

q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M > a b b a a, b a  b M’M’

Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a     > M > a b b a a, b a  b M’M’ JFLAP

Exercises Construct deterministic finite automata equivalent to the nondeterministic automata shown below: a, b  b ab b a a  > q0q0 q1q1 q2q2 q3q3 q4q4 > bba aba q0q0 (a)(b)

Lecture 5: Finite Automata Properties of Languages Accepted by Finite Automata 大同大學資工所 智慧型多媒體研究室

The Chomsky Hierarchy Chomsky Hierarchy LanguagesGrammarsAutomaton Type 0Recursively enumerableunrestrictedTuring Machine RecursiveunrestrictedDecider Type 1Context-Sensitive Linear-Bounded Automaton Type 2Context-Free Push-Down Automaton Type 3Regular 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.

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. > M1M1 > M2M2 >   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. > M2M2 accepts > M1M1 >   

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. > M2M2 accepts > M1M1 >    

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.

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.

Theorem There are algorithms to answer the following questions about finite automata: a) w  L(M) ? b) L(M) =  ? c) L(M) =  * ? d) L(M 1 )  L(M 2 ) ? e) L(M 1 ) = L(M 2 ) ?

Theorem There are algorithms to answer the following questions about finite automata: a) w  L(M) ? b) L(M) =  ? c) L(M) =  * ? d) L(M 1 )  L(M 2 ) ? e) L(M 1 ) = L(M 2 ) ?  Feed w into M.  Check any final state is reachable  Check L(M) =  ?  Check L(M 1 )  L(M 2 ) =  ?  Check L(M 1 )  L(M 2 ) ? L(M 2 )  L(M 1 ) ?

Exercises Draw state diagrams for nondeterministic finite automata accepting these languages: a) (ab)*(ba)*  aa* b) ((ab  aab)*a*)* c) ((a*b*a*)*b)* d) (ba  b)*  (bb  a)*

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)((0(1*))+0)01*+0  What is this language?

Examples (0+1)* 3. (0+1)* 00 (0+1)* 4. (0+  ) (1+10)* 5. (0+1)* *1*2* 7. 00*11*22* What do the following regular expressions represent?

More Examples Is 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 q0q0 > qfqf q0q0 > qfqf q0q0 a >

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)  true Hypothesis Assumed true for r.e.’s few than m operators Induction To be shown true for r.e.’s with m+1 operators Case 1: r = r 1 +r 2 Case 2: r = r 1 r 2 Case 3: r = r 1 * r : m+1 operators r 1 :  m operators r 2 :  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)  true Hypothesis Assumed true for r.e.’s few than m operators Induction To be shown true for r.e.’s with m+1 operators Case 1: r = r 1 +r 2 Case 2: r = r 1 r 2 Case 3: r = r 1 * r : m+1 operators r 1 :  m operators r 2 :  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 DFA, we will determine its corresponding regular expression also inductively. Consider DFA M = ({q 1, q 2,…,q n }, , , q 1, F). qiqi qjqj qlql x y l  kl  k Define

qiqi qjqj qlql x y l  kl  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 = ({q 1, q 2,…,q n }, , , q 1, F). Define 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 = ({q 1, q 2,…,q n }, , , q 1, F). Define is regular for any i, j, and k.

Equivalence FA & RE FARE

The Chomsky Hierarchy Chomsky Hierarchy LanguagesGrammarsAutomaton Type 0Recursively enumerableunrestrictedTuring Machine RecursiveunrestrictedDecider Type 1Context-Sensitive Linear-Bounded Automaton Type 2Context-Free Push-Down Automaton Type 3Regular 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:M: q1q1 q2q2 q3q ,1 >

Example   1  0+1  k = 0k = 1

Example   1  0+1  k = 0k = 1    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

Example   1  0+1  k = 0k = 1    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

  1  0+1  k = 0k = 1    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 Example

Lecture 5: Finite Automata Languages Not Accepted by Finite Automata 大同大學資工所 智慧型多媒體研究室

What FA’s can? What FA’s cannot? 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 uv n w  L for all n  0. Pf) Let L is accepted by a DFA M = (K, , , q 0, F) with m states. Consider input w =  1  2 …  l, l  m.... Then, By pigeonhole, there exists j, k with 0  j < k  l st. q j = q k. q j = q k qmqm u=  1 …  j v=  j+1 …  k w=  k+1 …  m q0q0 >

The Pumping Theory Let L be an infinite regular set. Then there are strings u,v, and w st. |v|>0 and uv n w  L for all n  0. Pf) Let L is accepted by a DFA M = (K, , , q 0, F) with m states. Consider input w =  1  2 …  l, l  m.... Then, By pigeonhole, there exists j, k with 0  j < k  l st. q j = q k. v=  j+1 …  k q j = q k qmqm u=  1 …  j w=  k+1 …  m q0q0 > It is then seen that if q m  F, then uw  L,uvw  L,uv 2 w  L,..., uv n w  L for all n  0.

Example Show that the language L={a n b n | n  0} is not regular. Pf) Suppose that L is regular. By pumping theorem, u v w Case 1: z = a a … a b b … b uv m w  L for all m  0  z = a n b n =uvw  L st. uv m w  L for all m  0. u v w Case 2: z = a a … a b b … b u v w Case 3: z = a a … a b b … b

The Chomsky Hierarchy Non-recursively enumerable Recursively-enumerable Recursive Context-sensitive Context-free Regular L={a n b n | n  0}

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. – (legal) – (illegal) Writing a C program to do so.

q0q0 q1q1 q2q2 q3q Examples: Finite State Machine

q0q0 q1q1 q2q2 q3q state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser

Implementation (I) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser #defineq00 #defineq11 #defineq22 #defineq33 #definefini4 #defineq00 #defineq11 #defineq22 #defineq33 #definefini4

Implementation (I) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser int parser[4][3]={ q1, q2,fini, q0,q3,fini, q3,q0,fini, q2,q1,fini }; intstate=q0; intevent; char* str; int parser[4][3]={ q1, q2,fini, q0,q3,fini, q3,q0,fini, q2,q1,fini }; intstate=q0; intevent; 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) Encoding

Implementation (I) void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); EventHandler(event); } void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); EventHandler(event); } } Event (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 Handler

Implementation (II) q0q0 q1q1 q2q2 q3q state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser

Implementation (II) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser #defineq00 #defineq11 #defineq22 #defineq33 #definefini4 #defineq00 #defineq11 #defineq22 #defineq33 #definefini4 void pq0(), pq1(), pq2(), pq3(); void ok(), err(); void pq0(), pq1(), pq2(), pq3(); void ok(), err();

Implementation (II) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The 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) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The 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) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The 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 str state = q0; //initialization while(state!=fini){ ToEvent(*str++); (*parser[state][event])(); } void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); (*parser[state][event])(); } } Event (or Message) Loop