+ All Categories

Download - 2. Scanning

Transcript
Page 1: 2. Scanning

2. Scanning

College of Information and Communications

Prof. Heejin Park

Page 2: 2. Scanning

Finite automata

Finite automata consists of states transitions (on symbols) start state accepting states

1 2i f

3

Page 3: 2. Scanning

Finite automata

Used for recognizing pattern represented by regular expressions

1 2

if

i f3

1 2 3i f

Page 4: 2. Scanning

Finite automata

identifier = letter(letter|digit)*

Page 5: 2. Scanning

Finite automata

Error transitions are not drawn.

~letter ~(letter|digit)

Page 6: 2. Scanning

Finite automata

xtemp 1 2 2 2 2 2

x t e m p

Page 7: 2. Scanning

Finite automata

DFA (deterministic finite automaton) Given a state and a symbol, the next state is unique.

Page 8: 2. Scanning

Finite automata

NFA (nondeterministic finite automaton) Given a state and a symbol, the next state is not unique.

Page 9: 2. Scanning

DFA

Examples The set of all strings over {a,b,c} containing exactly one b.

(a|c)*b(a|c)*

Page 10: 2. Scanning

DFA

Examples The set of all strings over {a,b,c} containing at most one b.

(a|c)* | (a|c)*b(a|c)* (a|c)*(b|ε)(a|c)*

Page 11: 2. Scanning

DFAs for PL tokens

Examplesnat = [0-9]+signedNat = (+|-)? natnumber = signedNat (“.” nat)? (E signedNat)?

digit = [0-9]nat = digit+signedNat = (+|-)? natnumber = signedNat (“.” nat)? (E signedNat)?

Page 12: 2. Scanning

DFAs for PL tokens

Examplesdigit = [0-9]nat = digit+singedNat = (+|-)? natnumber = signedNat (“.” nat)? (E signedNat)?

Page 13: 2. Scanning

DFAs for PL tokens

Examplesdigit = [0-9]nat = digit+signedNat = (+|-)? natnumber = signedNat (“.” nat)? (E signedNat)?

nat

Page 14: 2. Scanning

DFAs for PL tokens

Examplesdigit = [0-9]nat = digit+signedNat = (+|-)? natnumber = signedNat (“.” nat)? (E signedNat)?

signedNat nat

Page 15: 2. Scanning

DFAs for PL tokens

Examplesdigit = [0-9]nat = digit+signedNat = (+|-)? natnumber = signedNat (“.” nat)? (E signedNat)?

signedNat nat signedNat

Page 16: 2. Scanning

DFAs for PL tokens

Comments {this is a Pascal comment}

{(~})*}

Page 17: 2. Scanning

DFAs for PL tokens

Comments /* this is a C comment */

ba (b*(a*~(a|b)b*)*a*) ab

Page 18: 2. Scanning

DFAs for PL tokens

longest substring?

~letter ~(letter|digit)

lookahead

Page 19: 2. Scanning

Merging DFAs

a DFA for each token DFA for some tokens

Page 20: 2. Scanning

Merging DFAs when tokens begin with the same symbol.

Merging DFAs

< =

< >

<

Page 21: 2. Scanning

a DFA for all PL tokens

It is possible to generate a DFA for each token and merging the DFAs.

However, it is not a systematic way.

There is a more systematic way Regular expression NFA DFA

Page 22: 2. Scanning

NFA

Given a state and a symbol, the next state is not unique.

Page 23: 2. Scanning

NFA

It also includes ε-transitions.

ε

Page 24: 2. Scanning

NFA

ε-transitions makes merging automata without combining states.

Page 25: 2. Scanning

NFA

NFA for the empty string.

DFA for the empty string

ε

Page 26: 2. Scanning

NFA

abb1 2 4 2 4

1 3 4 2 4 2 4

a b ε b

a ε ε b ε b

Page 27: 2. Scanning

NFA

acab

ε

Page 28: 2. Scanning

NFA

ab+|ab*|b* or (a|ε)b*

Corresponding regular expression

Page 29: 2. Scanning

NFA

(a|c)*b

Corresponding regular expression

ε

Page 30: 2. Scanning

An alphabet Σ the set of symbols: {a, b, … }

a set of states S normal states, a start state, a set of accepting states

a transition function T (for avery pair of each state and each symbol) T: S X Σ S (DFA) T: S X (Σ U {ε}) p(S) (NFA)

Finite Automata

T(1,a) {2,4} T(1,a) 2

1 2a b

3

4a

1 2a b

3

4

a b

Page 31: 2. Scanning

Strings accepted by a finite automata Strings that can reach one of the accepting states using

transitions from the start state. DFA

ab: 1 2 3 (accepted)

a b aa: 1 2 4 (not accepted)

a a

Finite Automata

1 2a b

3

4a

Page 32: 2. Scanning

Strings accepted by a finite automata Strings that can reach one of the accepting states using

transitions from the start state. NFA

ab: 1 {2,4} {3,2} (accepted)

a b

Finite Automata

1 2a b

3

4

a b

subset construction

What if ε-transitions exist?

Page 33: 2. Scanning

The language accepted by a finite automata The set of strings accepted by the finite automata.

Finite Automata


Top Related