Docsity

Lexical Analysis Part III - Compiler Construction | EECS 483, Study Guides, Projects, Research of Electrical and Electronics Engineering

Partial preview of the text

Download Lexical Analysis Part III - Compiler Construction | EECS 483 and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

Lexical Analysis

– Part II

EECS 483 – Lecture 3 University of Michigan Wednesday, September 10, 2003

  • 1 -

Announcements

Y

GSI office hours

» Peter Kao » Yuan Lin

Y

Project 1

» Is available on the website » Due 9/24 » Talk about it at the end of the lecture

  • 3 -

How Does Lex Work?^ Regular^ Expressions

C code

FLEX

Some kind of DFAs and NFAs stuff going on inside

  • 4 -

How Does Lex Work?

Flex

REs for Tokens

RE
Æ
NFA
NFA
Æ
DFA

Optimize DFA DFA Simulation

Character Stream

Token stream (and errors)

  • 6 -

Thompson Construction

S
F

ε

empty string transition

S
F

x

alphabet symbol transition

Concatenation:

(E1 E2)
S
F
E
A
E

ε

ε

ε

ε

•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

  • 7 -

Thompson Construction - Continued

ε

S
F
E

ε

ε

ε

ε

Closure: (E*)

A
S
F

ε

Alteration: (E1 | E2)

•New start state S

ε

-transitions to the start states of

E1 and E2•

ε

-transitions from the final/accepting states of E and E2 to the new final state F

ε

E

ε

E
  • 9 -

Class Problem

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

  • 10 -

NFA to DFA

Y

Remove the non-determinism

Y

2 problems

» States with multiple outgoing edges due to

same input

transitions

a b 3

ε ε

ε ε

c

(a| b) c*

start

  • 12 -

NFA to DFA (3)

Y

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

  • 13 -

NFA to DFA - Example

2

3 a

ε 1 ε

start

a

4 6

6

a

ε

-closure(1) = {1, 2, 3, 5} •Create a new state A = {1, 2, 3, 5}and examine transitions out of it•move(A, a) = {3, 6}•Call this a new subset state = B ={3, 6}•move(A, b) = {4}•move(B, a) = {6}•move(B, b) = {4}•Complete by checking move(4, a);move(4, b); move(6, a); move(6, b)

b a

5 ε

A

B 4

start

a

a

b

b

  • 15 -

NFA to DFA Optimizations

a

ε ε

ε ε

ε

Y

Prior to NFA to DFAconversion:

Y

Empty cycle removal

» Combine nodes that

comprise cycle

» Combine 2 and 3

Y

Empty transitionremoval

» Remove state 4,

change transition 2-4to 2-

c

start

ε

b

a

ε

c

ε

start

  • 16 -

State Minimization

Y

Resulting DFA can be quite large

» Contains redundant or equivalent states

b

b a

b

a

b

a

b

start

a

Both DFAs accept baba

b

start

a

a

  • 18 -

Putting It All Together

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

Flex

REs for Tokens

RE
Æ
NFA
NFA
Æ
DFA

Optimize DFA DFA Simulation

Character Stream

Token stream (and errors)

  • 19 -

Simulating the DFA^ * Straight-forward translation of DFA to C program^ * Transitions from each state/input can be represented as table

  • Table lookup tells where to go based on current state/input

trans_table[NSTATES][NINPUTS];accept_states[NSTATES];state = INITIAL;while (state != ERROR) {

c = input.read();if (c == EOF) break;state = trans_table[state][c];

} return accept_states[state];

Not quite this simple but close!