0% found this document useful (0 votes)
5 views

Finite Automata-Topic RE to NFA

The document outlines the first unit of a Compiler Design course, focusing on Lexical Analysis, including the structure of compilers, the role of the lexical phase, and the representation of tokens using regular expressions. It covers finite automata, detailing Non-Deterministic Finite Automata (NFA) and Deterministic Finite Automata (DFA), along with their conversions and properties. Additionally, it includes assignments related to constructing NFAs using Thompson’s Construction method for various regular expressions.

Uploaded by

dhanavarshini14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Finite Automata-Topic RE to NFA

The document outlines the first unit of a Compiler Design course, focusing on Lexical Analysis, including the structure of compilers, the role of the lexical phase, and the representation of tokens using regular expressions. It covers finite automata, detailing Non-Deterministic Finite Automata (NFA) and Deterministic Finite Automata (DFA), along with their conversions and properties. Additionally, it includes assignments related to constructing NFAs using Thompson’s Construction method for various regular expressions.

Uploaded by

dhanavarshini14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT-I

LEXICAL ANALYSIS

March 2, 2025 SCS1303 Compiler Design 1


UNIT 1 LEXICAL ANALYSIS

• Structure of compiler
• Functions and Roles of lexical phase
• Input buffering
• Representation of tokens using regular expression
• Properties of regular expression
• Finite Automata
• Regular Expression to Finite Automata
• NFA to Minimized DFA.

March 2, 2025 SCS1303 Compiler Design 2


Outline
• Finite Automata
– Non-Deterministic Finite Automata(NFA)
– Deterministic Finite Automata(DFA)
– Conversion of Regular Expression to
NFA(Thompson’s Construction)
– Conversion of NFA to DFA(Subset Construction
Method)
– Conversion of DFA to Minimized DFA

• Assignments

March 2, 2025 SCS1303 Compiler Design 3


What is Finite Automata?
• A recognizer for a language is a program that takes as input a string x
and answers “yes” if x is a sentence of the language and “no”
otherwise.

• Finite automata: a generalized transition diagram to compile regular


expression into a recognizer.

• A machine that accepts Regular Language.

Applications:
– Compliers
– Text processing
– Hardware design

March 2, 2025 SCS1303 Compiler Design 4


Types of Automata

March 2, 2025 SCS1303 Compiler Design 5


Finite Automata
• Both DFA and NFA are capable of recognizing the regular sets.
(what a regular expression denote.)
• Time-space Trade-off:
– DFA can lead to fast recognizers than NFA.
– DFA can be much larger than an equivalent NFA.

March 2, 2025 SCS1303 Compiler Design 6


Non-Deterministic Finite Automata
• NFA is a mathematical model that consists of:
– a set of states s
– a set of input symbols ∑ (input symbol alphabet)
– a transition function move that maps state-symbol pairs to set of
states
– a state s0 that is distinguished as the start or initial state
– a set of states F distinguished as accepting or final states.

• An NFA can be represented diagrammatically by a labelled directed graph


called a transition graph.
• The nodes are states and the labelled edges represent the transition function.
• This graph looks like a transition diagram, but the same character can label
two or more transitions out of one state.
• Edges can be labelled by the special symbol ε .
March 2, 2025 SCS1303 Compiler Design 7
NFA-Example
NFA for (a|b)*abb
a

start a b b
0 1 2 3
Transition Table

• the set of states of the NFA is {0,1,2,3}


• the input symbol alphabet is {a,b}
• state 0 is start state
• The accepting state 3 is indicated by double circle.
March 2, 2025 SCS1303 Compiler Design 8
Formal definition of NFA

March 2, 2025 SCS1303 Compiler Design 9


DFA (Deterministic Finite Automata)
• DFA is a special case of NFA in which:
• for each state s and input symbol a ,there is at
most one edge labeled a leaving s.
i.e. only one path for specific input symbol from
the current state to the next state.
• no state has an ε-transition i.e. a transition on
input a .
• Each entry in the transition table of a DFA is a single
state.

