Reducing a DFA to a Minimal DFA

Size: px
Start display at page:

Download "Reducing a DFA to a Minimal DFA"

Transcription

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. Hrry H. Porter,

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 , Hrry H. Porter,

3 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 , 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,

4 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,

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. Hrry H. Porter,

6 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,

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) } Hrry H. Porter,

8 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,

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 Hrry H. Porter,

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) Hrry H. Porter,

11 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,

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 x e Hrry H. Porter,

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 Hrry H. Porter,

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 ) y d Hrry H. Porter,

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 e Now consider nother symol (e.g., y ) D is distinguished from A nd B! y d Hrry H. Porter,

16 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,

17 Lexicl Anlysis - Prt 4 Exmple DFA IN Initil Prtitioning: Π 1 = (A B C D) (E) C A B D E Hrry H. Porter,

18 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,

19 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,

20 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,

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? Consider (E) A C B D E Hrry H. Porter,

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) A C B D E Hrry H. Porter,

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. A C B D E Hrry H. Porter,

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) A C B D E Hrry H. Porter,

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? Consider A C B D E Hrry H. Porter,

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 D E Hrry H. Porter,

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) A C B D E Hrry H. Porter,

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? Consider Brek prt? A C B D E Hrry H. Porter,

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? A C B D E Hrry H. Porter,

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 D E Hrry H. Porter,

31 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,

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 Hrry H. Porter,

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 Hrry H. Porter,

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 A C c B D c Hrry H. Porter,

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 C c B D c Hrry H. Porter,

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 Hrry H. Porter,

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 A D c A D c Hrry H. Porter,

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 A D c A D c Hrry H. Porter,

39 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,

40 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,

41 Lexicl Anlysis - Prt 4 Summrizing... Hrry H. Porter,

42 Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Hrry H. Porter,

43 Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Hrry H. Porter,

44 Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Hrry H. Porter,

45 Lexicl Anlysis - Prt 4 Summrizing... Regulr Expressions to Descrie Tokens Algorithm: Regulr Expression NFA Algorithm for Simulting NFA Algorithm: NFA DFA Hrry H. Porter,

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 Hrry H. Porter,

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 Hrry H. Porter,

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 Hrry H. Porter,

49 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,

50 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,

51 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Hrry H. Porter,

52 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ 0 ε ε ε Pttern-1 Pttern-3 6 Pttern-2 Hrry H. Porter,

53 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ 0 ε ε ε 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,

54 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,

55 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Exmple Hrry H. Porter,

56 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε *+ 6 Hrry H. Porter,

57 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε *+ 6 Construct Miniml DFA 2,4,7 5,8 0,1,3, ,8 Hrry H. Porter,

58 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε *+ 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,

59 Lexicl Anlysis - Prt 4 Input: { Action-1 } { Action-2 } *+ { Action-3 } Crete NFA: *+ Exmple 0 ε ε ε *+ 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,

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 File: lex.yy.c A progrm in C... Redy to compile nd link with Prser (e.g., YACC output) Hrry H. Porter,

61 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,

62 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,

63 Lexicl Anlysis - Prt 4 Look-Ahed Opertor, / /cd Mtches, ut only if followed y cd. Hrry H. Porter,

64 Lexicl Anlysis - Prt 4 Look-Ahed Opertor, / /cd Mtches, ut only if followed y cd. Add specil ε edge for / ε c d Hrry H. Porter,

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 Hrry H. Porter,

66 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,

67 Lexicl Anlysis - Prt 4 Lex: Input File Formt %{...Any C Code... }%...Regulr Definitions... %%...Regulr Expressions with Actions... %%...Any C Code... Hrry H. Porter,

68 Lexicl Anlysis - Prt 4 %{ }% %% %%...Any C Code... #define ID 13 #define NUM 14 #define PLUS 15 #define MINUS #define WHILE 37 #define IF 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,

69 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,

70 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,

Dr. D.M. Akbar Hussain

Dr. D.M. Akbar Hussain Dr. D.M. Akr Hussin Lexicl Anlysis. Bsic Ide: Red the source code nd generte tokens, it is similr wht humns will do to red in; just tking on the input nd reking it down in pieces. Ech token is sequence

