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

dfas res scanning
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 1

1

DFAs, REs & Scanning

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

slide-2
SLIDE 2

2

DFAs, REs & Scanning

abcccabc

a a c b A D B C c (ab(cc*)a)*

slide-3
SLIDE 3

3

DFAs, REs & Scanning

abcccabc

a a c b A D B C c (ab(cc*)a)*

slide-4
SLIDE 4

4

DFAs, REs & Scanning

abcccabc

a a c b A D B C c (ab(cc*)a)*

slide-5
SLIDE 5

5

DFAs, REs & Scanning

abcccabc

a a c b A D B C c (ab(cc*)a)*

slide-6
SLIDE 6

6

DFAs, REs & Scanning

abcccabc

a a c b A D B C c (ab(cc*)a)*

slide-7
SLIDE 7

7

DFAs, REs & Scanning

abcccabc

a a c b A D B C c (ab(cc*)a)*

slide-8
SLIDE 8

a a c b A D B C c 8

DFAs, REs & Scanning

abcccabc

(ab(cc*)a)* ERROR, No Transition

slide-9
SLIDE 9

9

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

DFAs, REs & Scanning

slide-10
SLIDE 10

10 a a c b A D B C c

DFAs, REs & Scanning Simulation

fun A: token = if endstr {return “A”} getchar(x); bufferchar(x) if x = a {return B} else error fun B: token = if endstr { error} getchar(x); bufferchar(x) if x = b {return C} else error fun C: token if endstr { error} getchar(x); bufferchar(x) if x = c {return D} else error fun D: token if endstr { return “D”} getchar(x): bufferchar(x) if x = c {return D} else if x = a {return A} else error

fun getToken: token = clearbuffer return A

slide-11
SLIDE 11

11

DFAs, REs & Scanning DFA examples - Pascal tokens

Letter Letter or Digit

“ : ” “ = ”

slide-12
SLIDE 12

12

DFAs, REs & Scanning

Finite alphabet A plus meta symbols λ the empty string Finite strings of symbols from A (catenations) () grouping | alternatives * zero or more +

  • ne or more

“x” literal x

A = {a,b,c} (abc+a)* e.g.

“” empty string abcccaabcc abca

slide-13
SLIDE 13

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

slide-14
SLIDE 14

14

DFAs, REs & Scanning Regular Expression Regular Set Φ λ ab...c A | B A B A*

Where a,b,...,c are in alphabet and A,B are regular expressions

{ } { “” } { “ab...c” } RA + RB {ab | a in RA & b in RB } { “” } + {x..z | x..z in RA }

Where + is set union and RA is the Regular Set for A

A set is regular if some regular expression denotes it

slide-15
SLIDE 15

15

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.

Notes:

slide-16
SLIDE 16

16

DFAs, REs & Scanning NFA’s A finite automoton can be non-deterministic

a a λ Choice for transition or transition on no character read b c a | ab | ac When faced with a choice the NFA always guesses right.

slide-17
SLIDE 17

17

DFAs, REs & Scanning RE to NFA pt 1

Φ λ s s Α Β λ λ Α Β

slide-18
SLIDE 18

18

DFAs, REs & Scanning RE to NFA pt 2

A | B A * λ λ λ λ λ λ Α Β Α

slide-19
SLIDE 19

19

DFAs, REs & Scanning NFA to DFA pt 1

States of the DFA are sets of NFA states

3 1 4 2 5 a b a a.b

λ

a

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

slide-20
SLIDE 20

20

DFAs, REs & Scanning NFA to DFA pt 2

3 1 4 2 5 a b a a.b

λ

a Start with initial state Close it 12 Now, from state 12 we can reach states 3 or 4 or 5 on an “a” transition so:

slide-21
SLIDE 21

21

DFAs, REs & Scanning NFA to DFA pt 3

3 1 4 2 5 a b a a.b

λ

a 12 345 Now, from state 345 we can reach 5 on an “a” transition or states 4 or 5 on a “b” transition so: a

slide-22
SLIDE 22

22

DFAs, REs & Scanning NFA to DFA pt 4

3 1 4 2 5 a b a a.b

λ

a 12 345 b a a 5 45 Now, from state 45 we can reach 5 on an “a”

  • r “b” transition so:
slide-23
SLIDE 23

23

DFAs, REs & Scanning NFA to DFA pt 5

3 1 4 2 5 a b a a.b

λ

a 12 345 b a a 5 45 a,b

We now have an equivalent DFA

slide-24
SLIDE 24

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 The program LEX does this