March 2, 2025 SCS1303 Compiler Design 10


Formal Definition of DFA

March 2, 2025 SCS1303 Compiler Design 11


DFA vs. NFA
DFA NFA
DFA can be understood as one Multiple small machines
machine. computing at the same time.
Difficult to construct - Complex Easier to construct.
structure.
Rejects the string if not terminated Rejects only after multiple
at the accepting state. checks.
Less execution time on input string. More execution time.

All DFA are derived from NFA. Not all NFA are DFA.
DFA requires more space. Requires less Space.
The next possible state is clearly Ambiguity occurs.
set.

March 2, 2025 SCS1303 Compiler Design 12


Finite Automata Notations
• A state

• Start State

• Accepting State

• A transition label

March 2, 2025 SCS1303 Compiler Design 13


Conversion Process
Regular Expression Thompson’s
Construction
Method
NFA

DFA

Minimized DFA

March 2, 2025 SCS1303 Compiler Design 14


Regular Expressions to NFA
(Thompson’s Construction Method)
• For each kind of RE, define an NFA.
• Input: A Regular Expression r over an alphabet ∑
• Output: An NFA N accepting L(r)
• Method:
start ε
Step 1: For  i f

The NFA recognizes {}

Step 2: For a in ∑ start a


i f

The NFA recognizes {a}

March 2, 2025 SCS1303 Compiler Design 15


Thompson’s Construction Method
Step 3: RE=a |b
a
1 2
ε ε
start
0
5
ε ε
3 4
The NFA recognizes {a,b} b

Step 4: RE=ab

start a b a ε
OR start
0 1 2 0 1 2 b 3

The NFA recognizes {ab}


March 2, 2025 SCS1303 Compiler Design 16
Thompson’s Construction Method
Step 5: RE=a* ε

start ε a ε
0 1 2 3

The NFA recognizes {ε,a,aa,aaa,.......}

Step 6: RE= a+
= a.a*
Follow step 4 for construction.

March 2, 2025 SCS1303 Compiler Design 17


March 2, 2025 SCS1303 Compiler Design 18
Problem 1
• Construct Non deterministic Finite Automata for the
following regular expression using Thompson’s
Construction method.

a. (a|b)*abb
b. (a|b)*
c. (a*|b*)*
d. ((ε|a)b*)*
e. (0|123)*

March 2, 2025 SCS1303 Compiler Design 19


Example 1
• Decomposition of (a|b)*abb
r11 start a
0 1
r9
r10 start b
r7 2 3
r8
r5 b
r6 a
b 1 2
r4 * ε ε
a start
( r3 ) 0
5
r1 r2 ε ε
| 3 4
b
a b

March 2, 2025 SCS1303 Compiler Design 20


Example 1(contd.)
• Decomposition of (a|b)*abb
r11
r9 ε
r10
r7 a
r8 2 3
r5 b ε
ε
0ε 1
start
r6 b ε
r4 6 7
*
a ε 4 5 ε
( r3 ) b

r1 | r2 ε
a b

March 2, 2025 SCS1303 Compiler Design 21


Example 1(contd.)
• Decomposition of (a|b)*abb

r11
r9 ε
r10
r7 a
r8 2 3
r5 b ε
ε
0ε 1
start a
r6 b ε 8
r4 6 7
*
a ε 4 5 ε
( r3 ) b

r1 | r2 ε
a b

March 2, 2025 SCS1303 Compiler Design 22


NFA for (a|b)*abb

a
2 3
ε
ε
start 0 ε 1
6 ε 7 a 8 b 9 b 10

ε
4 5 ε
b

March 2, 2025 SCS1303 Compiler Design 23


Assignment
• Construct Non deterministic Finite Automata for the
following regular expression using Thompson’s
Construction method.

a. (a|b)*abb
b. (a|b)*
c. aa*|bb*
d. ((ε|a)b*)*
e. (0|123)*

March 2, 2025 SCS1303 Compiler Design 24


THANK YOU

March 2, 2025 SCS1303 Compiler Design 25

You might also like