Lexicl Anlysis - Prt 4 Reducing DFA to Miniml DFA Input: DFA IN Assume DFA IN never gets stuck (dd ded stte if necessry) Output: DFA MIN An equivlent DFA with the minimum numer of sttes. Hrry H. Porter, 2005 1
Lexicl Anlysis - Prt 4 Reducing DFA to Miniml DFA Input: DFA IN Assume DFA IN never gets stuck (dd ded stte if necessry) Output: DFA MIN An equivlent DFA with the minimum numer of sttes. 1 Ded Stte 2 3 4 5, Hrry H. Porter, 2005 2
Lexicl Anlysis - Prt 4 Reducing DFA to Miniml DFA Input: DFA IN Assume DFA IN never gets stuck (dd ded stte if necessry) Output: DFA MIN An equivlent DFA with the minimum numer of sttes. 1 Ded Stte 2 3 4 5, Approch: Merge two sttes if the effectively do the sme thing. Do the sme thing? At EOF, is DFA IN in n ccepting stte or not? Hrry H. Porter, 2005 3
Lexicl Anlysis - Prt 4 Sufficiently Different Sttes Merge sttes, if t ll possile. Are two sttes sufficiently different... tht they cnnot e merged? Hrry H. Porter, 2005 4
Lexicl Anlysis - Prt 4 Sufficiently Different Sttes Merge sttes, if t ll possile. Are two sttes sufficiently different... tht they cnnot e merged? Stte s is distinguished from stte t y some string w iff: strting t s, given chrcters w, the DFA ends up ccepting,... ut strting t t, the DFA does not ccept. Hrry H. Porter, 2005 5
Lexicl Anlysis - Prt 4 Sufficiently Different Sttes Merge sttes, if t ll possile. Are two sttes sufficiently different... tht they cnnot e merged? Stte s is distinguished from stte t y some string w iff: strting t s, given chrcters w, the DFA ends up ccepting,... ut strting t t, the DFA does not ccept. Exmple: s c t c does not distinguish s nd t. But c distinguishes s nd t. Therefore, s nd t cnnot e merged. Hrry H. Porter, 2005 6
Lexicl Anlysis - Prt 4 Prtitioning Set A prtitioning of set......reks the set into non-overlpping susets. (The prtition reks the set into groups ) Exmple: S = {A, B, C, D, E, F, G} Π = {(A B) (C D E F) (G) } Π 2 = {(A) (B C) (D E F G) } Hrry H. Porter, 2005 7
Lexicl Anlysis - Prt 4 Prtitioning Set A prtitioning of set......reks the set into non-overlpping susets. (The prtition reks the set into groups ) Exmple: S = {A, B, C, D, E, F, G} Π = {(A B) (C D E F) (G) } Π 2 = {(A) (B C) (D E F G) } We cn refine prtition... Π i = { (A B C) (D E) (F G) } Π i+1 = { (A C) (B) (D) (E) (F G) } Note: { (...) (...) (...) } mens {{...}, {...}, {...} } Hrry H. Porter, 2005 8
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Consider the set of sttes. Initilly, we will prtition it into two groups... Finl Sttes All Other Sttes c e d Hrry H. Porter, 2005 9
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Consider the set of sttes. Initilly, we will prtition it into two groups... Finl Sttes All Other Sttes c e d (A B D) (C E) Hrry H. Porter, 2005 10
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Consider the set of sttes. Initilly, we will prtition it into two groups... Finl Sttes All Other Sttes c e d (A B D) (C E) Repetedly refine the prtioning. Two sttes must e plced in different groups... if they cn e distinguished Repet until no group contins sttes tht cn e distinguished. Ech group in the prtitioning ecomes one stte in newly constructed DFA DFA MIN = The miniml DFA Hrry H. Porter, 2005 11
Lexicl Anlysis - Prt 4 How to Refine Prtitioning? Π i = { ( A B D ) ( C E ) } Consider one group... (A B D) Look t output edges on some symol (e.g., x ) P 1 P 2 x x c d x e Hrry H. Porter, 2005 12
Lexicl Anlysis - Prt 4 How to Refine Prtitioning? Π i = { ( A B D ) ( C E ) } Consider one group... (A B D) Look t output edges on some symol (e.g., x ) P 1 P 2 x x c d On x, ll sttes in P 1 go to sttes elonging to the sme group. x e Hrry H. Porter, 2005 13
Lexicl Anlysis - Prt 4 How to Refine Prtitioning? Π i = { ( A B D ) ( C E ) } Consider one group... (A B D) Look t output edges on some symol (e.g., x ) P 1 P 2 x x c d On x, ll sttes in P 1 go to sttes elonging to the sme group. x e y c y e Now consider nother symol (e.g., y ) y d Hrry H. Porter, 2005 14
Lexicl Anlysis - Prt 4 How to Refine Prtitioning? Π i = { ( A B D ) ( C E ) } Consider one group... (A B D) Look t output edges on some symol (e.g., x ) P 1 P 2 x x c d On x, ll sttes in P 1 go to sttes elonging to the sme group. x e y c y e Now consider nother symol (e.g., y ) D is distinguished from A nd B! y d Hrry H. Porter, 2005 15
Lexicl Anlysis - Prt 4 How to Refine Prtitioning? Π i = { ( A B D ) ( C E ) } Consider one group... (A B D) Look t output edges on some symol (e.g., x ) P 1 P 2 x x c d On x, ll sttes in P 1 go to sttes elonging to the sme group. x e y c y y d e Now consider nother symol (e.g., y ) D is distinguished from A nd B! So refine the prtition! Π i+1 = { ( A B ) ( D ) ( C E ) } P 3 P 4 P 2 Hrry H. Porter, 2005 16
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) C A B D E Hrry H. Porter, 2005 17
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) A C B D E Consider (E) Hrry H. Porter, 2005 18
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Consider A C B D E Consider (E) Hrry H. Porter, 2005 19
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? Consider A C B D E Consider (E) Hrry H. Porter, 2005 20
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? Consider (E) A C B D E Hrry H. Porter, 2005 21
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) A C B D E Hrry H. Porter, 2005 22
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. A C B D E Hrry H. Porter, 2005 23
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) A C B D E Hrry H. Porter, 2005 24
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? Consider A C B D E Hrry H. Porter, 2005 25
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? No Consider Brek prt? A C B D E Hrry H. Porter, 2005 26
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? No Consider Brek prt? (A C) (B) A C B D E Hrry H. Porter, 2005 27
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? No Consider Brek prt? (A C) (B) New Prtitioning: Π 3 = (A C) (B) (D) (E) Consider Brek prt? Consider Brek prt? A C B D E Hrry H. Porter, 2005 28
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? No Consider Brek prt? (A C) (B) New Prtitioning: Π 3 = (A C) (B) (D) (E) Consider Brek prt? No Consider Brek prt? A C B D E Hrry H. Porter, 2005 29
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? No Consider Brek prt? (A C) (B) New Prtitioning: Π 3 = (A C) (B) (D) (E) Consider Brek prt? No Consider Brek prt? No A C B D E Hrry H. Porter, 2005 30
Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) Consider (A B C D) Consider Brek prt? No Consider Brek prt? (A B C) (D) Consider (E) Not possile to rek prt. New Prtitioning: Π 2 = (A B C) (D) (E) Consider Brek prt? No Consider Brek prt? (A C) (B) New Prtitioning: Π 3 = (A C) (B) (D) (E) Consider Brek prt? No Consider Brek prt? No A C B DFA MIN D AC B D E E Hrry H. Porter, 2005 31
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Hrry H. Porter, 2005 32
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte Hrry H. Porter, 2005 33
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte A C c B D c Hrry H. Porter, 2005 34
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte Choose one stte in ech group (throw ll other sttes wy) Preserve the edges out of the chosen stte A C c B D c Hrry H. Porter, 2005 35
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte Choose one stte in ech group (throw ll other sttes wy) Preserve the edges out of the chosen stte A D c Hrry H. Porter, 2005 36
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte Choose one stte in ech group (throw ll other sttes wy) Preserve the edges out of the chosen stte A D c A D c Hrry H. Porter, 2005 37
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte Choose one stte in ech group (throw ll other sttes wy) Preserve the edges out of the chosen stte Del with strt stte nd finl sttes A D c A D c Hrry H. Porter, 2005 38
Lexicl Anlysis - Prt 4 Hopcroft s Algorithm Add ded stte nd trnsitions to it if necessry. (Now, every stte hs n outgoing edge on every symol.) Π = initil prtitioning loop Π NEW = Refine(Π) if (Π NEW = Π) then rek Π = Π NEW endloop Construct DFA MIN Ech group in Π ecomes stte Choose one stte in ech group (throw ll other sttes wy) Preserve the edges out of the chosen stte Del with strt stte nd finl sttes If desired... Remove ded stte Remove ny stte unrechle from the strt stte A A D D c c Hrry H. Porter, 2005 39
Lexicl Anlysis - Prt 4 Π NEW = Refine(Π) Π NEW = {} for ech group G in Π do Exmple: Π = (A B C E) (D F) Brek G into su-groups (A B C E) (A C) (B E) s follows: Put S nd T into different sugroups if... For ny symol Σ, S nd T go to sttes in two different groups in Π A B x x D C Must split A nd B into different groups Add the su-groups to Π NEW endfor return Π NEW Π NEW = { } Π NEW = { (A C) (B E) } Π NEW = { (A C) (B E) (D F) } Hrry H. Porter, 2005 40
Lexicl Anlysis - Prt 4 Summrizing... Hrry H. Porter, 2005 41
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Hrry H. Porter, 2005 42
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Hrry H. Porter, 2005 43
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Hrry H. Porter, 2005 44
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Algorithm: NFA DFA Hrry H. Porter, 2005 45
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Algorithm: NFA DFA Algorithm: DFA Miniml DFA Hrry H. Porter, 2005 46
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Algorithm: NFA DFA Algorithm: DFA Miniml DFA Algorithm for Simulting DFA Hrry H. Porter, 2005 47
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Algorithm: NFA DFA Algorithm: DFA Miniml DFA Algorithm for Simulting DFA Fst: Get Next Chr Evlute Move Function e.g., Arry Lookup Chnge Stte Vrile Test for Accepting Stte Test for EOF Repet Hrry H. Porter, 2005 48
Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Algorithm: NFA DFA Algorithm: DFA Miniml DFA Algorithm for Simulting DFA Fst: Get Next Chr Evlute Move Function e.g., Arry Lookup Chnge Stte Vrile Test for Accepting Stte Test for EOF Repet Scnner Genertors Crete n efficient Lexer from regulr expressions! Hrry H. Porter, 2005 49
Lexicl Anlysis - Prt 4 Scnner Genertor: LEX Input: r 1 { ction 1 } r 2 { ction 2 }... r N { ction N } Requirements: Choose the lrgest lexeme tht mtches. If more thn one r i mtches, choose the first one. DFA Simultor (C-code) Trnsition Tles (initilized rrys) lex-egin-ptr forwrd-ptr Input Buffers Cnned code dded y lex tool Computed y lex tool Hrry H. Porter, 2005 50
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Hrry H. Porter, 2005 51
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ 0 ε ε ε 1 3 7 2 4 5 8 Pttern-1 Pttern-3 6 Pttern-2 Hrry H. Porter, 2005 52
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ 0 ε ε ε 1 3 7 2 4 5 8 Pttern-1 Pttern-3 6 Pttern-2 Exmple Input: c... Strt: { 0, 1, 3, 7 } Input: { 2, 4, 7 } Input: { 7 } Input: { 8 } Input: c { } Mtch! Pttern: 1 Length: 1 Mtch! Pttern: 3 Length: 3 Done! Identify the lst mtch. Execute the corresponding ction & djust pointers Hrry H. Porter, 2005 53
Lexicl Anlysis - Prt 4 Find the NFA for r 1 r 2... r N Convert to DFA. Approch Ech stte of the DFA corresponds to set of NFA sttes. A stte is finl if ny NFA stte in it ws finl stte. If severl, choose the lowest numered pttern to e the one ccepted. During simultion, keep following edges until you get stuck. As the scnning proceeds... Every time you enter finl stte... Rememer: The current vlue of uffer pointers Which pttern ws recognized Upon termintion... Use tht informtion to... Adjust the uffer pointers Execute the desired ction Hrry H. Porter, 2005 54
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Exmple Hrry H. Porter, 2005 55
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε 1 3 7 2 4 5 8 *+ 6 Hrry H. Porter, 2005 56
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε 1 3 7 2 4 5 8 *+ 6 Construct Miniml DFA 2,4,7 5,8 0,1,3,7 8 7 6,8 Hrry H. Porter, 2005 57
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε 1 3 7 2 4 5 8 *+ 6 Construct Miniml DFA Attch Actions 0,1,3,7 *+ 8 2,4,7 7 5,8 6,8 *+ *+ Accept only first pttern Hrry H. Porter, 2005 58
Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε 1 3 7 2 4 5 8 *+ 6 Construct Miniml DFA Attch Actions Exmple Strings: 0,1,3,7 *+ 8 2,4,7 7 5,8 6,8 *+ *+ Accept only first pttern Hrry H. Porter, 2005 59
Lexicl Anlysis - Prt 4 Oldest, most well-known For Unix/C Environment In UNIX: %lex lex.l %cc lex.yy.c The Lex Tool File: lex.l Lex Tool Contins severl regulr expressions File: lex.yy.c A progrm in C... Redy to compile nd link with Prser (e.g., YACC output) Hrry H. Porter, 2005 60
Lexicl Anlysis - Prt 4 Oldest, most well-known For Unix/C Environment In UNIX: %lex lex.l %cc lex.yy.c The Lex Tool File: lex.l Lex Tool Contins severl regulr expressions Input File Formt: File: lex.yy.c %{ }% %% %%...Any C Code......Regulr Definitions......Regulr Expressions with Actions......Any C Code... A progrm in C... Redy to compile nd link with Prser (e.g., YACC output) Hrry H. Porter, 2005 61
Lexicl Anlysis - Prt 4 Regulr Expressions in Lex c Conctention; Most chrcters stnd for themselves Met Chrters: Usul menings * Exmple: ( )*c* () + One or more, e.g., +c? Optionl, e.g.,?c [x-y] Chrcter clsses, e.g., [-z][-za-z0-9]* [^x-y] Anything ut [x-y] \x The usul escpe sequences, e.g., \n. Any chrcter except \n ^ Beginning of line $ End of line "..." To use the met chrcters literlly, Exmple: PCAT comments: "(*".*"*)" {...} Defined nmes, e.g., {letter} / Look-hed Exmple: /cd (Mtches, ut only when followed y cd) Hrry H. Porter, 2005 62
Lexicl Anlysis - Prt 4 Look-Ahed Opertor, / /cd Mtches, ut only if followed y cd. Hrry H. Porter, 2005 63
Lexicl Anlysis - Prt 4 Look-Ahed Opertor, / /cd Mtches, ut only if followed y cd. Add specil ε edge for / ε c d Hrry H. Porter, 2005 64
Lexicl Anlysis - Prt 4 Look-Ahed Opertor, / /cd Mtches, ut only if followed y cd. Add specil ε edge for / ε c d Mrk the following stte to mke note of... The pttern in question The current vlue of the uffer pointers...whenever this stte is encountered during scnning. / Encountered Sve uffer pointers Hrry H. Porter, 2005 65
Lexicl Anlysis - Prt 4 Look-Ahed Opertor, / /cd Mtches, ut only if followed y cd. Add specil ε edge for / ε c d Mrk the following stte to mke note of... The pttern in question The current vlue of the uffer pointers...whenever this stte is encountered during scnning. / Encountered Sve uffer pointers When pttern is finlly mtched, check these notes. If we pssed through / stte for the pttern ccepted, Use the stored uffer positions, insted of the finl positions to descrie the lexeme mtched. Hrry H. Porter, 2005 66
Lexicl Anlysis - Prt 4 Lex: Input File Formt %{...Any C Code... }%...Regulr Definitions... %%...Regulr Expressions with Actions... %%...Any C Code... Hrry H. Porter, 2005 67
Lexicl Anlysis - Prt 4 %{ }% %% %%...Any C Code... #define ID 13 #define NUM 14 #define PLUS 15 #define MINUS 16... #define WHILE 37 #define IF 38......Regulr Definitions... Lex: Input File Formt...Regulr Expressions with Actions......Any C Code...... int lookup (chr * p) {...} int enter (chr * p, int i) {...}... Any C code; Copied without chnges to eginning of the output file Any C code; dded to end of file (typiclly, uxillry support routines) Hrry H. Porter, 2005 68
Lexicl Anlysis - Prt 4 %{ }% %% %% Defined Nmes...Any C Code... Lex: Input File Formt...Regulr Definitions... Defined nmes cn delim [ \t\n] e used in regulr expressions white {delim}+ letter [-za-z] digit [0-9] id {letter}({letter} {digit})* num {digit}+(\.{digit}+)?...regulr Expressions with Actions......Any C Code... Blnk: Every chrcter is Itself literlly Hrry H. Porter, 2005 69
Lexicl Anlysis - Prt 4 %{ }% %%...Any C Code......Regulr Definitions... Lex: Input File Formt...Regulr Expressions with Actions... "+" {return PLUS;} "-" {return MINUS;}... while if... {white} {}... {return WHILE;} {return IF;} Regulr expressions {num} {yylvl =...; return NUM;} {id} {yylvl =...lookup(...)...; return ID;} %%...Any C Code... yylvl is where token ttriute info is stored. Any C code. Include return to give the token to prser. No return mens do nothing. (This token is recognized ut not returned to prser) You my use these vriles to ccess the lexeme: chr * yytext; int yyleng; Hrry H. Porter, 2005 70