Docsity

Lexical Analysis-Part II - Lecture Notes | EECS 483, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Lexical Analysis-Part II - Lecture Notes | EECS 483 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Lexical Analysis

– Part II

EECS 483 – Lecture 3 University of Michigan Wednesday, September 13, 2006

  • 1 -

Class Problem From Last Time

q

q2 q

q

Is this a DFA or NFA? What strings does it recognize?

  • 3 -

Reading

Ch 2

» Just skim this » High-level overview of compiler, which could

be useful

Ch 3

» Read carefully, more closely follows lecture » Go over examples

  • 4 -

How Does Lex Work?

FLEX

Regular Expressions

C code

Some kind of DFAs and NFAs stuff going on inside

  • 6 -

Regular Expression to NFA

Its possible to construct an NFA from aregular expression

Thompson’s construction algorithm

» Build the NFA inductively » Define rules for each base RE » Combine for more complex RE’s

s

f

E

general machine

  • 7 -

Thompson Construction

S
F

ε

empty string transition

S
F

x

alphabet symbol transition

S
F
E
A
E

Concatenation:

(E1 E2)

ε

ε

ε

ε

•New start state S

ε

-transition to the start state of E

ε

-transition from final/accepting state of E1 to A,

ε

-transition from A to start

state of E2•

ε

-transitions from the final/accepting state E2 to the new final state F

  • 9 -

Thompson Construction - Example

A
F

ε

ε

ε

ε

Develop an NFA for the RE: (x | y)*

B
C
D
E

x y

First create NFA for (x | y)

A
H

ε

ε

ε

ε

B
C
D
E

x y

S
F
G

ε

ε

ε

ε

Then add in the closure operator

  • 10 -

Class Problem

Develop an NFA for the RE: (+? | -?) d+

  • 12 -

NFA to DFA (2)

Problem 1: Multiple transitions

» Solve by subset construction » Build new DFA based upon the power set of states on

the NFA

» Move (S,a) is relabeled to target a new state whenever

single input goes to multiple states

a

a

b

a+ b*

(1,a)

Æ

1 or 2, create new state 1/

(1/2,a)

Æ

1/

(1/2,b)

Æ

2

a

a

start

start

b

b

(2,a)

Æ

ERROR

(2,b)

Æ

2

Any state with “2” in name is a final state

  • 13 -

NFA to DFA (3)

Problem 2:

transitions

» Any state reachable by an

ε

transition is “part of the

state”

»

ε

-closure - Any state reachable from S by

ε

transitions

is in the

ε

-closure; treat

ε

-closure as 1 big state,

always include

ε

-closure as part of the state

a

b

start

ε

ε

ε

-closure(1) = {1,2,3} ε

-closure(2) = {2,3} create new state 1/2/3create new state 2/

2/

3

a

b

start

1/2/

a

b

(1/2/3, a)

Æ

2/

(1/2/3, b)

Æ

3

(2/3, a)

Æ

2/

(2/3, b)

Æ

3

ab

  • 15 -

Class Problem

0

1

(^24)

6

(^35)

9

7

ε

ε

ε ε

ε ε

a

b

ε^ ε a

8

b

Convert this NFA to a DFA

  • 16 -

NFA to DFA Optimizations

™

Prior to NFA to DFAconversion:

™

Empty cycle removal

» Combine nodes that

comprise cycle

» Combine 2 and 3

™

Empty transitionremoval

» Remove state 4,

change transition 2-4to 2-

a

c

start

ε ε

ε ε

ε

ε

b

start

a

ε

c

ε

  • 18 -

State Minimization (2)

Idea – find groups of equivalent states andmerge them

» All transitions from states in group G1 go to states in

another group G

» Construct minimized DFA such that there is 1 state for

each group of states

b

start

b a

b

a

a

b

a

Basic strategy: identifydistinguishing transitions

  • 19 -

Putting It All Together

RE
Æ
NFA
NFA
Æ
DFA

Optimize DFA DFA Simulation

Character Stream

REs for Tokens

Token stream (and errors)

Flex

Remaining issues: how to Simulate, multiple REs, producing a token stream, longest match, rule priority