More information

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

In the last lecture, we discussed how valid tokens may be specified by regular expressions. LECTURE 5 Scnning SYNTAX ANALYSIS We know from our previous lectures tht the process of verifying the syntx of the progrm is performed in two stges: Scnning: Identifying nd verifying tokens in progrm.

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5 CS321 Lnguges nd Compiler Design I Winter 2012 Lecture 5 1 FINITE AUTOMATA A non-deterministic finite utomton (NFA) consists of: An input lphet Σ, e.g. Σ =,. A set of sttes S, e.g. S = {1, 3, 5, 7, 11,

More information

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08 CS412/413 Introduction to Compilers Tim Teitelum Lecture 4: Lexicl Anlyzers 28 Jn 08 Outline DFA stte minimiztion Lexicl nlyzers Automting lexicl nlysis Jlex lexicl nlyzer genertor CS 412/413 Spring 2008

More information

Fig.25: the Role of LEX

Fig.25: the Role of LEX The Lnguge for Specifying Lexicl Anlyzer We shll now study how to uild lexicl nlyzer from specifiction of tokens in the form of list of regulr expressions The discussion centers round the design of n existing

More information

Lexical Analysis: Constructing a Scanner from Regular Expressions

Lexical Analysis: Constructing a Scanner from Regular Expressions Lexicl Anlysis: Constructing Scnner from Regulr Expressions Gol Show how to construct FA to recognize ny RE This Lecture Convert RE to n nondeterministic finite utomton (NFA) Use Thompson s construction

More information

Topic 2: Lexing and Flexing

Topic 2: Lexing and Flexing Topic 2: Lexing nd Flexing COS 320 Compiling Techniques Princeton University Spring 2016 Lennrt Beringer 1 2 The Compiler Lexicl Anlysis Gol: rek strem of ASCII chrcters (source/input) into sequence of

More information

Definition of Regular Expression

Definition of Regular Expression Definition of Regulr Expression After the definition of the string nd lnguges, we re redy to descrie regulr expressions, the nottion we shll use to define the clss of lnguges known s regulr sets. Recll

More information

Lexical analysis, scanners. Construction of a scanner

Lexical analysis, scanners. Construction of a scanner Lexicl nlysis scnners (NB. Pges 4-5 re for those who need to refresh their knowledge of DFAs nd NFAs. These re not presented during the lectures) Construction of scnner Tools: stte utomt nd trnsition digrms.

More information

Lexical Analysis and Lexical Analyzer Generators

Lexical Analysis and Lexical Analyzer Generators 1 Lexicl Anlysis nd Lexicl Anlyzer Genertors Chpter 3 COP5621 Compiler Construction Copyright Roert vn Engelen, Florid Stte University, 2007-2009 2 The Reson Why Lexicl Anlysis is Seprte Phse Simplifies

More information

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Lexicl Anlysis Amith Snyl (www.cse.iit.c.in/ s) Deprtment of Computer Science nd Engineering, Indin Institute of Technology, Bomy Septemer 27 College of Engineering, Pune Lexicl Anlysis: 2/6 Recp The input

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Progrmming Lnguges h"p://www.di.unipi.it/~ndre/did2c/plp- 14/ Prof. Andre Corrdini Deprtment of Computer Science, Pis Lesson 5! Gener;on of Lexicl Anlyzers Creting Lexicl Anlyzer with Lex

More information

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) * Pln for Tody nd Beginning Next week Interpreter nd Compiler Structure, or Softwre Architecture Overview of Progrmming Assignments The MeggyJv compiler we will e uilding. Regulr Expressions Finite Stte

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy Recognition of Tokens if expressions nd reltionl opertors if è if then è then else è else relop

