F ormal De�niti on of Finite Automaton 1. Finite set of states , t ypically Q . 2. Alphab et of input symb ols , t ypically �. 3. One state is the start/initial state, t ypically q . 0 4. Zero or more �nal/ac c epting states; the set is t ypically F . 5. A tr ansition function , t ypically � . This function: ✦ T ak es a state and input sym b ol as argumen ts. ✦ Returns a state. ✦ One \rule" of � w ould b e written � ( q ; a ) = p , where q and p are states, and a is an input sym b ol. ✦ In tuitiv ely: if the F A is in state q , and input a is receiv ed, then the F A go es to state p (note: q = p OK). � A F A is represen ted as the �v e-tuple: = A ( Q; � ; ). � ; q ; F 0 Example: Clamping Logic W e ma y think of an accepting state as represen ting a \1" output and nonaccepting states as represen ting \0" out. A \clamping" circuit w aits for a 1 input, and forev er after mak es a 1 output. Ho w ev er, to a v oid clamping on spurious noise, w e'll design a F A that w aits for t w o 1's in a ro w, and \clamps" only then. In general, w e ma y think of a state as represen ting a summary of the history of what has b een seen on the input so far. The states w e need are: 1. State , the start state, sa ys that the most q 0 recen t input (if there w as one) w as not a 1, and w e ha v e nev er seen t w o 1's in a ro w. 2. State sa ys w e ha v e nev er seen 11, but the q 1 previous input w as 1. 3. State q is the only accepting state; it sa ys 2 that w e ha v e at some time seen 11. � Th us, A = ( f q ; q ; q g ; f 0 ; 1 g ; � ; q ; f q g ), 0 1 2 0 2 where � is giv en b y: 1
0 1 ! q q q 0 0 1 q q q 1 0 2 � q q q 2 2 2 � ! By marking the start state with and � , accepting states with the tr ansition table that de�nes � also sp eci�es the en tire F A. Con v en tions It helps if w e can a v oid men tioning the t yp e of ev ery name b y follo wing some rules: � Input sym b ols are a , b , etc., or digits. � Strings of input sym b ols are u; v ; : : : ; z . � States are , p , etc. q T ransition Diagram A F A can b e represen ted b y a graph; no des = states; arc from q to p is lab eled b y the set of input sym b ols a suc h that � ( q ; a ) = p . � No arc if no suc h a . � Start state indicated b y w ord \start" and an arro w. � Accepting states get double circles. Example F or the clamping F A: 0,1 0 Start 1 1 q q q 0 1 2 0 Extension of � to P aths In tuitiv ely , a F A ac c epts a string w = a a � � � a if 1 2 n there is a path in the transition diagram that: 1. Begins at the start state, 2. Ends at an accepting states, and 3. Has sequence of lab els a ; a ; : : : ; a . 1 2 n F ormally , w e extend transition function � to ^ � ( q ; w ), where w can b e an y string of input sym b ols: 2
^ � Basis: ( q � ) = (i.e., on no input, the F A � ; q do esn't go an ywhere. ^ ^ � � � Induction: � ( q ; w a ) = � � ( q ; w ) ; a , where w is a string, and a a single sym b ol (i.e., see where the F A go es on w , then lo ok for the transition on the last sym b ol from that state). � Imp ortan t fact with a straigh tforw ard, ^ inductiv e pro of: really represen ts paths. � That is, if = � � � , and ( p ) = w a a a � ; a p n i i i +1 1 2 ^ for all = 0 ; 1 ; � 1, then ( p ) = . i : : : ; n � ; w p n 0 Acceptance of Strings A F A A = ( Q; � ; � ; q ; F ) accepts string w if 0 ^ � ( q ; w ) is in F . 0 Language of a F A ^ F A accepts the language L ( A ) = f w j ( q ) is A � ; w 0 in g . F Aside: T yp e Errors A ma jor source of confusion when dealing with automata (or mathematics in general) is making \t yp e errors." � Example: Don't confuse A , a F A, i.e., a program, with L ( A ), whic h is of t yp e \set of strings." � Example: the start state is of t yp e \state," q 0 but the accepting states is of t yp e \set of F states." � T ric kier example: Is a a sym b ol or a string of length 1? ✦ Answ er: it dep ends on the con text, e.g., is it used in � ( q ; a ), where it is a sym b ol, ^ or � ( q ; a ), where it is a string? Nondetermini st ic Finite Automata Allo w (deterministic) F A to ha v e a c hoice of 0 or more next states for eac h state-input pair. � Imp ortan t to ol for designing string pro cessors, e.g., grep , lexical analyzers. � But \imaginary , " in the sense that it has to b e implemen ted deterministically . Example In this somewhat con triv ed example, w e shall design an NF A to accept strings o v er alphab et f 1 ; 2 ; 3 g suc h that the last sym b ol app ears 3
previously , without an y in terv ening higher sym b ol, e.g., � � � 11, � � � 21112, � � � 312123. � T ric k: use start state to mean \I guess I ha v en't seen the sym b ol that matc hes the ending sym b ol y et. � Three other states represen t a guess that the matc hing sym b ol has b een seen, and remem b ers what that sym b ol is. q 1 1 Start 2 2 p r t 1 1,2,3 3 3 s 1,2 F ormal NF A N = ( Q; � ; � ; q ; F ), where all is as DF A, but: 0 � ( q a ) is a set of states, rather than a single � ; state. ^ Extension to � ^ � Basis: ( q � ) = f q g . � ; � Induction: Let: ^ ✦ � ( q ; w ) = f p ; p ; : : : ; p g . 1 2 k ✦ ( p a ) = for = 1 ; 2 . � ; S i : : : ; k i i ^ Then � ( q ; w a ) = S [ S [ � � � [ S . 1 2 k Language of an NF A An NF A accepts if any path from the start state w to an accepting state is lab eled . F ormally: w ^ � f w j \ ;g . L ( N ) = � ( q ; w ) F 6 = 0 Subset Construction � F or ev ery NF A there is an e quivalent (accepts the same language) DF A. � But the DF A can ha v e exp onen tially man y states. 4
Let = ( Q � ; ) b e an NF A. N ; � ; q ; F N N N 0 The equiv alen t DF A constructed b y the subset construction is D = ( Q ; � ; � ; f q g ; F ), where: D D 0 D Q 1. Q = 2 ;. i.e., Q is the set of all subsets of N D D Q . N \ 2. F is the set of sets S in Q suc h that S N D ; . F 6 = ( f q g ; [ � ; q ; : : : ; q a ) = � ( p ; a ) D 1 2 k N 1 [ � � � [ � ( p ; a ) � ( p ; a ). N 2 N k � Key theorem (induction on j w j , pro of in ^ ^ ( f q g ; b o ok): � w ) = � ( q ; w ). D 0 N 0 � Consequence: L ( D ) = L ( N ). Example: Subset Construction F rom Previous NF A An imp ortan t practical tric k, used in lexical analyzers and other text-pro cessors is to ignore the (often man y) states that are not accessible from the start state (i.e., no path leads there). � F or the NF A example ab o v e, of the 32 p ossible subsets, only 15 are accessible. Computing transitions \on demand" giv es the follo wing : � D 1 2 3 ! p pq pr ps pq pq t pr ps � pq t pq t pr ps pr pq r pr t ps � pr t pq r pr t ps ps pq s pr s pst � pst pq s pr s pst pr s pq r s pr st pst � pr st pq r s pr st pst pq s pq st pr s pst � pq st pq st pr s pst pq r pq r t pr t ps � pq r t pq r t pr t ps pq r s pq r st pr st pst � pq r st pq r st pr st pst 5
Recommend
More recommend