0% found this document useful (0 votes)
60 views64 pages

Toc (Unit-I)

The document provides an overview of the Theory of Computation, detailing concepts such as computation, algorithms, and automata, including finite automata (FA), deterministic finite automata (DFA), and non-deterministic finite automata (NFA). It outlines the course syllabus, covering topics like regular languages, context-free grammars, pushdown automata, and Turing machines, along with their applications in compiler design and pattern matching. Additionally, it discusses the formal definitions, representations, and conversions between different types of automata.

Uploaded by

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

Toc (Unit-I)

The document provides an overview of the Theory of Computation, detailing concepts such as computation, algorithms, and automata, including finite automata (FA), deterministic finite automata (DFA), and non-deterministic finite automata (NFA). It outlines the course syllabus, covering topics like regular languages, context-free grammars, pushdown automata, and Turing machines, along with their applications in compiler design and pattern matching. Additionally, it discusses the formal definitions, representations, and conversions between different types of automata.

Uploaded by

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

Theory of

Computation
UNIT-I
BY
V.SRAVANTHI
Theory of Computation
 Theory: It includes capabilities ,how a
problem can solved and limitations of
machine

 Computation : It can be defined as finding


solution to problem from the given inputs by
means of an algorithm (or)
It is processing  input towards output
Theory of Computation

 TOC is a branch of computer science that


deals with how efficiently a problem can be
solved on a model of computation using an
algorithm
 Theory of Computation is also called as

Automata Theory
 In general Every machine there will be low

level calculation called Automata (or)


Model (or) Abstract Machine
Algorithm : Step by step procedure to solve a
given problem in finite number of steps by
accepting a set of input and produce desired
output.

I/P Rules O/P

Abstract machine : Conceptual or theoretical


model of computer
Example 1: Switch
Device Remembers
whether it is on
Pus
or off state
h
Star
OFF ON
t

Pus
h
Traffic Light
Example 2 : 2 Level
Elevator
0 1
1

Star
G F
t

0
Example 3 :Word Search

t h e n
t th th then
e
Automata
 An automaton (Automata in plural) is an
abstract self-propelled machine which
follows a predetermined sequence of
operations automatically.
 Automata derived from Greek word ”Acting

of one’s own will”


 An automaton with a finite number of states

is named a Finite Automaton (FA) or Finite


State Machine (FSM) and Infinite number of
steps is named as Non Finite Automata(NFA)
Automata

FA Without O/P FA With O/P

DFA Moore
and
NFA Mealy
NFA with Epsilon Machines
SYLLABUS
UNIT - I
Fundamentals : Strings, Alphabet, Language, Operations, Finite state
machine, definitions, finite automaton model, acceptance of strings, and
languages, deterministic finite automaton (DFA) and non-deterministic
finite automaton (NFA), transition diagrams and Language recognizers.
Finite Automata: NFA to DFA conversion, minimization of FSM,
equivalence between two FSM's, Finite Automata with output- Moore and
Mealy machines.
UNIT - II
Regular Languages: Regular sets, regular expressions, identity rules,
Constructing finite Automata for a given regular expressions, Conversion
of Finite Automata to Regular expressions.
Context Free Grammars: Definition, Ambiguity in context free grammars.
Simplification of Context Free Grammars. Chomsky normal form,
Greibach normal form, Enumeration of properties of CFLs (proofs
omitted), Chomsky's hierarchy of languages.
UNIT - III
Push Down Automata: Push down automata, definition, model, acceptance
of CFL, by final state and by empty stack.
Turing Machine: Turing Machine, definition, model, design of TM, counter
machine, types of Turing machines (proofs not required).
Overview of Compilation: Phases of compilation-lexical analysis, regular
grammar and regular expression for common programming language
features, Pass and phases of translation, interpretation, bootstrapping, data
structures in compilation
UNIT - IV
Top Down Parsing: Back Tracking, LL(1), Recursive Descent Parsing,
Predictive Parsing, Pre-processing steps required for predictive parsing.
Bottom Up Parsing: Shift Reduce Parsing, LR and LALR Parsing, Error
Recovery in Parsing, Handling Ambiguous grammar.
Semantic Analysis: Intermediate forms of source programs-abstract syntax
tree, polish notation and three address codes, conversion of popular
programming languages language constructs into intermediate code forms.
UNIT - V
Code Optimization: Consideration for optimization, scope of
optimization, loop optimization, frequency reduction folding,
DAG representation, reduction in strengths.
Object Code Generation: Object code forms, machine
dependent code optimization, register allocation and
assignment generic code generation algorithms.

