You are on page 1of 10

Compiler Design 1

Assignment 1: Lexical Analysis (correction)
Solved by:
Ahmad Bijairimi
780214­1171
ahab7847@student.uu.se

(2010­11­23)

Let P1, P2, P3, … , Pn be lex patterns.

Lex structure:

Pattern Action
P1: (aba)+ (action 1);
P2: (a(b*)a) (action 2);
P3: (a|b) (action 3);

Recognizer:
(aba)+

(a(b*)a)

(a|b)

Each pattern recognizes lexemes and described by regexs.

NFA is constructed from the given regular expressions above:

P1
P2

P3

Then P1, P2 and P3 are combined together to form a complete NFA diagram:
From the NFA above, DFA is constructed in order to have a faster recognizer. To convert NFA to DFA,
operations below will be used:

– The transition function from NFA MoveNFA(S0-n, Σ) where Σ = {a,b}


– ε-closure(s) where s is a single state from NFA.
– ε-closure(T) where T is a set of states from NFA.

Start state: S0

ε-closure({0}) = {0,1,5,13,14,16} = S0

Find a state that has an edge a from S0:


MoveDFA(S0, a)
= ε-closure(MoveNFA(S0, a))
= ε-closure({2,6,15})
= {2,6,7,8,10,11,15,18,19} = S1

Find a state that has an edge b from S0:


MoveDFA(S0, b)
= ε-closure(MoveNFA(S0, b))
= ε-closure({17})
= {17,18,19} = S2
----------------------------
Find a state that has an edge a from S1:
MoveDFA(S1, a)
= ε-closure(MoveNFA(S1, a))
= ε-closure({12})
= {12,19} = S3

Find a state that has an edge b from S1:


MoveDFA(S1, b)
= ε-closure(MoveNFA(S1, b))
= ε-closure({3,9})
= {3,8,9,10,11} = S4
----------------------------
Find a state that has an edge a from S2:
MoveDFA(S2, a)
= ε-closure(MoveNFA(S2, a))
= ε-closure({ })
={}

Find a state that has an edge b from S2:


MoveDFA(S2, b)
= ε-closure(MoveNFA(S2, b))
= ε-closure({ })
={}
----------------------------
Find a state that has an edge a from S3:
MoveDFA(S3, a)
= ε-closure(MoveNFA(S3, a))
= ε-closure({ })
={}

Find a state that has an edge b from S3:


MoveDFA(S3, b)
= ε-closure(MoveNFA(S3, b))
= ε-closure({ })
={}
----------------------------
Find a state that has an edge a from S4:
MoveDFA(S4, a)
= ε-closure(MoveNFA(S4, a))
= ε-closure({4,12})
= {1,4,12,19} = S5

Find a state that has an edge b from S4:


MoveDFA(S4, b)
= ε-closure(MoveNFA(S4, b))
= ε-closure({9})
= {8,9,10,11} = S6
----------------------------
Find a state that has an edge a from S5:
MoveDFA(S5, a)
= ε-closure(MoveNFA(S5, a))
= ε-closure({2})
= {2} = S7

Find a state that has an edge b from S5:


MoveDFA(S5, b)
= ε-closure(MoveNFA(S5, b))
= ε-closure({ })
={}
----------------------------
Find a state that has an edge a from S6:
MoveDFA(S6, a)
= ε-closure(MoveNFA(S6, a))
= ε-closure({12})
= {12,19} = S3

Find a state that has an edge b from S6:


MoveDFA(S6, b)
= ε-closure(MoveNFA(S6, b))
= ε-closure({9})
= {8,9,10,11} = S6
----------------------------
Find a state that has an edge a from S7:
MoveDFA(S7, a)
= ε-closure(MoveNFA(S7, a))
= ε-closure({ })
={}

Find a state that has an edge b from S7:


MoveDFA(S7, b)
= ε-closure(MoveNFA(S7, b))
= ε-closure({3})
= {3} = S8
----------------------------
Find a state that has an edge a from S8:
MoveDFA(S8, a)
= ε-closure(MoveNFA(S8, a))
= ε-closure({4})
= {1,4,19} = S9

Find a state that has an edge b from S8:


MoveDFA(S8, b)
= ε-closure(MoveNFA(S8, b))
= ε-closure({ })
={}
----------------------------
Find a state that has an edge a from S9:
MoveDFA(S9, a)
= ε-closure(MoveNFA(S9, a))
= ε-closure({2})
= {2} = S7

Find a state that has an edge b from S9:


MoveDFA(S9, b)
= ε-closure(MoveNFA(S9, b))
= ε-closure({ })
={}
----------------------------

Final states in DFA = S1, S2, S3, S5, S9 where all these states contain the final states.
DFA diagram:

Transition table:

State Input a Input b


S0 S1 S2
S1 S3 S4
S2 - -
S3 - -
S4 S5 S6
S5 S7 -
S6 S3 S6
S7 - S8
S8 S9 -
S9 S7 -
Edges:

State Input symbol Transition state


S0 a S1
S0 b S2
S1 a S3
S1 b S4
S2 - -
S3 - -
S4 a S5
S4 b S6
S5 a S7
S6 a S3
S6 b S6
S7 b S8
S8 a S9
S9 a S7

The final state returns either an action number or 0:

State Action
S0 0
S1 3
S2 3
S3 2
S4 0
S5 1
S6 0
S7 0
S8 0
S9 1
Analysis of the string abaabbaba

State Input symbol String Transition Action Output/Token


state number
ε abaabbaba
S0 a baabbaba S1 3
S1 b aabbaba S4
S4 a abbaba S5 1
S5 a bbaba S7
S7 b baba S8
S8 b aba = (aba)+
Error occurred because there is no transition state for input b from S8. Perform
backtracking to mark the last encountered match and lookahead to to determine the longest
match. Terminate at S8, return action 1 and then restart DFA.
abbaba
S0 a bbaba S1 3
S1 b baba S4
S4 b aba S6
S6 a ba S3 2
S3 b abba = (a(b*)a)
Error occurred because there is no transition state for input b from S3. Perform
backtracking to mark the last encountered match and lookahead to to determine the longest
match. Terminate at S3, return action 2 and then restart DFA.
ba
S0 b a S2 3
S2 a b = (a|b)
Error occurred because there is no transition state for input a from S2. Perform
backtracking to mark the last encountered match and lookahead to to determine the longest
match. Terminate at S2, return action 3 and then restart DFA.
a
S0 a ε S1 3 a = (a|b)
No more input, so the lexer terminates here and return action 3.
Minimizing DFA

The original DFA is not minimal!

S6 = {8,9,10,11} and S4 = {3,8,9,10,11} are both non-final states, so merge S6 to S4,

DFA is still not minimal!

S3 = {12, 19} and S5 = {1,4,12,19} are both final states, so merge S3 to S5,

DFA is still not minimal!


S8 = {3} and S4 = {3,8,9,10,11} are both non-final states, so merge S8 to S4,

DFA is still not minimal!

S9 = {1,4,19} and S5 = {1,4,12,19} are both final states, so merge S9 to S5,

DFA is still not minimal!

S5 = {1,4,12,19} and S1 = {2,6,7,8,10,11,15,18,19} are both final states, so merge S5 to S1

No more states that can be combined, so I assume that this is the minimal state of DFA.

You might also like