Sunteți pe pagina 1din 63

Compiler Construction

6001310-3
Lecture 03 – Finite Automata
Abdelrahman Osman
aamosman@uqu.edu.sa

Web site: https://uqu.edu.sa/aamosman/10062


Introduction
• Finite automata is a state machine that takes a string of symbols as
input and changes its state accordingly.
• Finite automata is a recognizer for regular expressions.
• When a regular expression string is fed into finite automata, it
changes its state for each literal.
• If the input string is successfully processed and the automata reaches
its final state, it is accepted, i.e., the string just fed was said to be a
valid token of the language in hand.
The mathematical model of finite automata
consists of:
• Finite set of states (Q)
• Finite set of input symbols (Σ)
• One Start state (q0)
• Set of final states (qf)
• Transition function (δ)
• The transition function (δ) maps the finite set of state (Q) to a finite
set of input symbols (Σ), Q × Σ ➔ Q
Finite Automata Construction
• Let L(r) be a regular language recognized by some finite automata (FA).
• States : States of FA are represented by circles. State names are written inside circles.
• Start state : The state from where the automata starts, is known as the start state.
Start state has an arrow pointed towards it.
• Intermediate states : All intermediate states have at least two arrows; one pointing
to and another pointing out from them.
• Final state : If the input string is successfully parsed, the automata is expected to be
in this state. Final state is represented by double circles. It may have any odd number
of arrows pointing to it and even number of arrows pointing out from it. The number
of odd arrows are one greater than even, i.e. odd = even+1.
• Transition : The transition from one state to another state happens when a desired
symbol in the input is found. Upon transition, automata can either move to the next
state or stay in the same state. Movement from one state to another is shown as a
directed arrow, where the arrows points to the destination state. If automata stays
on the same state, an arrow pointing from a state to itself is drawn.
Example
• We assume FA accepts any three digit binary value ending in digit 1.
FA = {Q(q0, qf), Σ(0,1), q0, qf, δ}
Nondeterministic Finite Automaton (NFA)
• It is not deterministic since in state p reading a 1 can lead to p or to q.
It is not deterministic since in state q0 reading a can lead to q1 or q3

Alphabet =
{a}

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 9


Alphabet =
{a}

a
Two choices
q1 q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 10


Alphabet =
{a}

a
Two choices
q1 q2 No transition

a
q0
a
q3 No transition

Fall 2006 Costas Busch - RPI 11


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 12


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 13


First Choice

a a
All input is consumed

q1 a q2 “accept”

a
q0
a
q3

Fall 2006 Costas Busch - RPI 14


Second Choice

a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 15


Second Choice

a a
Input cannot be consumed

q1 a q2
a
Automaton Halts
q0
a
q3 “reject”

Fall 2006 Costas Busch - RPI 16


Deterministic Finite Automaton (DFA)
Finite State Automata (FSA)
• FSAs recognize the regular languages represented by regular
expressions
baa+!
a !
b a
a

q0 q1 q2 q3 q4
Formally
• FSA is a 5-tuple consisting of
• Q: set of states {q0,q1,q2,q3,q4}
• : an alphabet of symbols {a,b,!}
• q0: A start state
• F: a set of final states in Q {q4}
• (q,i): a transition function mapping Q x  to Q

a
b a a !

q0 q1 q2 q3 q4
• FSA recognizes (accepts) strings of a regular language
• baa!
• baaa!
a
• baaaa! b a a !
• …
q0 q1 q2 q3 q4
• Tape Input: a rejected input

a b a ! b
State Transition Table for SheepTalk
baa! Input
baaa! State
baaaa! b a !
baaaaa!
...
0 1 Ø Ø
1 Ø 2 Ø
a 2 Ø 3 Ø
b a a !
3 Ø 3 4
q0 q1 q2 q3 q4
4 Ø Ø Ø
A finite-state automaton for talking sheep
Non-Deterministic FSAs for SheepTalk

b a a a !

q0 q1 q2 q3 q4

b a a !

q0 q1 q2 q3 q4

Finite Accepter
Input

String
Output
“Accept”
Finite
or
Automata
“Reject”
Transition Graph
abba -Finite Accepter a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4

initial final
state state
transition
state “accept”
Initial Configuration
Input String
•a b b a
a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Reading the Input
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4

Output: “accept”
Rejection
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b
Output:
q5 “reject”
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Another Example
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b
Output: “accept”

q0 b q1 a, b q2
Rejection
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2

Output: “reject”
Formalities
• Deterministic Finite Accepter (DFA)
M  Q, ,  , q0 , F 
Q : set of states
 : input alphabet
 : transition function
q0 : initial state

F : set of final states


About Alphabets
• Alphabets means we need a finite set of symbols
in the input.
• These symbols can and will stand for bigger
objects that can have internal structure.
Input Alphabet 
•   a, b
a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Set of States Q

Q  q0 , q1, q2 , q3 , q4 , q5 
a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Initial State q0

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Set of Final States F
F  q4 
a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Transition Function 
 :Q  Q
a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 q0 , a   q1

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 q0 , b   q5

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 q2 , b   q3

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Transition Table for

symbols
•  a b
q0 q1 q5
q1 q5 q2
q2 q3
states

q5
a, b
q3 q4 q5
q4 q5 q5
q5
q5 q5 q5 a, b
b a a b
q0 a q1 b q2 b q3 a q4
Fall 2006 Costas Busch - RPI 56
Example:
•  *
q0 , ab   q2

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Fall 2006 Costas Busch - RPI 57
 q0 , abbbaa   q5
*

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4

Fall 2006 Costas Busch - RPI 58


 q1 , bba   q4
*

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4

Fall 2006 Costas Busch - RPI 59


Convert Regular Expression to Finite-State
Automaton

https://www.youtube.com/watch?v=GwsU2LPs85U
https://www.tutorialspoint.com/automata_theory/constructing_fa_from_re.htm
References
• [1] https://www.tutorialspoint.com/compiler_design/compiler_design_finite_automata.htm
• [2] Formal Languages and Automata Theory, CSE 4083/5210 ,
Florida Institute of Technology, Instructor: Debasis Mitra,
http://cs.fit.edu/~dmitra/FormaLang/Lectures/FiniteAutomata.ppt
• [3] cecs.wright.edu/~tkprasad/courses/cs466/L9FSA.ppt
• [4] www.cs.uky.edu/~mihail/CS216/lectures/FSAs.ppt
• [5] www.cs.rpi.edu/~moorthy/Courses/modcomp/slides/DFA.ppt
• [6] https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton