Unit 3-Theory of Computation
Unit 3-Theory of Computation
TY BTech
Course Outcomes:
• After completion of this course students will be able:
• To construct finite state machines to solve problems in computing.
• To write mathematical expressions and syntax verification for the formal languages.
• To construct and analyze Push Down Automata and Turing Machine for formal
languages.
• To express the understanding of decidability and complexity.
Text Books & Reference Books
• Text Books
• John C. Martin, Introduction to Language and Theory of Computation, TMH,
3rdEdition, ISBN: 978-0-07-066048-9.
• Vivek Kulkarni, Theory of Computation, Oxford University Press, ISBN-13: 978-0-
19-808458-7.
• Reference Books
• K.L.P Mishra,N. Chandrasekaran,Theory of Computer Science (Automata, Languages
and Computation), Prentice Hall India, 2nd Edition.
• Michael Sipser, Introduction to the Theory of Computation,CENGAGE Learning, 3rd
Edition, ISBN:13:978-81-315-2529-6.
• Daniel Cohen, Introduction to Computer Theory, Wiley India, 2nd Edition, ISBN:
9788126513345.
• Kavi Mahesh, Theory of Computation: A Problem Solving Approach, 1st Edition,
Wiley-India, ISBN: 978-81-265-3311-4.
Unit III – CFG & PDA
CFG
Formal definition of Grammar, Chomsky Hierarchy, CFG : Formal
definition of CFG, Derivations, Parse Tree, Ambiguity in grammars and
languages, Language Specification using CFG, Normal Forms: Chomsky
Normal Form and Greibach Normal Form. Closure properties of CFL.
5
Introduction
• Finite Automata accept all regular languages and only regular languages
- {anbn : n = 0, 1, 2, …}
- {w : w a is palindrome}
Context-Free Languages
Regular Languages
Formal Definition of a Grammar
Grammar:
Set of
variables
Set of Start Set of
terminal variable productions
symbols
Language for the Grammar
Grammar:
S→aSb
S→є
Language of the grammar:
L = {a b : n 0}
n n
A Convenient Notation
We write:
for zero or more derivation steps
Instead of:
Generalizing:
w1 w2 w3 wn
Trivially:
Formal Definition-CFG
productions
P={S→aSb,
S→є}
start variable
variables
terminals
Context-Free Language
A language is context-free
if there is a context-free grammar
with
Context-Free Language-Example 1
is a context-free language
since context-free grammar :
{S aSb | є}
generates
Context-Free Language – Example 2
Context-free grammar :
{S aSa | bSb | є}
Example derivations:
S AB A aaA|ϵ B Bb|ϵ
Leftmost and Rightmost Derivation
Consider the following example grammar
with 5 productions:
S AB A aaA|ϵ B Bb|ϵ
Leftmost derivation: for the string aab
Rightmost derivation:
Give same
derivation tree
ϵ ϵ
Context Free Language for a CFG
Find the CFL generated for the given CFG.
1. S aSb | ab
L(G)={anbn | n>=1}
2. S aB|bA
A a|aS|bAA
B b|bS|aBB
26
Removal of Useless symbol
Ex.1 Consider the following grammar:
G={ (S,A),(1,0),P,S}
Where P consists of the following productions:
S🡪 1 0|0 S 1|1 S 0|A|S S
Simplify the grammar by removing the useless symbols if any.
Remove S🡪 A, as A is useless symbol.
27
Removal of Useless symbol
Ex.2 Consider the following grammar:
G={ (S,A,B),(a),P,S}
Where P consists of the following productions:
S🡪 A B | a
A🡪 a
Simplify the grammar by removing the useless symbols if any.
B is useless, Remove B
S🡪 A | a
A🡪 a
28
Removal of Unit Production
• A production rule of the form A🡪 B where A and B are both
non-terminals is called a unit production.
• All the other productions (including ∈ productions) are non-unit
productions.
Ex.1 Consider the following grammar:
G={ (A,B),(a,b),P,A}
Where P consists of :
A🡪 B, B🡪 a | b
Simplify the grammar by removing the unit productions if any.
On eliminating unit production A🡪 B
A🡪 a|b
29
Removal of Unit Production
Ex.2Consider the following grammar:
S🡪 a | X b | a Y a | b | a a
X🡪 Y
Y🡪 b|X
Simplify the grammar by removing the unit productions if any.
Simplified grammar is:
S🡪 a | Yb | aYa | b | aa
Y🡪 b
30
Removal of ∈ - Production
A production of the form A🡪 ∈ where A is non-terminal is known
as ∈ - Production.
31
Removal of ∈ - Production
Ex.2 Consider the following grammar,
S🡪 a|Xb|aYa
X🡪 Y | ∈
Y🡪 b | X
Simplify the grammar by eliminating ∈ - Productions if any.
Simplified grammar is:
S 🡪 a | X b | a Y a | b | aa
X🡪 Y
Y🡪 b | X
32
Chomsky Hierarchy
•Type 2 grammar:
It is also called as context free grammar.
A→α where A is NT and α is sentential form.
Start symbol of the Grammar can also appear on RHS.
Recognized by PDA(Pushdown Automata)
Chomsky Hierarchy
• Type 1 grammar:
It is context sensitive grammar or context dependent.
1. α → β where length of β is at least as much as the length of α
except S→ .
2. The rule S→ is allowed only if start symbol S does not appear
on RHS.
3. Productions are of the form
α1Aα2 →α1 β α2 (β ≠ )
Recognized by TM(Turing Machine).
35
Chomsky Hierarchy
•Type 0 grammar:
It is unrestricted grammar that is no restriction on production.
α →β (α ≠ )
Recognized by TM(Turing machines)
These languages are known as Recursively Enumerable
Languages.
36
Normal Forms
A→ a, where A is NT and a is T
A → BC where A,B and C are NTs
S → is in G if belongs to L(G).
When is in L(G),
we assume that S does not appear on the RHS of any production.
e.g. G is :
S→AB|
A→ a
B→b
Construction of G in CNF
1. S→ aAD 3. S→ABA
A→ aB| bAB A→ aA | ϵ
B→b B→ bB | ϵ
D→d
2. S→ aAbB
A→ aA | a
B→ bB | b
Greibach Normal Form
44
Push Down Automata
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
Pushdown Automaton - PDA
Input String
Stack
States
Initial Stack Symbol
Stack Stack
stack
top
head
1. q is the state
2. w is the remaining input
3. ɤ is the stack contents
we show the top of the stack at the left end of ɤ and
the bottom at the right end.
Such a triple is called an Instantaneous Description
or ID of a PDA.
δ(q, a, b) = (q, c)
stack
top Replac
e
δ(q, a, b) = (q, cb)
stack
top Pus
h
δ(q, a, b) = (q, ϵ)
stack
top Po
p
δ(q, a, b) = (q, b)
stack
top No
Change
A PDA Example
A =(Q,Σ, Г ,δ,q0,Z0,F)
Q={q0, q1, q2}, Σ={a,b}, Г={Z0, a}, F={q2}, δ as below
Let
A= (Q, Σ, Г , δ, q0, Z0, F)
Q={q0,q1},
Σ={a,b},
Г={a,Z0},
F={q1}
is a PDA.
δ (Transition Function) by Final State is
Move no State input stack Move
symbol
1 q0 a Z0 (q0,aZ0)
2 q0 a a (q0,aa)
3 q0 b a (q1, ∈)
4 q1 b a (q1,∈)
5 q1 ∈ Z0 (q1, Z0)
Let
A= (Q, Σ, Г , δ, q0, Z0, F) is a PDA.
where
Q={q0,q1,qf},
Σ={a,b,c},
Г={a,b,Z0},
F={qf}
δ (Transition Function) by Final State is
DPDA:
transition function is :
Q X Σ X Г 🡪 Q X Г*
e.g. δ(q,a,Z) is either empty or a singleton.
δ(q,a,Z) ≠ Ø
NPDA:
Q X Σ U {ϵ } X Г 🡪 finite subsets of Q X Г*
e.g. δ(q,a,Z) = {(p1,ɤ1) ,(p2, ɤ2)…..(pm, ɤm)}
DPDA and NPDA
NPDA and DPDA
• The NPDA can accept any CFL, while DPDA is a special case of
NPDA that accepts only a subset of the CFLs accepted by the
NPDA.
Let
A= (Q, Σ, Г , δ, q0, Z0, F) is a PDA.
where
Q={q0,q1,qf},
Σ={a,b},
Г={a,b,Z0},
F={qf}
NPDA to accept language of all palindrome strings
We construct PDA A as
A = (Q, Σ, Г , δ, q, Z0,F)
Q = {q}
Σ = {0,1} Move State input stack symbol Move
Г = {S, B, 0, 1} no
Z0= S 1 q ∈ S {(q,0BB)}
F=Ф 2 q ∈ B {(q,0S), (q,1S), (q,0)}
3 q 0 0 {(q, ∈)}
4 q 1 1 {(q,∈)}
CFG to PDA
1 .Construct a PDA for the CFG
S🡪 0BB
B🡪 0S | 1S |0
Test whether 0104 is in N(A).
78