TEXT BOOK(S):
1. Introduction to Automata Theory Languages and
Computation, Hopcroft H.E. and Ullman J. D. Pearson
Education, 2002.
2. Principles of Compiler Design, A.V. Aho, J.D. Ullman,
Pearson Education, 1977.
Course Outcomes (COs)
At the end of the course, student would be able to
1. Design mathematical models that recognize different of
languages and carryout computations.
2.Build language generators.
3.Explain the functioning of phases of compilation process and
data structures used in this process.
4.Perform lexical and syntax analysis for high level programs.
5.Generate intermediate code and optimize to prepare the code for
absolute machine code generation.
6.Explain how machine code generation algorithm works and how
compiler efficiently uses various system resources in machine
code generation.
Applications
 Design of Compiler
 Pattern Matching
 Spell Checker
 Cryptography
 Quantum calculation
 Correcting errors in code etc.
UNIT-I
(Fundamentals and
Finite Automata)
Finite Automata

 It is an abstract Computing device.


 Finite automata are used to recognize patterns.
 It takes the string of symbol as input and changes
its state accordingly. When the desired symbol is
found, then the transition occurs.
 At the time of transition, the automata can either
move to the next state or stay in the same state.
 Finite automata have two states, Accept
state or Reject state. When the input string is
processed successfully, and the automata
reached its final state, then it will accept.
Finite Automata Model:

 Finite automata can be represented by input


tape and finite control.
 Input tape: It is a linear tape having some

number of cells. Each input symbol is


placed in each cell.
 Finite control: The finite control decides

the next state on receiving particular input


from input tape. The tape reader reads the
cells one by one from left to right, and at a
time only one input symbol is read.
Formal Definition of FA

A finite automaton is a collection of 5-tuple


(Q, ∑, δ, q0, F), where:
 Q: finite set of states
 ∑: finite set of the input symbol
 q0: initial state
 F: final state
 δ: Transition function
FA Representations
1. Graphical(Transition Diagrams (or)
State transition diagram (or) Graph)

2. Tabular(Transition Table)

3. Mathematical( Transition Function (0r)


Mapping Function)
Transition Diagram

 A transition diagram or state transition


diagram is a directed graph which can be
constructed as follows:
 There is a node for each state in Q, which is
represented by the circle.
 There is a directed edge from node q to node
p labelled a if δ(q, a) = p.
 In the start state, there is an arrow with no
source.
 Accepting states or final states are indicating
by a double circle.
Some Notations that are used in the
transition diagram:
Example

q0 is initial state
q1 is intermediate state
q2 is finial state
Transition Table
 The transition table is basically a tabular
representation of the transition function. It takes
two arguments (a state and a symbol) and returns
a state (the "next state").
 A transition table is represented by the following
things:
 Columns correspond to input symbols.
 Rows correspond to states.
 Entries correspond to the next state.
 The start state is denoted by an arrow with no
source.
 The accept state is denoted by a star.
Example

Present State Next state for Input 0 Next State of Input 1

→q0 q1 q2

q1 q0 q2

*q2 q2 q2
Language recognizers
 A device that accepts valid strings is called the
Language recognizer. For example finite automata
are language recognizer.
 Recognizers are Machines. The Machines take a
string as input. The Machines will accept the input if
when run, the Machine stops at an accept state.
Otherwise the input is rejected.
 If a Machine M recognizes all strings in Language L,
and accepts input provided by a given string S, M is
said to accept S. Otherwise M is said to reject S. S is
in L if and only if M accepts S.
 Another example for language recognizer are PDA’s
(Push Down Automaton)
Automata

FA Without O/P FA With O/P

DFA Moore
and
NFA Mealy
NFA with Epsilon Machines
DFA (Deterministic finite automata)
 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 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.
Formal Definition of DFA

A DFA is a collection of 5-tuples same as we


described in the definition of FA.
 Q: finite set of states
 ∑: finite set of the input symbol
 q0: initial state
 F: final state
 δ: Transition function
 Transition function can be defined as:
 δ: Q x ∑→Q ** Examples: **
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.
 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.
