Unit 3 TOC
Unit 3 TOC
SY BTech CSE-AIDS
Unit- III
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.
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
Gramma
r:
Set of
variable
s Set of Start Set of
termina variabl production
l e s
symbols
Language for the Grammar
Gramma
r: SaSb
Sє
Language of the
grammar:
A Convenient Notation
We write:
for zero or more derivation
Instead of:steps
Generalizing:
Trivially:
Formal Definition-CFG
production
s
P={SaSb,
Sє}
start
variable
terminal variable
s
s
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 :
{SaSb | є}
generates
Context-Free Language – Example 2
Context-free grammar :
{SaSa | bSb | є}
Example derivations:
Give same
derivation
tree
ϵ ϵ
Context Free Language for a CFG
Find the CFL generated for the given CFG.
1. SaSb | ab
L(G)={anbn | n>=1}
2. SaB|bA
Aa|aS|bAA
Bb|bS|aBB
A context-free grammar is
ambiguous
if there is a string which
has:
generates the
same
language
Unique
derivation tree
and leftmost
derivation
For the string
a+a*a
Simplification of Context Free Grammar
A CFG G can be simplified as:
1. Each variable and terminal of the CFG should appear in the
derivation if at least one word in the L(G).
2. There should not be any production of the form AB where A and
B are both non-terminals.
25
Removal of Useless symbol
Ex.1 Consider the following grammar:
G={ (S,A),(1,0),P,S}
Where P consists of the following productions:
S1 0|0 S 1|1 S 0|A|S S
Simplify the grammar by removing the useless symbols if any.
Remove SA, as A is useless symbol.
26
Removal of Useless symbol
Ex.2 Consider the following grammar:
G={ (S,A,B),(a),(1,0),P,S}
Where P consists of the following productions:
S A B | a
Aa
Simplify the grammar by removing the useless symbols if any.
B is useless, Remove B
SA | a
A a
27
Removal of Unit Production
• A production rule of the form AB 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 :
AB, Ba | b
Simplify the grammar by removing the unit productions if any.
On eliminating unit production AB
Aa|b
28
Removal of Unit Production
Ex.2Consider the following grammar:
S a | X b | a Y a | b | a a
X Y
Yb|X
Simplify the grammar by removing the unit productions if any.
Simplified grammar is:
Sa | Yb | aYa | b | aa
Y b
29
Removal of ∈ - Production
A production of the form A ∈ where A is non-terminal is known as
∈ - Production.
30
Removal of ∈ - Production
Ex.2 Consider the following grammar,
S a | X b | a Y a
XY | ∈
Yb | 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
31
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).
34
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.
35
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 :
SAB| ∈
A a
Bb
Construction of G in CNF
previous method.
Let the G is (V,T,P,S)
Step 2. Elimination of terminals on RHS.
Step 3. Restricting the number of variables on RHS.
Reduce to CNF
1. Convert to CNF, SaSa | bSb | a | b | aa | bb
Adding Aa and Bb we can rewrite the grammar as
SASA|BSB|a|b|AA|BB
Aa, Bb
Only SASA and SBSB are not in CNF
For SASA we can write
SAR1, R1 SA
For SBSB we can write
SBR2, R2SB
Grammar in CNF is
SAR1|BR2|a|b|AA|BB
Aa, Bb
R1SA, R2SB
Reduce to CNF
2. Convert to CNF,
SbA|aB, AbAA|aS|a, BaBB|bS|b
Add R1a, R2 b
Rewriting the grammar,
S R2 A| R1 B,
A R2 AA| R1 S|a,
BR1 BB| R2 S|b
R1a, R2b
AR2 AA and B R1 BB are not in CNF
Converted grammar in CNF is
SR2 A| R1 B,
AR2 R3| R1 S|a, R3AA
BR1 R4 | R2 S|b, R4 BB
Greibach Normal Form
42
Push Down Automata
Context-Free Languages
Context- Pushdow
Free n
Grammars Automat
a stac
k
automat
on
Pushdown Automaton - PDA
Input
String
Stac
k
States
Initial Stack Symbol
Stac Stac
k k
stac
to
k
p
head
botto special
mAppears
symbol
at time
0
Formal definition of PDA
The PDA is as:
A =(Q,Σ, Г ,δ,q0,Z0,F)
Where
Q : A finite set of states
Σ : A finite set of input symbols
Г : A finite stack alphabet or pushdown symbols
δ : the transition function Q X(Σ U {ϵ } ) X Г to the set of finite
subsets of Q X Г*
q0: the start state
Z0 : the start symbol(pushdown symbol)
F : the set of accepting state or final states
Transition Function
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)
stac
k to Repla
p ce
δ(q, a, b) = (q, cb)
stac
k to Pus
p h
δ(q, a, b) = (q, ϵ)
stac
k to Po
p p
δ(q, a, b) = (q, b)
stac
k to No
p Change
A PDA Example
A =(Q,Σ, Г ,δ,q0,Z0,F)
Q={q0, q1, q2}, Σ={a,b}, Г={Z0, a}, F={q2}, δ as below
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 (q2, Z0)
Acceptance by PDA using final state
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 (q2, Z0)
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
66
Deterministic and non-deterministic PDA
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)}
2. Now, We will derive forward, only building productions for variables that can
be derived.
Let δ(q0,a,X) = (r, y1y2y3y4…yk)
Then (q0 X rk) a [r y1 r1], [r1 y2 r2], [r2 y3 r3], ….. [rk-1 yk rk],
For all states r1,r2,……rk
δ(q0,a,X) = (r, ∈) then [q0 X r] a
80