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 wL(M)?wL(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 wL(M)?wL(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 KK p a Q K K
Transition Functions of FA’s : K 2 K : K K p a q KK 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 pk1pk1 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