More information

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata CS 432 Fll 2017 Mike Lm, Professor (c)* Regulr Expressions nd Finite Automt Compiltion Current focus "Bck end" Source code Tokens Syntx tree Mchine code chr dt[20]; int min() { flot x = 42.0; return 7;

More information

CSCE 531, Spring 2017, Midterm Exam Answer Key

CSCE 531, Spring 2017, Midterm Exam Answer Key CCE 531, pring 2017, Midterm Exm Answer Key 1. (15 points) Using the method descried in the ook or in clss, convert the following regulr expression into n equivlent (nondeterministic) finite utomton: (

More information

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program: Lexicl Anlysis Red source progrm nd produce list of tokens ( liner nlysis) source progrm The lexicl structure is specified using regulr expressions Other secondry tsks: (1) get rid of white spces (e.g.,

More information

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona CSc 453 Compilers nd Systems Softwre 4 : Lexicl Anlysis II Deprtment of Computer Science University of Arizon collerg@gmil.com Copyright c 2009 Christin Collerg Implementing Automt NFAs nd DFAs cn e hrd-coded

More information

CMPSC 470: Compiler Construction

CMPSC 470: Compiler Construction CMPSC 47: Compiler Construction Plese complete the following: Midterm (Type A) Nme Instruction: Mke sure you hve ll pges including this cover nd lnk pge t the end. Answer ech question in the spce provided.

More information

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011 CSCI 3130: Forml Lnguges nd utomt Theory Lecture 12 The Chinese University of Hong Kong, Fll 2011 ndrej Bogdnov In progrmming lnguges, uilding prse trees is significnt tsk ecuse prse trees tell us the

More information

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis CS143 Hndout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexicl Anlysis In this first written ssignment, you'll get the chnce to ply round with the vrious constructions tht come up when doing lexicl

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy RecogniNon of Tokens if expressions nd relnonl opertors if è if then è then else è else relop è

More information

CS 430 Spring Mike Lam, Professor. Parsing

CS 430 Spring Mike Lam, Professor. Parsing CS 430 Spring 2015 Mike Lm, Professor Prsing Syntx Anlysis We cn now formlly descrie lnguge's syntx Using regulr expressions nd BNF grmmrs How does tht help us? Syntx Anlysis We cn now formlly descrie

More information

Assignment 4. Due 09/18/17

Assignment 4. Due 09/18/17 Assignment 4. ue 09/18/17 1. ). Write regulr expressions tht define the strings recognized by the following finite utomt: b d b b b c c b) Write FA tht recognizes the tokens defined by the following regulr

More information

CMPT 379 Compilers. Lexical Analysis

CMPT 379 Compilers. Lexical Analysis CMPT 379 Compilers Anoop Srkr http://www.cs.sfu.c/~noop 9//7 Lexicl Anlysis Also clled scnning, tke input progrm string nd convert into tokens Exmple: T_DOUBLE ( doule ) T_IDENT ( f ) T_OP ( = ) doule

More information

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona Implementing utomt Sc 5 ompilers nd Systems Softwre : Lexicl nlysis II Deprtment of omputer Science University of rizon collerg@gmil.com opyright c 009 hristin ollerg NFs nd DFs cn e hrd-coded using this

More information

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an Scnner Termintion A scnner reds input chrcters nd prtitions them into tokens. Wht hppens when the end of the input file is reched? It my be useful to crete n Eof pseudo-chrcter when this occurs. In Jv,

More information

Compilers Spring 2013 PRACTICE Midterm Exam

Compilers Spring 2013 PRACTICE Midterm Exam Compilers Spring 2013 PRACTICE Midterm Exm This is full length prctice midterm exm. If you wnt to tke it t exm pce, give yourself 7 minutes to tke the entire test. Just like the rel exm, ech question hs

More information

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1 Deterministic Finite Automt And Regulr Lnguges Fll 2018 Costs Busch - RPI 1 Deterministic Finite Automton (DFA) Input Tpe String Finite Automton Output Accept or Reject Fll 2018 Costs Busch - RPI 2 Trnsition

More information

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex Long Quiz2 45mins Nme: Personl Numer: Prolem. (20pts) Here is n Tle of Perl Regulr Ex Chrcter Description. single chrcter \s whitespce chrcter (spce, t, newline) \S non-whitespce chrcter \d digit (0-9)

More information

Scanner Termination. Multi Character Lookahead

Scanner Termination. Multi Character Lookahead If d.doublevlue() represents vlid integer, (int) d.doublevlue() will crete the pproprite integer vlue. If string representtion of n integer begins with ~ we cn strip the ~, convert to double nd then negte

More information

CSE 401 Midterm Exam 11/5/10 Sample Solution

CSE 401 Midterm Exam 11/5/10 Sample Solution Question 1. egulr expressions (20 points) In the Ad Progrmming lnguge n integer constnt contins one or more digits, but it my lso contin embedded underscores. Any underscores must be preceded nd followed

More information

Compilation

Compilation Compiltion 0368-3133 Lecture 2: Lexicl Anlysis Nom Rinetzky 1 2 Lexicl Anlysis Modern Compiler Design: Chpter 2.1 3 Conceptul Structure of Compiler Compiler Source text txt Frontend Semntic Representtion

More information

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015 Finite Automt Lecture 4 Sections 3.6-3.7 Ro T. Koether Hmpden-Sydney College Wed, Jn 21, 2015 Ro T. Koether (Hmpden-Sydney College) Finite Automt Wed, Jn 21, 2015 1 / 23 1 Nondeterministic Finite Automt

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 3: Lexer genertors Viktor Leijon Slides lrgely y John Nordlnder with mteril generously provided y Mrk P. Jones. 1 Recp: Hndwritten Lexers: Don t require sophisticted

More information

CS201 Discussion 10 DRAWTREE + TRIES

CS201 Discussion 10 DRAWTREE + TRIES CS201 Discussion 10 DRAWTREE + TRIES DrwTree First instinct: recursion As very generic structure, we could tckle this problem s follows: drw(): Find the root drw(root) drw(root): Write the line for the

More information

TO REGULAR EXPRESSIONS

TO REGULAR EXPRESSIONS Suject :- Computer Science Course Nme :- Theory Of Computtion DA TO REGULAR EXPRESSIONS Report Sumitted y:- Ajy Singh Meen 07000505 jysmeen@cse.iit.c.in BASIC DEINITIONS DA:- A finite stte mchine where

More information

COMP 423 lecture 11 Jan. 28, 2008

COMP 423 lecture 11 Jan. 28, 2008 COMP 423 lecture 11 Jn. 28, 2008 Up to now, we hve looked t how some symols in n lphet occur more frequently thn others nd how we cn sve its y using code such tht the codewords for more frequently occuring

More information

Lexical Analysis. Role, Specification & Recognition Tool: LEX Construction: - RE to NFA to DFA to min-state DFA - RE to DFA

Lexical Analysis. Role, Specification & Recognition Tool: LEX Construction: - RE to NFA to DFA to min-state DFA - RE to DFA Lexicl Anlysis Role, Specifiction & Recognition Tool: LEX Construction: - RE to NFA to DFA to min-stte DFA - RE to DFA Conducting Lexicl Anlysis Techniques for specifying nd implementing lexicl nlyzers

More information

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the LR() nlysis Drwcks of LR(). Look-hed symols s eplined efore, concerning LR(), it is possile to consult the net set to determine, in the reduction sttes, for which symols it would e possile to perform reductions.

More information

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2014 Dec 11 th /13 th Finl Exm Nme: Note: in ll questions, the specil symol ɛ (epsilon) is used to indicte the empty string. Question 1. [5 points] Consider the following regulr expression;

More information

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night Should e done L hours nd Office hours Sign up for the miling list t, strting to send importnt info to list http://groups.google.com/group/cs453-spring-2011 Red Ch 1 nd skim Ch 2 through 2.6, red 3.3 nd

More information

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University Fll 2014-2015 Compiler Principles Lecture 1: Lexicl Anlysis Romn Mnevich Ben-Gurion University Agend Understnd role of lexicl nlysis in compiler Lexicl nlysis theory Implementing professionl scnner vi

More information

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class LEX5: Regexps to NFA Lexicl Anlysis CMPT 379: Compilers Instructor: Anoop Srkr noopsrkr.github.io/compilers-clss Building Lexicl Anlyzer Token POern POern Regulr Expression Regulr Expression NFA NFA DFA

More information

Discussion 1 Recap. COP4600 Discussion 2 OS concepts, System call, and Assignment 1. Questions. Questions. Outline. Outline 10/24/2010

Discussion 1 Recap. COP4600 Discussion 2 OS concepts, System call, and Assignment 1. Questions. Questions. Outline. Outline 10/24/2010 COP4600 Discussion 2 OS concepts, System cll, nd Assignment 1 TA: Hufeng Jin hj0@cise.ufl.edu Discussion 1 Recp Introduction to C C Bsic Types (chr, int, long, flot, doule, ) C Preprocessors (#include,

More information

CS 241 Week 4 Tutorial Solutions

CS 241 Week 4 Tutorial Solutions CS 4 Week 4 Tutoril Solutions Writing n Assemler, Prt & Regulr Lnguges Prt Winter 8 Assemling instrutions utomtilly. slt $d, $s, $t. Solution: $d, $s, nd $t ll fit in -it signed integers sine they re 5-it

More information

Lecture T1: Pattern Matching

Lecture T1: Pattern Matching Introduction to Theoreticl CS Lecture T: Pttern Mtchin Two fundmentl questions. Wht cn computer do? Wht cn computer do with limited resources? Generl pproch. Don t tlk out specific mchines or prolems.

More information

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7. CS 241 Fll 2017 Midterm Review Solutions Octoer 24, 2017 Contents 1 Bits nd Bytes 1 2 MIPS Assemly Lnguge Progrmming 2 3 MIPS Assemler 6 4 Regulr Lnguges 7 5 Scnning 9 1 Bits nd Bytes 1. Give two s complement

More information

Suffix trees, suffix arrays, BWT

Suffix trees, suffix arrays, BWT ALGORITHMES POUR LA BIO-INFORMATIQUE ET LA VISUALISATION COURS 3 Rluc Uricru Suffix trees, suffix rrys, BWT Bsed on: Suffix trees nd suffix rrys presenttion y Him Kpln Suffix trees course y Pco Gomez Liner-Time

More information

ECE 468/573 Midterm 1 September 28, 2012

ECE 468/573 Midterm 1 September 28, 2012 ECE 468/573 Midterm 1 September 28, 2012 Nme:! Purdue emil:! Plese sign the following: I ffirm tht the nswers given on this test re mine nd mine lone. I did not receive help from ny person or mteril (other

More information

Information Retrieval and Organisation

Information Retrieval and Organisation Informtion Retrievl nd Orgnistion Suffix Trees dpted from http://www.mth.tu.c.il/~himk/seminr02/suffixtrees.ppt Dell Zhng Birkeck, University of London Trie A tree representing set of strings { } eef d

More information

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table TDDD55 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing, Prt 2 Constructing Prse Tles Prse tle construction Grmmr conflict hndling Ctegories of LR Grmmrs nd Prsers Peter Fritzson, Christoph

More information

The dictionary model allows several consecutive symbols, called phrases

The dictionary model allows several consecutive symbols, called phrases A dptive Huffmn nd rithmetic methods re universl in the sense tht the encoder cn dpt to the sttistics of the source. But, dpttion is computtionlly expensive, prticulrly when k-th order Mrkov pproximtion

More information

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev Fll 2016-2017 Compiler Principles Lecture 1: Lexicl Anlysis Romn Mnevich Ben-Gurion University of the Negev Agend Understnd role of lexicl nlysis in compiler Regulr lnguges reminder Lexicl nlysis lgorithms

More information

Slides for Data Mining by I. H. Witten and E. Frank

Slides for Data Mining by I. H. Witten and E. Frank Slides for Dt Mining y I. H. Witten nd E. Frnk Simplicity first Simple lgorithms often work very well! There re mny kinds of simple structure, eg: One ttriute does ll the work All ttriutes contriute eqully

More information

What are suffix trees?

What are suffix trees? Suffix Trees 1 Wht re suffix trees? Allow lgorithm designers to store very lrge mount of informtion out strings while still keeping within liner spce Allow users to serch for new strings in the originl

More information

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or Opertor Precedence Most progrmming lnguges hve opertor precedence rules tht stte the order in which opertors re pplied (in the sence of explicit prentheses). Thus in C nd Jv nd CSX, +*c mens compute *c,

More information

Stack. A list whose end points are pointed by top and bottom

Stack. A list whose end points are pointed by top and bottom 4. Stck Stck A list whose end points re pointed by top nd bottom Insertion nd deletion tke plce t the top (cf: Wht is the difference between Stck nd Arry?) Bottom is constnt, but top grows nd shrinks!

More information

Network Interconnection: Bridging CS 571 Fall Kenneth L. Calvert All rights reserved

Network Interconnection: Bridging CS 571 Fall Kenneth L. Calvert All rights reserved Network Interconnection: Bridging CS 57 Fll 6 6 Kenneth L. Clvert All rights reserved The Prolem We know how to uild (rodcst) LANs Wnt to connect severl LANs together to overcome scling limits Recll: speed

More information

2014 Haskell January Test Regular Expressions and Finite Automata

2014 Haskell January Test Regular Expressions and Finite Automata 0 Hskell Jnury Test Regulr Expressions nd Finite Automt This test comprises four prts nd the mximum mrk is 5. Prts I, II nd III re worth 3 of the 5 mrks vilble. The 0 Hskell Progrmming Prize will be wrded

More information

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing Motivting Exmple Lexicl nd yntx Anlysis (2) In Text: Chpter 4 Consider the grmmr -> cad A -> b Input string: w = cd How to build prse tree top-down? 2 Initilly crete tree contining single node (the strt

More information

COS 333: Advanced Programming Techniques

COS 333: Advanced Programming Techniques COS 333: Advnced Progrmming Techniques Brin Kernighn wk@cs, www.cs.princeton.edu/~wk 311 CS Building 609-258-2089 (ut emil is lwys etter) TA's: Junwen Li, li@cs, CS 217,258-0451 Yong Wng,yongwng@cs, CS

More information

Midterm I Solutions CS164, Spring 2006

Midterm I Solutions CS164, Spring 2006 Midterm I Solutions CS164, Spring 2006 Februry 23, 2006 Plese red ll instructions (including these) crefully. Write your nme, login, SID, nd circle the section time. There re 8 pges in this exm nd 4 questions,

More information

CS 321 Programming Languages and Compilers. Bottom Up Parsing

CS 321 Programming Languages and Compilers. Bottom Up Parsing CS 321 Progrmming nguges nd Compilers Bottom Up Prsing Bottom-up Prsing: Shift-reduce prsing Grmmr H: fi ; fi b Input: ;;b hs prse tree ; ; b 2 Dt for Shift-reduce Prser Input string: sequence of tokens

More information

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv Compression Outline 15-853:Algorithms in the Rel World Dt Compression III Introduction: Lossy vs. Lossless, Benchmrks, Informtion Theory: Entropy, etc. Proility Coding: Huffmn + Arithmetic Coding Applictions

More information

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing Undergrdute Compilers Review nd Intro to MJC Announcements Miling list is in full swing Tody Some thoughts on grd school Finish prsing Semntic nlysis Visitor pttern for bstrct syntx trees Some Thoughts

More information

2 Computing all Intersections of a Set of Segments Line Segment Intersection

2 Computing all Intersections of a Set of Segments Line Segment Intersection 15-451/651: Design & Anlysis of Algorithms Novemer 14, 2016 Lecture #21 Sweep-Line nd Segment Intersection lst chnged: Novemer 8, 2017 1 Preliminries The sweep-line prdigm is very powerful lgorithmic design

More information

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2016 Sep 29th Exm 1 Nme: Note: in ll questions, the speil symol ɛ (epsilon) is used to indite the empty string. Question 1. [10 points] Speify regulr expression tht genertes the lnguge over

More information

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results 1 L 1 - Counter A project is collection mechnism for n HDL design under specifiction or test. Projects in ModelSim ese interction nd re useful for orgnizing files nd specifying simultion settings. The

More information

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays COMPSCI 5 SS Principles of Computer Science Arrys & Multidimensionl Arrys Agend & Reding Agend Arrys Creting & Using Primitive & Reference Types Assignments & Equlity Pss y Vlue & Pss y Reference Copying

More information

Lecture T4: Pattern Matching

Lecture T4: Pattern Matching Introduction to Theoreticl CS Lecture T4: Pttern Mtching Two fundmentl questions. Wht cn computer do? How fst cn it do it? Generl pproch. Don t tlk bout specific mchines or problems. Consider miniml bstrct

More information

COS 333: Advanced Programming Techniques

COS 333: Advanced Programming Techniques COS 333: Advnced Progrmming Techniques How to find me wk@cs, www.cs.princeton.edu/~wk 311 CS Building 609-258-2089 (ut emil is lwys etter) TA's: Mtvey Arye (rye), Tom Jlin (tjlin), Nick Johnson (npjohnso)

More information

Regular Expressions and Automata using Miranda

Regular Expressions and Automata using Miranda Regulr Expressions nd Automt using Mirnd Simon Thompson Computing Lortory Univerisity of Kent t Cnterury My 1995 Contents 1 Introduction ::::::::::::::::::::::::::::::::: 1 2 Regulr Expressions :::::::::::::::::::::::::::::

More information

CS481: Bioinformatics Algorithms

CS481: Bioinformatics Algorithms CS481: Bioinformtics Algorithms Cn Alkn EA509 clkn@cs.ilkent.edu.tr http://www.cs.ilkent.edu.tr/~clkn/teching/cs481/ EXACT STRING MATCHING Fingerprint ide Assume: We cn compute fingerprint f(p) of P in

More information

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number> DDD16 Compilers nd Interpreters DDB44 Compiler Construction R Prsing Prt 1 R prsing concept Using prser genertor Prse ree Genertion Wht is R-prsing? eft-to-right scnning R Rigthmost derivtion in reverse

More information

10.5 Graphing Quadratic Functions

10.5 Graphing Quadratic Functions 0.5 Grphing Qudrtic Functions Now tht we cn solve qudrtic equtions, we wnt to lern how to grph the function ssocited with the qudrtic eqution. We cll this the qudrtic function. Grphs of Qudrtic Functions

More information

Midterm 2 Sample solution

Midterm 2 Sample solution Nme: Instructions Midterm 2 Smple solution CMSC 430 Introduction to Compilers Fll 2012 November 28, 2012 This exm contins 9 pges, including this one. Mke sure you hve ll the pges. Write your nme on the

More information

From Dependencies to Evaluation Strategies

From Dependencies to Evaluation Strategies From Dependencies to Evlution Strtegies Possile strtegies: 1 let the user define the evlution order 2 utomtic strtegy sed on the dependencies: use locl dependencies to determine which ttriutes to compute

More information

Theory of Computation CSE 105

Theory of Computation CSE 105 $ $ $ Theory of Computtion CSE 105 Regulr Lnguges Study Guide nd Homework I Homework I: Solutions to the following problems should be turned in clss on July 1, 1999. Instructions: Write your nswers clerly

More information

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications Jv CUP Jv CUP is prser-genertion tool, similr to Ycc. CUP uilds Jv prser for LALR(1) grmmrs from production rules nd ssocited Jv code frgments. When prticulr production is recognized, its ssocited code

More information

Distributed Systems Principles and Paradigms

Distributed Systems Principles and Paradigms Distriuted Systems Principles nd Prdigms Chpter 11 (version April 7, 2008) Mrten vn Steen Vrije Universiteit Amsterdm, Fculty of Science Dept. Mthemtics nd Computer Science Room R4.20. Tel: (020) 598 7784

More information

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID: Fll term 2012 KAIST EE209 Progrmming Structures for EE Mid-term exm Thursdy Oct 25, 2012 Student's nme: Student ID: The exm is closed book nd notes. Red the questions crefully nd focus your nswers on wht

More information

Suffix Tries. Slides adapted from the course by Ben Langmead

Suffix Tries. Slides adapted from the course by Ben Langmead Suffix Tries Slides dpted from the course y Ben Lngmed en.lngmed@gmil.com Indexing with suffixes Until now, our indexes hve een sed on extrcting sustrings from T A very different pproch is to extrct suffixes

More information

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries Tries Yufei To KAIST April 9, 2013 Y. To, April 9, 2013 Tries In this lecture, we will discuss the following exct mtching prolem on strings. Prolem Let S e set of strings, ech of which hs unique integer

More information

Control-Flow Analysis and Loop Detection

Control-Flow Analysis and Loop Detection ! Control-Flow Anlysis nd Loop Detection!Lst time! PRE!Tody! Control-flow nlysis! Loops! Identifying loops using domintors! Reducibility! Using loop identifiction to identify induction vribles CS553 Lecture

More information

Introduction To Files In Pascal

Introduction To Files In Pascal Why other With Files? Introduction To Files In Pscl Too much informtion to input ll t once The informtion must be persistent (RAM is voltile) Etc. In this section of notes you will lern how to red from

More information

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example: Boxes nd Arrows There re two kinds of vriles in Jv: those tht store primitive vlues nd those tht store references. Primitive vlues re vlues of type long, int, short, chr, yte, oolen, doule, nd flot. References

More information

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup Regulr Expression Mtching with Multi-Strings nd Intervls Philip Bille Mikkel Thorup Outline Definition Applictions Previous work Two new problems: Multi-strings nd chrcter clss intervls Algorithms Thompson

More information

CMSC 331 First Midterm Exam

CMSC 331 First Midterm Exam 0 00/ 1 20/ 2 05/ 3 15/ 4 15/ 5 15/ 6 20/ 7 30/ 8 30/ 150/ 331 First Midterm Exm 7 October 2003 CMC 331 First Midterm Exm Nme: mple Answers tudent ID#: You will hve seventy-five (75) minutes to complete

More information

Algorithm Design (5) Text Search

Algorithm Design (5) Text Search Algorithm Design (5) Text Serch Tkshi Chikym School of Engineering The University of Tokyo Text Serch Find sustring tht mtches the given key string in text dt of lrge mount Key string: chr x[m] Text Dt:

More information

UT1553B BCRT True Dual-port Memory Interface

UT1553B BCRT True Dual-port Memory Interface UTMC APPICATION NOTE UT553B BCRT True Dul-port Memory Interfce INTRODUCTION The UTMC UT553B BCRT is monolithic CMOS integrted circuit tht provides comprehensive MI-STD- 553B Bus Controller nd Remote Terminl

More information

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS COMPUTATION & LOGIC Sturdy st April 7 : to : INSTRUCTIONS TO CANDIDATES This is tke-home exercise. It will not

More information

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example: cisc1110 fll 2010 lecture VI.2 cll y vlue function prmeters more on functions more on cll y vlue nd cll y reference pssing strings to functions returning strings from functions vrile scope glol vriles

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

Introduction to Algebra

Introduction to Algebra INTRODUCTORY ALGEBRA Mini-Leture 1.1 Introdution to Alger Evlute lgeri expressions y sustitution. Trnslte phrses to lgeri expressions. 1. Evlute the expressions when =, =, nd = 6. ) d) 5 10. Trnslte eh

More information

Example: 2:1 Multiplexer

Example: 2:1 Multiplexer Exmple: 2:1 Multiplexer Exmple #1 reg ; lwys @( or or s) egin if (s == 1') egin = ; else egin = ; 1 s B. Bs 114 Exmple: 2:1 Multiplexer Exmple #2 Normlly lwys include egin nd sttements even though they

More information

COMBINATORIAL PATTERN MATCHING

COMBINATORIAL PATTERN MATCHING COMBINATORIAL PATTERN MATCHING Genomic Repets Exmple of repets: ATGGTCTAGGTCCTAGTGGTC Motivtion to find them: Genomic rerrngements re often ssocited with repets Trce evolutionry secrets Mny tumors re chrcterized

More information

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs. Lecture 5 Wlks, Trils, Pths nd Connectedness Reding: Some of the mteril in this lecture comes from Section 1.2 of Dieter Jungnickel (2008), Grphs, Networks nd Algorithms, 3rd edition, which is ville online

More information

Lists in Lisp and Scheme

Lists in Lisp and Scheme Lists in Lisp nd Scheme Lists in Lisp nd Scheme Lists re Lisp s fundmentl dt structures, ut there re others Arrys, chrcters, strings, etc. Common Lisp hs moved on from eing merely LISt Processor However,

More information

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata EE573 Fll 2002, Exm open book, if question seems mbiguous, sk me to clrify the question. If my nswer doesn t stisfy you, plese stte your ssumptions. cronyms possibly used in this test: CFG :context free

More information