UNIT I BKS Lexical Analysis VI - NFA With Epsilon To DFA
UNIT I BKS Lexical Analysis VI - NFA With Epsilon To DFA
Sharma
UNIT I
Unit I: Syllabus
• Introduction to Compiler
• Major Data Structures in Compiler
• Types of Compiler
• Front-End and Back-End of Compiler
• Compiler Structure:
– Analysis-synthesis model of compilation
• Various phases of Compiler
Learn Compiler Design: From B. K. Sharma
Unit I: Syllabus
• Lexical Analysis:
– Input Buffering
– Specification and Recognition of Tokens
– Design of a Lexical Analyzer Generator
• Lex
Learn Compiler Design: From B. K. Sharma
Summary of Lecture 9: Regular Expression and
Lexical Analyzer
1: Abstract model of a computing entity or digital
computers that decides whether to accept or reject a
string is called
2: In Finite Automaton, the automaton is exactly one of a
finite number of states at any given time.
3: Two main types of automata are DFA and NFA.
4: Three specifications of tokens are : string, language and
regular expressions.
5. Regular language is accepted by Finite Automata and is
described by regular expression.
6: Tokens are specified by regular expressions and recognized
by Finite Automata.
7: Thompson’s Construction is used to convert regular expressions to NFA with
-moves
Learn Compiler Design: From B. K. Sharma
Thompson construction
NFA
Subset construction
DFA
Minimized DFA
Scanner Program
Learn Compiler Design: From B. K. Sharma
Operation Description
Set of NFA states reachable from NFA
ε- closure(s)
state s on ε-transitions alone.
Set of NFA states reachable from some
NFA state s in set S on ε-transitions
ε- closure(S) alone.
= ∪s in S ε- closure(s)
Set of NFA states to which there is a
move(S, α) transition on input symbol α from some
state s in S
Learn Compiler Design: From B. K. Sharma
ε-Closure(s): {s} {t s … t}
start b b
a
0 3 4 5 6
a b
b
7 8
= ε-closure {MoveNFA((q0, 1) ∪
MoveNFA(q1, 1) ∪ MoveNFA(q2, 1) }
= ε-closure {q3}
= {q3} = B.
Learn Compiler Design: From B. K. Sharma
NFA
For state C:
MoveDFA(C, 0) = ε-closure {MoveNFA(q4, 0) } = ϕ
MoveDFA(C, 1) = ε-closure {MoveNFA(q4, 1) } = ϕ
Learn Compiler Design: From B. K. Sharma
The DFA is :
Learn Compiler Design: From B. K. Sharma
= ε-closure{MoveNFA(q0, 1) ∪ δ(q1, 1) ∪
MoveNFA (q2, 1)}
= ε-closure{q1}
= ε-closure{q2}
= {q2} call it state C
Learn Compiler Design: From B. K. Sharma
= ε-closure{q2}
= {q2}, C itself
Learn Compiler Design: From B. K. Sharma
First we calculate ε-closure of each state in the NFA:
ε-closure(s)
Learn Compiler Design: From B. K. Sharma
a
2 3
start a b b
0 1 6 7 8 9 10
4 b
b
5
Second step: Look for the start state of equivalent DFA
The start state A of the equivalent DFA is ε-closure(0)=
{0,1,2,4,7}
Then the starting state A= {0,1,2,4,7}
Learn Compiler Design: From B. K. Sharma
Second compute:
1: MoveDFA(A,a)= ε-closure( MoveNFA( A, a))
2: MoveDFA(A,b)= ε-closure( MoveNFA( A, b))
Learn Compiler Design: From B. K. Sharma
Partial DFA:
b
b C
a
Start a b
A B
a
Learn Compiler Design: From B. K. Sharma
Third compute:
3: MoveDFA(B,a)= ε-closure( MoveNFA( B, a))
4: MoveDFA(B,b)= ε-closure( MoveNFA( B, b))
Learn Compiler Design: From B. K. Sharma
MoveDFA(B,a)= B
Learn Compiler Design: From B. K. Sharma
MoveDFA(B,b)=D
Learn Compiler Design: From B. K. Sharma
Fourth compute:
5: MoveDFA(C,a)= ε-closure( MoveNFA( C, a))
6: MoveDFA(C,b)= ε-closure( MoveNFA( C, b))
Learn Compiler Design: From B. K. Sharma
MoveDFA(C,a)=B
Learn Compiler Design: From B. K. Sharma
MoveDFA(C,b)=C
Learn Compiler Design: From B. K. Sharma
Fifth compute:
7: MoveDFA(D,a)= ε-closure( MoveNFA( D, a))
8: MoveDFA(D,b)= ε-closure( MoveNFA( D, b))
Learn Compiler Design: From B. K. Sharma
MoveDFA(D,a)=B
Learn Compiler Design: From B. K. Sharma
MoveDFA(D,b)=E
Learn Compiler Design: From B. K. Sharma
Sixth compute:
9: MoveDFA(E,a)= ε-closure( MoveNFA( E, a))
10: MoveDFA(E,b)= ε-closure( MoveNFA( E, b))
Learn Compiler Design: From B. K. Sharma
MoveDFA(E,a)=B
Learn Compiler Design: From B. K. Sharma
MoveDFA(E,b)=C
Learn Compiler Design: From B. K. Sharma
ε-Closure(s):
ε-Closure(S): ε-Closure of a set of states S
MoveNFA(S, α):
Learn Compiler Design: From B. K. Sharma
(a | b)* a(a | b)
(a | b)*a (a | b)
Draw the transition diagram for identifiers.