Download presentation
Presentation is loading. Please wait.
Published byAlexina Norris Modified over 9 years ago
1
Lecture 5: Finite Automata 虞台文 大同大學資工所 智慧型多媒體研究室
2
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
3
Lecture 5: Finite Automata Alphabets and Languages 大同大學資工所 智慧型多媒體研究室
4
Alphabets Alphabet a finite set of symbols Examples: binary alphabet Roman alphabet decimal alphabet Greek alphabet my alphabet
5
Strings String over an alphabet ( ) a finite sequence of symbols from * the set of all strings over Examples:
6
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
7
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.
8
Countability of * * is countably infinite for any finite alphabet . Example: * can be enumerated by:
9
Languages Language L over any subset of * i.e., L * How many possible languages over ? How to specify a language? uncountably infinite
10
Operations of Languages Complement Union Intersection
11
Operations of Languages Complement, Union and Intersection Concatenation Closure or Kleene Star Positive Closure
12
Example: Kleene Star ?? 1909 – 1994
13
Example: Kleene Star ?? 1909 – 1994
14
Example: Kleene Star ? ? 1909 – 1994
15
Language Recognition Device A machine or algorithm to answer the question
16
Lecture 5: Finite Automata Deterministic Finite Automata 大同大學資工所 智慧型多媒體研究室
17
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
18
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
19
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 q3q3 1 1 1 1 0 0 0 0 > (K, , , s, F)
20
Operations of a DFA q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > 01100101 q0q0 q1q1 q3q3 q2q2 Finite control Reading Head Input tape
21
Operations of a DFA q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > 01100101 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
22
DFA As a Language Acceptor q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > 01100101 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
23
DFA As a Language Acceptor q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > 01100101 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?
24
DFA As a Language Acceptor q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > 01100101 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
25
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
26
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.
27
Memory Configuration of a DFA M = (K, , , s, F)
28
Memory Configuration of a DFA M = (K, , , s, F)
29
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
30
Yields in One Step if
31
Yields if * in some (including zero) steps
32
String Acceptance by a DFA M = (K, , , s, F) w * is said to be accepted by M iff such that
33
Language Defined by a DFA M = (K, , , s, F) Language L(M) denotes the set of all strings accepted by M, i.e.,
34
Example q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > w=0100100110 (q 0, 0100100110) (q 2, 100100110) (q 3, 00100110) (q 1, 0100110) (q 3, 100110) (q 2, 00110) (q 0, 0110) (q 2, 110) (q 3, 10) (q 2, 0) (q 0, ) M wL(M)?wL(M)?
35
Example q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > w=0100100110 (q 0, 0100100110) (q 2, 100100110) (q 3, 00100110) (q 1, 0100110) (q 3, 100110) (q 2, 00110) (q 0, 0110) (q 2, 110) (q 3, 10) (q 2, 0) (q 0, ) M wL(M)?wL(M)?
36
Example q0q0 q2q2 q1q1 q3q3 1 1 1 1 0 0 0 0 > M
37
q0q0 q1q1 q2q2 q3q3 > b b b b a a a a
38
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)
39
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 }.
40
Lecture 5: Finite Automata Nondeterministic Finite Automata 大同大學資工所 智慧型多媒體研究室
41
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
42
: 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
43
Transition Functions of FA’s : K 2 K : K K p a q KK p a Q K K
44
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.
45
Yields in One Step if M = (K, , , s, F)
46
Yields * in some (including zero) steps if is possible. M = (K, , , s, F)
47
String Acceptance by a NFA M = (K, , , s, F) w * is said to be accepted by M iff such that
48
Language Defined by an NFA M = (K, , , s, F) Language L(M) denotes the set of all strings accepted by M, i.e.,
49
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
50
Extension: -Move & Multi- Move NFA q1q1 a q0q0 > b q2q2 b a q1q1 a q0q0 > b a q2q2 L=(ab+aba) *
51
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
52
: 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
53
Yields in One Step M = (K, , , s, F)
54
Yields * in some (including zero) steps if is possible. M = (K, , , s, F)
55
String Acceptance by a NFA M = (K, , , s, F) w * is said to be accepted by M iff such that
56
Language Defined by an NFA M = (K, , , s, F) Language L(M) denotes the set of all strings accepted by M, i.e.,
57
Lecture 5: Finite Automata Equivalence of DFA &NFA 大同大學資工所 智慧型多媒體研究室
58
Equivalence of Finite Automata Finite automata M 1 and M 2 are said to be equivalent iff
59
Machine Simulation NFA DFA ? ? Of Course To be discussed
60
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’
61
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.
62
-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
63
Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M To be used in the next example.
64
Theorem For each nondeterministic finite automaton, there is an equivalent deterministic finite automaton.
65
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 ’ ).
66
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 ’ ).
67
Theorem For each nondeterministic finite automaton, there is an equivalent deterministic finite automaton. Pf) Next, we have to show that
68
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.
69
Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M
70
q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M
71
q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M There are 32 possible subsets. However, most of them are useless.
72
Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M
73
q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M > M’M’
74
q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M > a b M’M’
75
q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M > a b b a a, b a b M’M’
76
Example q0q0 q1q1 q2q2 q3q3 q4q4 a b a b a > M > a b b a a, b a b M’M’ JFLAP
77
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)
78
Lecture 5: Finite Automata Properties of Languages Accepted by Finite Automata 大同大學資工所 智慧型多媒體研究室
79
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
80
The Chomsky Hierarchy Non-recursively enumerable Recursively-enumerable Recursive Context-sensitive Context-free Regular
81
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.
82
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
83
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 >
84
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 >
85
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.
86
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.
87
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 ) ?
88
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 ) ?
89
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)*
90
Lecture 5: Finite Automata Finite Automata & Regular Expressions 大同大學資工所 智慧型多媒體研究室
91
The Languages Accepted by FA’s The languages accepted by FA’s are called regular sets. How to describe a regular set? – Regular Expression
92
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
93
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
94
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?
95
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?
96
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)
97
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?
98
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 >
99
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
100
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
101
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
102
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.
103
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.
104
Equivalence FA & RE FARE
105
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
106
FA The Chomsky Hierarchy Non-recursively enumerable Recursively-enumerable Recursive Context-sensitive Context-free Regular
107
Example Find an r.e. to represent the following FA. M:M: q1q1 q2q2 q3q3 1 1 0 0 0,1 >
108
Example 0 1 0 1 0+1 k = 0k = 1
109
Example 0 1 0 1 0+1 k = 0k = 1 0 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
110
Example 0 1 0 1 0+1 k = 0k = 1 0 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
111
0 1 0 1 0+1 k = 0k = 1 0 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 Example
112
Lecture 5: Finite Automata Languages Not Accepted by Finite Automata 大同大學資工所 智慧型多媒體研究室
113
What FA’s can? What FA’s cannot? Which of the following languages are regular?
114
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 >
115
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.
116
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
117
The Chomsky Hierarchy Non-recursively enumerable Recursively-enumerable Recursive Context-sensitive Context-free Regular L={a n b n | n 0}
118
Lecture 5: Finite Automata Implementation of DFA 大同大學資工所 智慧型多媒體研究室
119
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.
120
q0q0 q1q1 q2q2 q3q3 0 0 0 0 1111 Examples: 00100011 01101011 001010101 0010010101 Finite State Machine
121
q0q0 q1q1 q2q2 q3q3 0 0 0 0 1111 state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser
122
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
123
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;
124
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
125
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
126
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
127
Implementation (II) q0q0 q1q1 q2q2 q3q3 0 0 0 0 1111 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
128
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();
129
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; }
130
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; }
131
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 };
132
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.