


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















C code
Some kind of DFAs and NFAs stuff going on inside
Flex
REs for Tokens
Optimize DFA DFA Simulation
Character Stream
Token stream (and errors)
ε
empty string transition
x
alphabet symbol transition
Concatenation:
ε
ε
ε
ε
•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
ε
ε
ε
ε
ε
Closure: (E*)
ε
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
ε
ε
Develop an NFA for the RE: (+? | -?) d+
a b 3
ε ε
ε ε
c
(a| b) c*
start
» 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
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
a
ε ε
ε ε
ε
Y
Y
» Combine nodes that
comprise cycle
» Combine 2 and 3
Y
» Remove state 4,
change transition 2-4to 2-
c
start
ε
b
a
ε
c
ε
start
b
b a
b
a
b
a
b
start
a
Both DFAs accept baba
b
start
a
a
Remaining issues: how to Simulate, multiple REs, producing a token stream, longest match, rule priority
Flex
REs for Tokens
Optimize DFA DFA Simulation
Character Stream
Token stream (and errors)
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!