Compiler Construction
CSC441
Finite state machine
Finite state machine is used to recognize patterns.
Finite automata machine takes the string of symbol as
input and changes its state accordingly.
In the input, when a desired symbol is found then the
transition occurs.
While transition, the automata can either move to the
next state or stay in the same state.
Finite state machine
FA has two states: accept state or reject state.
When the input string is successfully processed and
the automata reached its final state then it will
accept.
Finite state machine
A finite automata consists of following:
Q: finite set of states
∑: finite set of input symbol
q0: initial state
F: final state
δ: Transition function
Finite state machine
Transition function can be define as
Finite state machine
FA is characterized into two ways:
1. DFA (Deterministic Finite Automata)
2. NDFA (Non-deterministic Finite Automata)
DFA
DFA refers to deterministic finite automata.
Deterministic refers to the uniqueness of the computation.
The finite automata are called deterministic finite
automata if the machine is read an input string one
symbol at a time.
In DFA, there is only one path for specific input from the
current state to the next state.
DFA
DFA does not accept the null move, i.e., the DFA
cannot change state without any input character.
DFA can contain multiple final states.
It is used in Lexical Analysis in Compiler.
DFA
In the following diagram, we can see that from state
q0 for input a, there is only one path which is going
to q1.
Similarly, from q0, there is only one path for input b
going to q2.
DFA
A DFA is a collection of 5-tuples same as we
described in the definition of FA.
DFA has five tuples {Q, ∑, q0, F, δ}
Q: set of all states
∑: finite set of input symbol where δ: Q x ∑ →Q
q0: initial state
F: final state
δ: Transition function
Graphical Representation of DFA
A DFA can be represented by digraphs called state
diagram.
In which:
The state is represented by vertices.
The arc labeled with an input character show the transitions.
The initial state is marked with an arrow.
The final state is denoted by a double circle.
Example # 01 DFA
Q = {q0, q1, q2}
∑ = {0, 1}
q0 = {q0}
F = {q2}
Example # 02 DFA
DFA with ∑ = {0, 1} accepts all starting with 0.
Example # 02 DFA
In the above diagram, we can see that on given 0 as
input to DFA in state q0 the DFA changes state to q1
and always go to final state q1 on starting input 0.
It can accept 00, 01, 000, 001....etc.
It can't accept any string which starts with 1,
because it will never go to final state on a string
starting with 1.
Example # 03 DFA
DFA with ∑ = {0, 1} accepts all ending with 0.
Example # 03 DFA
In the above diagram, we can see that on given 0 as input to
DFA in state q0, the DFA changes state to q1.
It can accept any string which ends with 0 like 00, 10, 110,
100....etc.
It can't accept any string which ends with 1, because it will
never go to the final state q1 on 1 input, so the string ending
with 1, will not be accepted or will be rejected.
NFA (Non-Deterministic finite
automata)
NFA stands for non-deterministic finite automata.
It is easy to construct an NFA than DFA for a given
regular language.
The finite automata are called NFA when there exist
many paths for specific input from the current state to
the next state.
NFA (Non-Deterministic finite
automata)
Every NFA is not DFA, but each NFA can be
translated into DFA.
NFA is defined in the same way as DFA but with the
following two exceptions, it contains multiple next
states, and it contains ε transition.
NFA (Non-Deterministic finite
automata)
In the following image, we can see that from state q0 for input
a, there are two next states q1 and q2, similarly, from q0 for input
b, the next states are q0 and q1.
Thus it is not fixed or determined that with a particular input
where to go next.
Hence this FA is called non-deterministic finite automata.
NFA (Non-Deterministic finite
automata)
NFA also has five states same as DFA, but with
different transition function, as shown follows:
δ: Q x ∑ →2Q
where,
Q: finite set of states
∑: finite set of the input symbol
q0: initial state
F: final state
δ: Transition function
Graphical Representation of an NFA
An NFA can be represented by digraphs called state
diagram.
In which:
The state is represented by vertices.
The arc labeled with an input character show the transitions.
The initial state is marked with an arrow.
The final state is denoted by the double circle.
Example # 01 NFA
Q = {q0, q1, q2}
∑ = {0, 1}
q0 = {q0}
F = {q2}
Example # 01 NFA
In the above diagram, we can see that when the current
state is q0, on input 0, the next state will be q0 or q1,
and on 1 input the next state will be q1.
When the current state is q1, on input 0 the next state
will be q2 and on 1 input, the next state will be q0.
When the current state is q2, on 0 input the next state is
q2, and on 1 input the next state will be q1 or q2.
Example # 02 NFA
NFA with ∑ = {0, 1} accepts all strings with 01.
Example # 03 NFA
NFA with ∑ = {0, 1} and accept all string of length
atleast 2.