Formal definition of NFA:

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

** Examples: **
Difference between DFA and
NFA
NFA to DFA conversion
There are two ways to convert NFA to DFA
1.Subset Construction
2. Lazy Evaluation
Steps:
Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M). There
should be equivalent DFA denoted by M' = (Q', ∑', q0', δ', F') such that
L(M) = L(M').
Steps for converting NFA to DFA:
Step 1: Initially Q' = ϕ
Step 2: Add q0 of NFA to Q'. Then find the transitions from this start
state.
Step 3: In Q', find the possible set of states for each input symbol. If this
set of states is not in Q', then add it to Q'.
Step 4: In DFA, the final state will be all the states which contain F(final
states of NFA)

**Example**
NFA (Non-Deterministic finite automata) with
Episilon

 An epsilon nondeterministic finite


automaton (NFA) has null or epsilon
transitions from one state to another.
 Epsilon NFA is also called a null NFA or

an NFA lambda.
Epsilon (ε) - closure

 Epsilon closure for a given state X is a set of


states which can be reached from the states
X with only (null) or E moves including the
state X itself.
 In other words, £-closure for a state can be

obtained by union operation of the £-closure


of the states which can be reached from X
with a single E move in a recursive manner.
Example-1
 Consider the following figure of NFA with ε move −

ε closure are as follows −


 E closure( A) : {A, B,C}

 E closure( B) :{B,C}

 E closure( C) : {C}

**Example**
Conversion of Epsilon-NFA to NFA

Step-1: Consider the two vertexes having the epsilon move. Here
in Fig.1 we have vertex v1 and vertex v2 having epsilon move from v1
to v2.
Step-2: Now find all the moves to any other vertex that start from
vertex v2 (other than the epsilon move that is considering). After
finding the moves, duplicate all the moves that start from vertex v2,
with the same input to start from vertex v1 and remove the epsilon
move from vertex v1 to vertex v2.
Step-3: See that if the vertex v1 is a start state or not. If vertex v1 is a
start state, then we will also make vertex v2 as a start state. If vertex
v1 is not a start state, then there will not be any change.
Step-4: See that if the vertex v2 is a final state or not. If vertex v2 is a
final state, then we will also make vertex v1 as a final state. If vertex v2
is not a final state, then there will not be any change. Repeat the
steps(from step 1 to step 4) until all the epsilon moves are removed
from the NFA.
Example

Step 1:

Final step

Step-2:

:**Example**
Conversion from NFA with ε to
DFA
Step 1: We will take the ε-closure for the starting state of NFA as a
starting state of DFA.

Step 2: Find the states for each input symbol that can be traversed
from the present. That means the union of transition value and their
closures for each state of NFA present in the current state of DFA.

Step 3: If we found a new state, take it as current state and repeat


step 2.

Step 4: Repeat Step 2 and Step 3 until there is no new state present in
the transition table of DFA.

Step 5: Mark the states of DFA as a final state which contains the final
state of NFA.
**Examples**
Equivalence between two
FSM's
The two finite automata (FA) are said to be equivalent if both
the automata accept the same set of strings over an input set
Σ.
 Let M and M1 be the two FA’s and Σ be a set of input strings.
 Step 1 − Construct a transition table that has pairwise

entries (q, q1) where q ∈ M and q1 ∈ M1 for each input


symbol.
 Step 2 − If we get in a pair as one final state and other

non-final state then we terminate the construction of


transition table declaring that two FA’s are not equivalent
 Step 3 − The construction of the transition table gets

terminated when there is no new pair appearing in the


transition table.
**Examples**
Minimization of FSM
Minimization of DFA means reducing the
number of states from given FA. In Two ways
we can do Minimization of FSM
1. Equivalence Method (or) Partition Method
2. Table Filling Method (or) Myhill-neurode
theoram
Equivalence Method:
Step 1: Remove all the states that are unreachable from the
initial state via any set of the transition of DFA.
Step 2: Draw the transition table for all pair of states.
Step 3: Now split the transition table into two tables T1 and T2.
T1 contains all final states, and T2 contains non-final states.
Step 4: Find similar rows from T1 such that:
1. δ (q, a) = p
2. δ (r, a) = p
That means, find the two states which have the same value of a
and b and remove one of them.
Step 5: Repeat step 3 until we find no similar rows available in
the transition table T1.
Step 6: Repeat step 3 and step 4 for table T2 also.
Step 7: Now combine the reduced T1 and T2 tables. The
combined transition table is the transition table of minimized
DFA.
**Examples**
Table Filling Method :

Myhill-Nerode Theorem
Steps :
 Create the pairs of all the states involved in the

given DFA.
 Mark all the pairs (Q ,Q ) such a that Q is Final
a b a
state and Qb is Non-Final State.
 If there is any unmarked pair (Qa,Qb) such a that

δ(Qa,x) and δ(Qb,x) is marked, then mark


(Qa,Qb). Here x is a input symbol. Repeat this
step until no more marking can be made.
 Combine all the unmarked pairs and make them a

single state in the minimized DFA.


**Examples**
Finite automata with
output

Finite automata may have outputs corresponding to each transition. There


are two types of finite state machines that generate output −
 Mealy Machine

 Moore machine
Moore machine
 Moore machine is a finite state machine in which
the next state is decided by the current state
and current input symbol. The output symbol at
a given time depends only on the present state
of the machine. Moore machine can be
described by 6 tuples (Q, q0, ∑, O, δ, λ) where,
 Q: finite set of states
 q0: initial state of machine
 ∑: finite set of input symbols
 O: output alphabet
 δ: transition function where Q × ∑ → Q
 λ: output function where Q → O
Example 1:
 The state diagram and Transition Table for Moore
Machine is

The output is represented with each input state separated by /.


The output length is greater than input by 1.
 Input: 010

 Transition: δ (q0,0) => δ(q1,1) => δ(q1,0) => q2

 Output: 1110(1 for q0, 1 for q1, again 1 for q1, 0 for q2)
2. Design a Moore machine to generate
1's complement of a given binary number .
To generate 1's complement of a given binary
number the simple logic is that if the input is
0 then the output will be 1 and if the input is
1 then the output will be 0. That means there
are three states. One state is start state. The
second state is for taking 0's as input and
produces output as 1. The third state is for
taking 1's as input and producing output as 0.
 Hence the Moore machine will be,
**Examples**
Mealy Machine

 A Mealy machine is a machine in which output symbol


depends upon the present input symbol and present
state of the machine. In the Mealy machine, the output
is represented with each input symbol for each state
separated by /.
The Mealy machine can be described by 6 tuples (Q, q0,
∑, O, δ, λ') where
 Q: finite set of states
 q0: initial state of machine
 ∑: finite set of input alphabet
 O: output alphabet
 δ: transition function where Q × ∑ → Q
 λ': output function where Q × ∑ →O

**Examples**
Conversion from Mealy machine to
Moore Machine
In Moore machine, the output is associated with every state,
and in Mealy machine, the output is given along the edge with
input symbol. To convert Moore machine to Mealy machine,
state output symbols are distributed to input symbol paths. But
while converting the Mealy machine to Moore machine, we will
create a separate state for every new output symbol and
according to incoming and outgoing edges are distributed.
The following steps are used for converting Mealy machine to
the Moore machine:
 Step 1: For each state(Qi), calculate the number of different

outputs that are available in the transition table of the Mealy


machine.
 Step 2: Copy state Qi, if all the outputs of Qi are the same.

Break qi into n states as Qin, if it has n distinct outputs where


n = 0, 1, 2....
 Step 3: If the output of initial state is 0, insert a new initial

state at the starting which gives 1 output.


**Examples**
Conversion from Moore machine to
Mealy Machine

Let M = (Q, ∑, δ, λ, q0) be a Moore machine.


The equivalent Mealy machine can be
represented by M' = (Q, ∑, δ, λ', q0). The
output function λ' can be obtained as:
λ' (q, a) = λ(δ(q, a))
Example 1:
The equivalent Mealy machine can be obtained
as follows:
λ' (q0, a) = λ(δ(q0, a))
= λ(q0)
=0
λ' (q0, b) = λ(δ(q0, b))
= λ(q1)
=1
λ' (q1, a) = λ(δ(q1, a))
= λ(q0)
=0

λ' (q1, b) = λ(δ(q1, b))


= λ(q1)
=1
Difference between Moore and Mealy machine

You might also like