dfas res scanning
play

DFAs, REs & Scanning Finite set of states: S Finite alphabet: A - PowerPoint PPT Presentation

DFAs, REs & Scanning Finite set of states: S Finite alphabet: A Transition function: a b T: SxA -> S A B C Start State: s Set of final states f1..fn c a A,a: B B,b: C D C,c: D D,a: A c D,c: D with s = A and F = {A,


  1. DFAs, REs & Scanning Finite set of states: S Finite alphabet: A Transition function: a b T: SxA -> S A B C Start State: s Set of final states f1..fn c a A,a: B B,b: C D C,c: D D,a: A c D,c: D with s = A and F = {A, D} 1 (ab(cc*)a)*

  2. DFAs, REs & Scanning abcccabc a b A B C c a D c 2 (ab(cc*)a)*

  3. DFAs, REs & Scanning abcccabc a b A B C c a D c 3 (ab(cc*)a)*

  4. DFAs, REs & Scanning abcccabc a b A B C c a D c 4 (ab(cc*)a)*

  5. DFAs, REs & Scanning abcccabc a b A B C c a D c 5 (ab(cc*)a)*

  6. DFAs, REs & Scanning abcccabc a b A B C c a D c 6 (ab(cc*)a)*

  7. DFAs, REs & Scanning abcccabc a b A B C c a D c 7 (ab(cc*)a)*

  8. DFAs, REs & Scanning abcccabc a b A B C ERROR, No Transition c a D c 8 (ab(cc*)a)*

  9. DFAs, REs & Scanning A DFA divides the set of all finite strings over its alphabet into two sets: accepted strings vs rejected To ACCEPT the input string Consume entire input string - and - Finish in a FINAL state. REJECT if either No transition - or - Finish in a non-Final state 9

  10. DFAs, REs & Scanning Simulation fun A: token = if endstr {return “A”} getchar(x); bufferchar(x) a b if x = a {return B} else error A B C fun B: token = if endstr { error} c getchar(x); bufferchar(x) a if x = b {return C} else error fun C: token D if endstr { error} getchar(x); bufferchar(x) c if x = c {return D} else error fun D: token fun getToken: token = if endstr { return “D”} getchar(x): bufferchar(x) clearbuffer if x = c {return D} else return A if x = a {return A} else error 10

  11. DFAs, REs & Scanning DFA examples - Pascal tokens Letter Letter or Digit “ : ” “ = ” 11

  12. DFAs, REs & Scanning Finite alphabet A plus meta symbols λ the empty string Finite strings of symbols from A (catenations) () grouping A = {a,b,c} | alternatives * zero or more (abc + a)* + one or more e.g. “x” literal x “” empty string abcccaabcc abca 12

  13. DFAs, REs & Scanning Regular Expression examples D = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 L = A | a | B | ... | z ID1 = L (L | D)* Letter followed by letters and digits ID2 = L (L |D | ”_” (L | D))* Letter followed by letters and digits and underscores but no adjacent underscores or terminated by underscore. PR = D + “.” D + ( λ | “E” ( λ | “+” | “-” ) D + ) Pascal real numbers 13

  14. DFAs, REs & Scanning Regular Expression Regular Set Φ { } λ { “” } { “ab...c” } ab...c R A + R B A | B {ab | a in R A & b in R B } A B { “” } + {x..z | x..z in R A } A* Where a,b,...,c are in alphabet Where + is set union and and A,B are regular R A is the Regular Set for A expressions 14 A set is regular if some regular expression denotes it

  15. Notes: Not all sets are regular E.g. { a...ab...b | same number of a’s as b’s } Modern Computer languages have the property that their set of tokens is a regular set. 15

  16. DFAs, REs & Scanning NFA’s A finite automoton can be non-deterministic λ a b c a Choice for transition or transition on no character read a | ab | ac 16 When faced with a choice the NFA always guesses right.

  17. RE to NFA pt 1 DFAs, REs & Scanning s Φ λ s Α Β λ λ Α Β 17

  18. RE to NFA pt 2 DFAs, REs & Scanning A | B λ λ Α Β λ λ A * Α λ λ 18

  19. NFA to DFA pt 1 DFAs, REs & Scanning λ a 2 1 5 a a a.b 4 3 b Proc Close = add to a state those states to which you can go on lambda transitions Proc MakeDeterministic = add states if you can go on lambda or same symbol transitions 19 States of the DFA are sets of NFA states

  20. NFA to DFA pt 2 DFAs, REs & Scanning λ a 2 1 5 a a a.b 4 3 Start with initial state b Close it 12 Now, from state 12 we can reach states 3 or 4 or 5 on an “a” transition so: 20

  21. NFA to DFA pt 3 DFAs, REs & Scanning λ a 2 1 5 a a a.b 4 3 b a 345 12 Now, from state 345 we can reach 5 on an “a” transition or states 4 or 5 on a “b” transition so: 21

  22. NFA to DFA pt 4 DFAs, REs & Scanning λ a 2 1 5 a a a.b 4 3 b b 45 a 345 12 Now, from state 45 we a can reach 5 on an “a” 5 or “b” transition so: 22

  23. NFA to DFA pt 5 DFAs, REs & Scanning λ a 2 1 5 a a a.b 4 3 b b 45 a 345 12 a,b a 5 23 We now have an equivalent DFA

  24. DFAs, REs & Scanning To automate the construction of a scanner Define the tokens with a regular expression Create the equivalent NFA Construct the equivalent DFA Program the DFA Caveat: Then NFA -> DFA construction can require a LARGE number of states 24 The program LEX does this

Recommend


More recommend