Lexicl Anlysis Finite Automte CMPSC 470 Lecture 04 Topics: Deterministic Finite Automt (DFA) Nondeterministic Finite Automt (NFA) Regulr Expression NFA DFA A. Finite Automt (FA) FA re grph, like trnsition digrm, with few differences. 1. FA re recognizers; they sy yes or no out possile input string. 2. There re two types of FA. Non-deterministic Finite Automt (NFA): node/stte cn hve severl edge outs for symol. lel of edge cn hve.. Deterministic Finite Automt (DFA): node/stte hs unique edge out for symol. Edges don t hve. Both NFA nd DFA cn recognize the sme lnguge clled, regulr lnguge.
B. Non-deterministic Finite Automt (NFA) B.) NFA definition NFA consists of: 1. A finite set of sttes 2. Set of input symol 3. Trnsition function 4. Strt stte (or initil stte) 5. Accepting sttes (or finl sttes) B.) NFA Trnsition Tle One of the wys to represent NFA (trnsition function) NFA Accepts input string xx if nd only if there is some pth in trnsition grph from strt (ss 0 ) to one of ccepting sttes (FF). Is ccepted y NFA? Is ccepted y NFA? Is ccepted y NFA?
Exmple) Given the following NFA, strt 0 1 2 3 How mny pths re there for? Does NFA ccept? Does NFA ccept? C. Deterministic Finite Automt (DFA) DFA is specil cse of NFA 1. There re no move on input 2. For ech stte ss nd input symol, there is only one edge out of ss leled. Drw symol tle for DFA. strt 0 1 2 Exmple) Drw DFA ccepting ( ). Does this DFA ccept? How mny pths does it hs?
Does this DFA ccept? How mny pths does this hs? Given n input string, DFA is esier to nswer yes or no thn NFA, ecuse DFA provides. D. Convert NFA to DFA Regulr expression is used in lexicl nlyzer. Regulr expression cn e esily converted to NFA. However, implementing lexicl nlyzer requires DFA implementtion. First, we will discuss: 1. how to convert NFA to DFA, nd then 2. how to convert regulr expression to NFA. The generl ide is tht ech stte of DFA corresponds to set of NFA. D.) Opertions -closure(ss): Set of NFA sttes rechle from stte ss on -trnsition lone -closure(tt): Set of NFA sttes rechle from set of sttes TT on -closure(ss) move(tt, ): Set of NFA sttes rechle from set of sttes TT on input symol
Generte DFA Given NFA, the following sequence of steps uilds DFA sttes (Dsttes), which re sets of NFA sttes, nd trnsition tle (Dtrn). strt 0 1 2 4 3 5 6 7 8 9 10 Trnsition tle: NFA sttes DFA stte A B C D E 1. Strt stte: AA = -closure(s 0 ) 2. Dtrn[AA, ] = -closure move(aa, ) 3. Dtrn[A, ] = -closure move(aa, )
4. Dtrn[BB, ] = -closure move(bb, ) 5. Dtrn[BB, ] = -closure move(bb, ) 6. Dtrn[CC, ] = -closure move(cc, ) 7. Dtrn[CC, ] = -closure move(cc, ) 8. Dtrn[DD, ] = -closure move(dd, ) 9. Dtrn[DD, ] = -closure move(dd, ) 10. Dtrn[EE, ] = -closure move(ee, ) 11. Dtrn[EE, ] = -closure move(ee, ) 12. Since there is no more new DFA stte, stop. Note tht, mong possile DFA sttes, we hve only 5 sttes.
E. Convert regulr expression to NFA The McNughton-Ymd-Thompson lgorithm. NFA is constructed, recursively, initilly from ech tomic expression in regulr expression. BASIS: NAF for regulr expression : NFA for regulr expression in Σ: INDUCTION: Let ss nd tt re prts of regulr expression, nd NN(ss) nd NN(tt) re their corresponding NFAs, recursively. ) Suppose rr = ss tt, then NN(rr) = NN(ss) NN(tt), tht is ) Suppose rr = ssss, then NN(rr) = NN(ss)NN(tt), tht is c) Suppose rr = ss, then NN(rr), tht is d) Suppose rr = (ss), then LL(rr) = LL(ss)
Exmple) Convert the following regulr expression to NFA. ( ) ( ) 1. Build prse tree from the regulr expression 2. Recursively uild NFA from prse tree.
F. Minimize numer of sttes of DFA Is the following two DFAs sme? strt C A B D E strt 0 1 2 3 Two utomt re the sme up to stte nmes if one cn e trnsformed into nother y doing nothing more thn chnging the nme of sttes. Two sttes AA nd CC re equivlent ecuse: String xx distinguishes stte ss from stte tt if exctly one of the sttes reched from ss nd tt y following the pth with lel ss is n ccepting stte. Stte ss is distinguishle from stte tt if there exists string xx tht distinguishes them. Q: Are AA nd BB distinguishle? If then why?
Minimize the numer of sttes of DFA. There is lwys unique minimum stte DFA for ny regulr expression. The following steps show how to determine the minimum stte DFA. 1. Strt with initil prtition Π with two groups: FF for ccepting sttes, nd SS FF for non-ccepting sttes. 2. Π new Π 3. Updte ΠΠ: For ech group GG Π, prtition GG into sugroups such tht two sttes ss nd tt re in the sme group if nd only if, Σ, sttes ss nd tt hve trnsition on to sttes in the sme groups of Π. 4. Replce GG in Π new y the set of ll sugroups formed. 5. Repet 2-4, until Π new = Π. Then Π finl = Π 6. Choose one stte in ech group of Π finl s the representtive for tht group. the new stte is the minimum-stte DFA.
G. How lexicl nlyzer works The min question in this prt is how lexicl-nlyzer generted y tools, such s Lex, work recognize lexeme nd tokens from regulr expression. ) Pttern mtching sed on NFA Let you hve three ptterns:,, +. Its NFA is: strt 1 0 3 2 4 5 6 7 8 You hve the following input nd lexemebegin t 0 th loction How generted nlyzer works?
) Pttern mtching sed on DFA Given NFA, its DFA cn e determined s follows: strt 0,1,3,7 2,4,7 7 strt 1 0 3 7 2 4 8 5 6 8 6,8 5,8 In DFA, 247, 8, 68, 58 re ccepting sttes ecuse they hve ccepting sttes 2, 6, 8 in NFA. 247 ccepts pttern: 8 ccepts pttern: 58 ccepts pttern: 68 ccepts pttern: You hve the following input nd lexemebegin t 0 th loction. How generted nlyzer works?