0% found this document useful (0 votes)
47 views204 pages

Module2 Theory of Computation

CFG: Formal Definition, Derivation and Syntax trees, Simplification Forms, Ambiguous Grammar, Properties of CFL, Normal Forms (CNF and GNF), Pushdown Automata: Definitions, Relationship between PDA and context free language, Decision Algorithms

Uploaded by

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

Module2 Theory of Computation

CFG: Formal Definition, Derivation and Syntax trees, Simplification Forms, Ambiguous Grammar, Properties of CFL, Normal Forms (CNF and GNF), Pushdown Automata: Definitions, Relationship between PDA and context free language, Decision Algorithms

Uploaded by

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

Context Free Grammars and Push Down

Automata
Module 2

Dr. Vibha Nehra


Department of Computer Science & Engineering

Amity School of Engineering and Technology


Amity University Uttar Pradesh, Noida

March 2, 2022
Learning Objectives and Outcomes

Sub-topic Learning Objective Learning Outcomes


CFG: Formal To understand the basic idea To obtain basic understanding
Definition of Context Free Grammar of CFG
Derivation and To understand the concept of To become aware of hierarchy
Syntax trees derivation and syntax trees and syntax of trees
Simplification To understand the process and To become familiar with
Forms necessicity of simplification of simplified CFG
CFG
Ambiguous To understand how a grammar To understand impact of
Grammar can be ambiguous Ambiguous grammar
Properties of To understand properties of To understand properties of
CFL CFL CFL
Normal Forms To become familiar different To understand different normal
(CNF and GNF) normal forms of CFG forms of CFG and their relevance
Pushdown Auto- To undertand the concept of To undertand the concept of
mata: Definitions Push Down Automata Push Down Automata
Relationship bet- To understand the relation To understand the relation
ween PDA and CFL between PDA and CFG between PDA and CFG
Decision To understand the concept of To become familiar with
Algorithms Decision Algorithms Decision Trees

Department of Computer Science and Engineering CFG and PDA March 2, 2022 2/48
Outline

1 CFG: Formal Definition

2 Derivation and Syntax Trees

3 Ambiguous Grammar

4 Simplification Forms

5 Properties of CFL

6 Normal Forms (CNF and GNF)

7 PushDown Automata: Introduction

8 Relationship between PDA and CFL

9 Decision Algorithms

Department of Computer Science and Engineering CFG and PDA March 2, 2022 3/48
CFG: Formal Definition

Context Free Grammar

Finite Automata accepts all regular languages.


Simple languages such as
an b n : n = 0, 1, 2, ....
w : w is a Palindrome
are not regular and thus no finite automata accepts them.
Context Free Languages are a larger class of languages that
encompasses all regular languages and many others including
above examples.
Languages generated by context free grammar are called
Context free languages.
Context free grammar are more expressive than finite
automata: If a language L is accepted by a finite automata,
then L can be generated by a context-free grammar, While
opposite is not true

Department of Computer Science and Engineering CFG and PDA March 2, 2022 4/48
CFG: Formal Definition

Context-Free Grammars

A Context-free grammar is a 4-tuple (V, Σ, P, S)


V is set of Variables.
Σ is set of terminals.
P is set of Productions.
S is the start symbol.
A Grammar G is Context free, if every production is of the
form A → α, where A ∈ VN and α ∈ (VN ∪ Σ)∗

Department of Computer Science and Engineering CFG and PDA March 2, 2022 5/48
CFG: Formal Definition

Example:CFG

Contruct a CFG generating all integers (with sign).

Department of Computer Science and Engineering CFG and PDA March 2, 2022 6/48
CFG: Formal Definition

Example:CFG

Contruct a CFG generating all integers (with sign).


Solution: Let G=(V, Σ, P, S)
where V={S,<sign>,<digit>,<integer> }
Σ = {0,1,2,3,.....9,+,-}
P consists of:
S →< sign >< integer >
< sign >→+\-
< integer >→< digit >< integer > \ < digit >
< digit >→ 0\1\2\....\

Department of Computer Science and Engineering CFG and PDA March 2, 2022 6/48
CFG: Formal Definition

Example:CFG

Contruct a CFG generating all integers (with sign).


Solution: Let G=(V, Σ, P, S)
where V={S,<sign>,<digit>,<integer> }
Σ = {0,1,2,3,.....9,+,-}
P consists of:
S →< sign >< integer >
< sign >→+\-
< integer >→< digit >< integer > \ < digit >
< digit >→ 0\1\2\....\
Derivation for -42
S→< sign >< integer >
=⇒ -< integer >
=⇒ -< digit >< integer >
=⇒ -4< digit >
=⇒ -42

Department of Computer Science and Engineering CFG and PDA March 2, 2022 6/48
Derivation and Syntax Trees

Derivation Trees
Trees are used for derivation of CFG.
Definition: A derivation tree (or a parse tree) for a CFG
G = (V , Σ, P, S) is a tree satisfying:
Every vertex has a label which is variable\terminal\∧ .
The root has label S.
The label of the internal vertex is a variable.
If vertices n1 , n2 , .....nk written with labels X1 , X2 , ....Xk are sons of
vertex ’n’ with label A, then A → X1 X2 ...Xk is a production in P.
A vertex ’n’ is a leaf if its label is a ∈ Σ or ∧; ’n’ is the only son of
its father if its label is ∧
Let G = ({S,A},{a,b},P,S) where P consists of
S → aAS\a\SS, A → SbA\ba

Department of Computer Science and Engineering CFG and PDA March 2, 2022 7/48
Derivation and Syntax Trees

Derivation Trees

Yield of Derivation Tree:is a


concatenationof labels of the
leaves without repetition in
the left to right ordering.
Example: aabaa Figure 1: Derivation Tree T

Subtree of a Derivation Tree


T is a tree:
whose root is some vertex
’v ’ of T ,
whose vertices are
descendants of ’v ’ together
with their labels. Figure 2: Sub tree of Tree T
whose edges are those
connecting the descendants
of v .
Figure 3: Sub tree of Tree T
Department of Computer Science and Engineering CFG and PDA March 2, 2022 8/48
Derivation and Syntax Trees

Derivation Trees
Theorem 1:

Let G=(V,Σ, P, S) be a CFG. Then, S =⇒ α if and only if there
is a derivation tree for G with yield α.
Example: Consider G whose productions are

S → aAS\a, A → SbA\SS\ba. Show that S =⇒ aabbaa and Construct a
derivation tree whose yield is aabbaa.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 9/48
Derivation and Syntax Trees

Derivation Trees
Theorem 1:

Let G=(V,Σ, P, S) be a CFG. Then, S =⇒ α if and only if there
is a derivation tree for G with yield α.
Example: Consider G whose productions are

S → aAS\a, A → SbA\SS\ba. Show that S =⇒ aabbaa and Construct a
derivation tree whose yield is aabbaa.
Case 1:
S =⇒ aAS =⇒ aSbAS =⇒ aabAS =⇒ a2 bbaS =⇒ aabbaa

Department of Computer Science and Engineering CFG and PDA March 2, 2022 9/48
Derivation and Syntax Trees

Derivation Trees
Theorem 1:

Let G=(V,Σ, P, S) be a CFG. Then, S =⇒ α if and only if there
is a derivation tree for G with yield α.
Example: Consider G whose productions are

S → aAS\a, A → SbA\SS\ba. Show that S =⇒ aabbaa and Construct a
derivation tree whose yield is aabbaa.
Case 1:
S =⇒ aAS =⇒ aSbAS =⇒ aabAS =⇒ a2 bbaS =⇒ aabbaa
Case 2:
S =⇒ aAS =⇒ aAa =⇒ aSbAa =⇒ aSbbaa =⇒ aabbaa

Department of Computer Science and Engineering CFG and PDA March 2, 2022 9/48
Derivation and Syntax Trees

Derivation Trees
Theorem 1:

Let G=(V,Σ, P, S) be a CFG. Then, S =⇒ α if and only if there
is a derivation tree for G with yield α.
Example: Consider G whose productions are

S → aAS\a, A → SbA\SS\ba. Show that S =⇒ aabbaa and Construct a
derivation tree whose yield is aabbaa.
Case 1:
S =⇒ aAS =⇒ aSbAS =⇒ aabAS =⇒ a2 bbaS =⇒ aabbaa
Case 2:
S =⇒ aAS =⇒ aAa =⇒ aSbAa =⇒ aSbbaa =⇒ aabbaa
Case 3:
S =⇒ aAS =⇒ aSbAS =⇒ aSbAa =⇒ aabAa =⇒ aabbaa

Department of Computer Science and Engineering CFG and PDA March 2, 2022 9/48
Derivation and Syntax Trees

Derivation Trees


Left most derivation: A derivation A =⇒ w is called
left-most derivation, if we apply production only to the left
most variable at every step.

Right most derivation: A derivation A =⇒ w is a right most
derivation, if we apply production to right most variable at
each step.

Theorem

If A =⇒ w in G , then there is a left most derivation of w .

Department of Computer Science and Engineering CFG and PDA March 2, 2022 10/48
Derivation and Syntax Trees

Derivation Trees
Example: Let G be the grammar S → 0B\1A, A → 0\0S\1AA,
B → 1\1S\0BB. For the string 00110101, find:
the leftmost derivation
the rightmost derivation
The derivation tree

Department of Computer Science and Engineering CFG and PDA March 2, 2022 11/48
Derivation and Syntax Trees

Derivation Trees
Example: Let G be the grammar S → 0B\1A, A → 0\0S\1AA,
B → 1\1S\0BB. For the string 00110101, find:
the leftmost derivation
the rightmost derivation
The derivation tree
Leftmost derivation:
S =⇒ 0B =⇒ 00BB =⇒ 001B =⇒ 0011S =⇒ 02 12 0B =⇒
02 12 01S =⇒ 02 12 010B =⇒ 02 12 0101

Department of Computer Science and Engineering CFG and PDA March 2, 2022 11/48
Derivation and Syntax Trees

Derivation Trees
Example: Let G be the grammar S → 0B\1A, A → 0\0S\1AA,
B → 1\1S\0BB. For the string 00110101, find:
the leftmost derivation
the rightmost derivation
The derivation tree
Leftmost derivation:
S =⇒ 0B =⇒ 00BB =⇒ 001B =⇒ 0011S =⇒ 02 12 0B =⇒
02 12 01S =⇒ 02 12 010B =⇒ 02 12 0101
Rightmost derivation:
S =⇒ 0B =⇒ 00BB =⇒ 00B1S =⇒ 00B10B =⇒ 02 B101S =⇒
02 B1010B =⇒ 02 B10101 =⇒ 02 110101

Department of Computer Science and Engineering CFG and PDA March 2, 2022 11/48
Derivation and Syntax Trees

Derivation Trees
Example: Let G be the grammar S → 0B\1A, A → 0\0S\1AA,
B → 1\1S\0BB. For the string 00110101, find:
the leftmost derivation
the rightmost derivation
The derivation tree
Leftmost derivation:
S =⇒ 0B =⇒ 00BB =⇒ 001B =⇒ 0011S =⇒ 02 12 0B =⇒
02 12 01S =⇒ 02 12 010B =⇒ 02 12 0101
Rightmost derivation:
S =⇒ 0B =⇒ 00BB =⇒ 00B1S =⇒ 00B10B =⇒ 02 B101S =⇒
02 B1010B =⇒ 02 B10101 =⇒ 02 110101
Derivation tree:

Department of Computer Science and Engineering CFG and PDA March 2, 2022 11/48
Ambiguous Grammar

Ambiguity in CFG

In books selected information is given.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 12/48
Ambiguous Grammar

Ambiguity in CFG

In books selected information is given.


A terminal string w ∈ L(G ) is ambiguous, if there exist two or
more derivation trees for ’w ’ (or there exist two or more left
most derivation of w ).
G = ({S}, {a, b, +, ∗}, P, S), where P consists of
Example:
S → S + S|S ∗ S|a|b. We have two derivation trees for a+a*b:

S =⇒ S + S =⇒ a + S =⇒ a + S ∗ S =⇒ a + a ∗ S =⇒ a + a ∗ b
S =⇒ S ∗ S =⇒ S + S ∗ S =⇒ a + S ∗ S =⇒ a + a ∗ S =⇒ a + a ∗ b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 12/48
Ambiguous Grammar

Ambiguity in CFG

Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 13/48
Ambiguous Grammar

Ambiguity in CFG

Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.
For w = abababa

Department of Computer Science and Engineering CFG and PDA March 2, 2022 13/48
Ambiguous Grammar

Ambiguity in CFG

Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.
For w = abababa

Department of Computer Science and Engineering CFG and PDA March 2, 2022 13/48
Ambiguous Grammar

Ambiguity in CFG

Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.
For w = abababa

Thus, G is ambiguous.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 13/48
Simplification Forms

Simplification of CFG

We eliminate following in order to simplify a grammar:


Useless variables
Unit productions
Null Productions

Department of Computer Science and Engineering CFG and PDA March 2, 2022 14/48
Simplification Forms

Eliminating Useless Variables

Those variables which are not deriving any terminal string and
which are not reachable are known as Useless Variables.
Example: S → AB
A → a|B
B → b|C
C → aC
D→b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 15/48
Simplification Forms

Eliminating Useless Variables

Those variables which are not deriving any terminal string and
which are not reachable are known as Useless Variables.
Example: S → AB
A → a|B
B → b|C
C → aC
D→b
C is not deriving any terminal, C is not deriving any terminal
=⇒ useless variable, ∴ remove C

Department of Computer Science and Engineering CFG and PDA March 2, 2022 15/48
Simplification Forms

Eliminating Useless Variables

Those variables which are not deriving any terminal string and
which are not reachable are known as Useless Variables.
Example: S → AB
A → a|B
B → b|C
C → aC
D→b
C is not deriving any terminal, C is not deriving any terminal
=⇒ useless variable, ∴ remove C
S → AB, A → a|B, B → b, D → b
D is not included in S =⇒ remove D

Department of Computer Science and Engineering CFG and PDA March 2, 2022 15/48
Simplification Forms

Eliminating Useless Variables

Those variables which are not deriving any terminal string and
which are not reachable are known as Useless Variables.
Example: S → AB
A → a|B
B → b|C
C → aC
D→b
C is not deriving any terminal, C is not deriving any terminal
=⇒ useless variable, ∴ remove C
S → AB, A → a|B, B → b, D → b
D is not included in S =⇒ remove D
∴ S → AB, A → a|B, B → b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 15/48
Simplification Forms

Eliminating Unit Productions

Production of the form A → B is known as Unit Production

Department of Computer Science and Engineering CFG and PDA March 2, 2022 16/48
Simplification Forms

Eliminating Unit Productions

Production of the form A → B is known as Unit Production


Example: S → A, A → B, B → C , C → D, D → a
appears as chainlike process

Department of Computer Science and Engineering CFG and PDA March 2, 2022 16/48
Simplification Forms

Eliminating Unit Productions

Production of the form A → B is known as Unit Production


Example: S → A, A → B, B → C , C → D, D → a
appears as chainlike process
Instead S → a will serve the purpose
∴ required grammar is S → a

Department of Computer Science and Engineering CFG and PDA March 2, 2022 16/48
Simplification Forms

Eliminating Unit Productions

Example:S → Aa|B, B → A|bb, A → a|bc|B

Department of Computer Science and Engineering CFG and PDA March 2, 2022 17/48
Simplification Forms

Eliminating Unit Productions

Example:S → Aa|B, B → A|bb, A → a|bc|B


Starting from last to first
A → B is a unit production, replace B by its R.H.S.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 17/48
Simplification Forms

Eliminating Unit Productions

Example:S → Aa|B, B → A|bb, A → a|bc|B


Starting from last to first
A → B is a unit production, replace B by its R.H.S.
A → a|bc|A|bb
B → a|bc|A|bb
S → Aa|a|bc|A|bb

Department of Computer Science and Engineering CFG and PDA March 2, 2022 17/48
Simplification Forms

Eliminating Unit Productions

Example:S → Aa|B, B → A|bb, A → a|bc|B


Starting from last to first
A → B is a unit production, replace B by its R.H.S.
A → a|bc|A|bb
B → a|bc|A|bb
S → Aa|a|bc|A|bb
Now, remove unit productions
A → a|bc|bb
B → a|bc|bb
S → Aa|bc|a|bb

Department of Computer Science and Engineering CFG and PDA March 2, 2022 17/48
Simplification Forms

Eliminating Unit Productions

Example:S → Aa|B, B → A|bb, A → a|bc|B


Starting from last to first
A → B is a unit production, replace B by its R.H.S.
A → a|bc|A|bb
B → a|bc|A|bb
S → Aa|a|bc|A|bb
Now, remove unit productions
A → a|bc|bb
B → a|bc|bb
S → Aa|bc|a|bb
Now, S doesnot has B, =⇒ remove B
S → Aa|bc|a|bb
A → a|bc|bb

Department of Computer Science and Engineering CFG and PDA March 2, 2022 17/48
Simplification Forms

Eliminating Null Productions

Any production of the form A →  is called Null Production.


Example: S → aAb, A → aAb|

Department of Computer Science and Engineering CFG and PDA March 2, 2022 18/48
Simplification Forms

Eliminating Null Productions

Any production of the form A →  is called Null Production.


Example: S → aAb, A → aAb|
Replace A with  in each production containing A and add it
to grammar without 
S → aAb\ab
A → aAb\ab

Department of Computer Science and Engineering CFG and PDA March 2, 2022 18/48
Simplification Forms

Simplifying CFG

Example: Construct reduced grammar equivalent to grammar


G whose productions are:
S → AB|CA, B → BC |AB, A → a, C → aB|b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 19/48
Simplification Forms

Simplifying CFG

Example: Construct reduced grammar equivalent to grammar


G whose productions are:
S → AB|CA, B → BC |AB, A → a, C → aB|b
Here, B is not deriving any terminals ∴ remove B

Department of Computer Science and Engineering CFG and PDA March 2, 2022 19/48
Simplification Forms

Simplifying CFG

Example: Construct reduced grammar equivalent to grammar


G whose productions are:
S → AB|CA, B → BC |AB, A → a, C → aB|b
Here, B is not deriving any terminals ∴ remove B
S → CA, A → a, C → b
∴ New Grammar G 0 = ({S, A, C }, {a, b}, P, S)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 19/48
Simplification Forms

Simplifying CFG

Example: Construct reduced grammar equivalent to grammar


G whose productions are:
S → AB|CA, B → BC |AB, A → a, C → aB|b
Here, B is not deriving any terminals ∴ remove B
S → CA, A → a, C → b
∴ New Grammar G 0 = ({S, A, C }, {a, b}, P, S)
Question: Find the reduced grammar equivalent to G .
S → aAa, A → bBB, B → ab, C → aB

Department of Computer Science and Engineering CFG and PDA March 2, 2022 19/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b
Example: Convert the grammar G with Productions as:
S → ABa, A → aab, B → Ac to chomsky normal form

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b
Example: Convert the grammar G with Productions as:
S → ABa, A → aab, B → Ac to chomsky normal form
Let us assume few new variables:
X → a, Y → b, Z → c

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b
Example: Convert the grammar G with Productions as:
S → ABa, A → aab, B → Ac to chomsky normal form
Let us assume few new variables:
X → a, Y → b, Z → c
gives S → ABX , A → XXY , B → AZ

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b
Example: Convert the grammar G with Productions as:
S → ABa, A → aab, B → Ac to chomsky normal form
Let us assume few new variables:
X → a, Y → b, Z → c
gives S → ABX , A → XXY , B → AZ
Now, Assuming Q → XX , P → AB

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b
Example: Convert the grammar G with Productions as:
S → ABa, A → aab, B → Ac to chomsky normal form
Let us assume few new variables:
X → a, Y → b, Z → c
gives S → ABX , A → XXY , B → AZ
Now, Assuming Q → XX , P → AB
gives, S → PX , A → QY , B → AZ , X → a, Y → b, Z → c, Q → XX ,
P → AB

Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Normal forms of CFG


Chomsky Normal Form
Greibach Normal Form
Chomsky Normal Form:
A CFG is in Chomsky normal form if all productions are of the form:
A → BC or A → a
where A,B,C are variables and a is a terminal.
Example: The Grammar in the form:
S → AS|a, A → SA|b
Example: Convert the grammar G with Productions as:
S → ABa, A → aab, B → Ac to chomsky normal form
Let us assume few new variables:
X → a, Y → b, Z → c
gives S → ABX , A → XXY , B → AZ
Now, Assuming Q → XX , P → AB
gives, S → PX , A → QY , B → AZ , X → a, Y → b, Z → c, Q → XX ,
P → AB
Convert the given grammar to CNF
S → aSb |ab
S → aAB |Bb
A→a,B→b
S → bA |aB
A → bAA |aS |a
B → aBB |bS |b
Department of Computer Science and Engineering CFG and PDA March 2, 2022 20/48
Normal Forms (CNF and GNF)

Greibach Normal Form

A CFG is said to be in Greibach Normal form, if all the


productions have the form A → aX , where a ∈ T and X ∈ V ∗
Example: S → aAB|bBB|bB
A → aA|bB|b
B→b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 21/48
Normal Forms (CNF and GNF)

Greibach Normal Form

A CFG is said to be in Greibach Normal form, if all the


productions have the form A → aX , where a ∈ T and X ∈ V ∗
Example: S → aAB|bBB|bB
A → aA|bB|b
B→b
Example: Convert the grammar G into equivalent GNF:
S → abSb|aa

Department of Computer Science and Engineering CFG and PDA March 2, 2022 21/48
Normal Forms (CNF and GNF)

Greibach Normal Form

A CFG is said to be in Greibach Normal form, if all the


productions have the form A → aX , where a ∈ T and X ∈ V ∗
Example: S → aAB|bBB|bB
A → aA|bB|b
B→b
Example: Convert the grammar G into equivalent GNF:
S → abSb|aa
Solution: Let A → a, B → b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 21/48
Normal Forms (CNF and GNF)

Greibach Normal Form

A CFG is said to be in Greibach Normal form, if all the


productions have the form A → aX , where a ∈ T and X ∈ V ∗
Example: S → aAB|bBB|bB
A → aA|bB|b
B→b
Example: Convert the grammar G into equivalent GNF:
S → abSb|aa
Solution: Let A → a, B → b
∴ S → aBSB|aA, A → a, B → b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 21/48
Normal Forms (CNF and GNF)

Greibach Normal Form

A CFG is said to be in Greibach Normal form, if all the


productions have the form A → aX , where a ∈ T and X ∈ V ∗
Example: S → aAB|bBB|bB
A → aA|bB|b
B→b
Example: Convert the grammar G into equivalent GNF:
S → abSb|aa
Solution: Let A → a, B → b
∴ S → aBSB|aA, A → a, B → b
Convert the grammar S → ab |aS |aaS into GNF
Solution: Let B → b, A → a, S → aB |aS |aAS

Department of Computer Science and Engineering CFG and PDA March 2, 2022 21/48
Normal Forms (CNF and GNF)

Linear Grammar
A grammar in which atmost one variable can occur on right
side of any production without restriction on the size of this
grammar, is known as Linear Grammar.
A regular grammar is always linear but not all linear grammars
are regular.
Right Linear Grammar- A grammar G = (V , T , S, P) is said to be
right linear, if all the productions are of the form:
A → xB, A → x
where A, B ∈ V , x ∈ T ∗

Department of Computer Science and Engineering CFG and PDA March 2, 2022 22/48
Normal Forms (CNF and GNF)

Linear Grammar
A grammar in which atmost one variable can occur on right
side of any production without restriction on the size of this
grammar, is known as Linear Grammar.
A regular grammar is always linear but not all linear grammars
are regular.
Right Linear Grammar- A grammar G = (V , T , S, P) is said to be
right linear, if all the productions are of the form:
A → xB, A → x
where A, B ∈ V , x ∈ T ∗
Left Linear Grammar- A grammar is said to be left linear if all the
productions are of the form:
A → Bx, A → x
where A, B ∈ V , x ∈ T ∗

Department of Computer Science and Engineering CFG and PDA March 2, 2022 22/48
Normal Forms (CNF and GNF)

Linear Grammar
A grammar in which atmost one variable can occur on right
side of any production without restriction on the size of this
grammar, is known as Linear Grammar.
A regular grammar is always linear but not all linear grammars
are regular.
Right Linear Grammar- A grammar G = (V , T , S, P) is said to be
right linear, if all the productions are of the form:
A → xB, A → x
where A, B ∈ V , x ∈ T ∗
Left Linear Grammar- A grammar is said to be left linear if all the
productions are of the form:
A → Bx, A → x
where A, B ∈ V , x ∈ T ∗
A regular grammar is one that is either right linear or left
linear

Department of Computer Science and Engineering CFG and PDA March 2, 2022 22/48
Normal Forms (CNF and GNF)

Linear Grammar
A grammar in which atmost one variable can occur on right
side of any production without restriction on the size of this
grammar, is known as Linear Grammar.
A regular grammar is always linear but not all linear grammars
are regular.
Right Linear Grammar- A grammar G = (V , T , S, P) is said to be
right linear, if all the productions are of the form:
A → xB, A → x
where A, B ∈ V , x ∈ T ∗
Left Linear Grammar- A grammar is said to be left linear if all the
productions are of the form:
A → Bx, A → x
where A, B ∈ V , x ∈ T ∗
A regular grammar is one that is either right linear or left
linear
In a regular grammar, atmost one variable appears on right
side of any production. Further, that variable must
consistently be either on rightmost or leftmost symbol of right
side of any production.
Department of Computer Science and Engineering CFG and PDA March 2, 2022 22/48
Normal Forms (CNF and GNF)

Regular Grammar

In a regular grammar, atmost one variable appears on right


side of any production. Further, that variable must
consistently be either on rightmost or leftmost symbol of right
side of any production.
Example: G1 = ({S}, {a, b}, S, P1 ) where P1 given as
S → abS\a is right linear.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 23/48
Normal Forms (CNF and GNF)

Regular Grammar

In a regular grammar, atmost one variable appears on right


side of any production. Further, that variable must
consistently be either on rightmost or leftmost symbol of right
side of any production.
Example: G1 = ({S}, {a, b}, S, P1 ) where P1 given as
S → abS\a is right linear.
Example: G2 = ({S, S1 , S2 }, {a, b}, S, P2 ) with productions
S → S1 ab, S1 → S1 ab, S1 → S2 , S2 → a is left linear

Department of Computer Science and Engineering CFG and PDA March 2, 2022 23/48
Normal Forms (CNF and GNF)

Regular Grammar

In a regular grammar, atmost one variable appears on right


side of any production. Further, that variable must
consistently be either on rightmost or leftmost symbol of right
side of any production.
Example: G1 = ({S}, {a, b}, S, P1 ) where P1 given as
S → abS\a is right linear.
Example: G2 = ({S, S1 , S2 }, {a, b}, S, P2 ) with productions
S → S1 ab, S1 → S1 ab, S1 → S2 , S2 → a is left linear
G = ({S, A, B}, {a, b}, S, P) with productions
S → A, A → aB\∧, B → Ab is not regular
∵ even if each production is right linear or left linear, but
grammar itself is neither right linear nor left linear ∴ not
regular

Department of Computer Science and Engineering CFG and PDA March 2, 2022 23/48
Normal Forms (CNF and GNF)

Linear Grammar

1 Construct a finite automata that accepts the language


generated by grammar
V0 → aV1 , v1 → abV0 |b
2 Construct a right linear grammar for L(aab ∗ a)
3 Convert the following regular expression into equivalent
regular grammar
(a + b)∗ a
a∗ +b+b ∗

Department of Computer Science and Engineering CFG and PDA March 2, 2022 24/48
PushDown Automata: Introduction

Push Down Automata

Figure 4: Model of Push Down Automata

A Non-deterministic Push Down Automata is a 7-tuple (Q,Σ,


τ , δ, qo , zo , F )
where , Q= finite non-empty set of states
Σ= finite non-empty set of input symbols
τ = finite non-empty set of push down symbols
qo = initial state
zo = initial symbol on push down store
F = set of final states
δ =⇒ Q × (Σ ∪ {∧}) × τ → Q × τ ∗
Department of Computer Science and Engineering CFG and PDA March 2, 2022 25/48
PushDown Automata: Introduction

Pushdown Automata

δ =⇒ Q × (Σ ∪ {∧}) × τ → Q × τ ∗
Each move of the control unit is determined by the current
input symbol as well as by the symbol currently on the top of
the stack.
The result of the move is a new state of control unit and a
change in the top of the stack.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 26/48
PushDown Automata: Introduction

NPDA: Example

Consider a NPDA as under:


Q = {qo , q1 , q2 , q3 }, Σ = {a, b}
τ = {0, 1}, Z = 0, F = {q3 } and
δ(qo , a, 0) = (q0 , a0)
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , ∧)
δ(q1 , b, a) = (q1 , ∧)
δ(q1 , ∧, 0) = (qf , 0)
What can we say about the action of this automaton?

Department of Computer Science and Engineering CFG and PDA March 2, 2022 27/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process
(q0 , baab, Z ) `

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process
(q0 , baab, Z ) ` (q0 , aab, 1Z ) `

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process
(q0 , baab, Z ) ` (q0 , aab, 1Z ) ` (q0 , ab, Z ) `

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process
(q0 , baab, Z ) ` (q0 , aab, 1Z ) ` (q0 , ab, Z ) ` (q0 , b, 0Z ) `

Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process
(q0 , baab, Z ) ` (q0 , aab, 1Z ) ` (q0 , ab, Z ) ` (q0 , b, 0Z ) `
(q0 , λ, Z ) `
Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by a PDA


Let M =(Q,Σ, τ, δ, q0 ,Z, F) be a non-deterministic push-down
automata. The language accepted by M is the set
L(M)={w ∈ Σ∗ : (q0 , w , Z ) `∗M (P,λ, U) }
where P ∈ F and U ∈ τ ∗
Example: Construct a NPDA for the language
L={w ∈ {a, b}∗ : na (w)=nb (w) }
Solution: Q={q0 , qf }, Σ={a,b}
τ ={0,1,Z}, F={qf }
Let M= {Q, Σ, τ, δ, q0 , F , Z }
δ(q0 , λ, Z )= (qf , Z ) δ(q0 , a, Z ) = (q0 , 0Z )
δ(q0 , b, Z ) = (q0 , 1Z ) δ(q0 , a, 0) = (q0 , 00)
δ(q0 , b, 0) = (q0 , λ) δ(q0 , a, 1) = (q0 , λ)
δ(q0 , b, 1) = (q0 , 11)
Let us assume w=baab to process
(q0 , baab, Z ) ` (q0 , aab, 1Z ) ` (q0 , ab, Z ) ` (q0 , b, 0Z ) `
(q0 , λ, Z ) ` (qf , λ, Z )
Department of Computer Science and Engineering CFG and PDA March 2, 2022 28/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) ,

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z) ` (q0 , bba, az)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z) ` (q0 , bba, az) ` (q0 , ba, baz)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z) ` (q0 , bba, az) ` (q0 , ba, baz) ` (q1 , ba, baz)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z) ` (q0 , bba, az) ` (q0 , ba, baz) ` (q1 , ba, baz)
` (q1 , a, az)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z) ` (q0 , bba, az) ` (q0 , ba, baz) ` (q1 , ba, baz)
` (q1 , a, az) ` (q1 , λ, z)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by PDA


Question: Construct a NPDA for accepting the language
L={ww R : w ∈ {a, b}+ }
Solution: M=(Q,Σ, τ , δ, q0 , z, F)
where Q= {q0 , q1 , q2 }, Σ={a,b}
τ = {a,b,z}, F={q2 }
δ(q0 , a, a) = (q0 , aa), δ(q0 , b, a) = (q0 , ba)
δ(q0 , a, b) = (q0 , ab), δ(q0 , b, b) = (q0 , bb)
δ(q0 , a, z) = (q0 , az), δ(q0 , b, z) = (q0 , bz)
For middle:
δ(q0 , λ, a) = (q1 , a), δ(q0 , λ, b) = (q1 , b)
For matching w R against contents of stack
δ(q1 , a, a) = (q1 , λ) , δ(q1 , b, b) = (q1 , λ)
Finally, δ(q1 , λ, z) = (q2 , z)
Let the String assumed be ’abba’:
(q0 , abba, z) ` (q0 , bba, az) ` (q0 , ba, baz) ` (q1 , ba, baz)
` (q1 , a, az) ` (q1 , λ, z) ` (q2 , z)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 29/48
PushDown Automata: Introduction

Language accepted by a PDA

1 Construct a NPDA that accepts the language


L={an b m : n≥0, n6=m}
2 Find NPDA on Σ={a,b,c} that accepts the language
L={w1 cw2 : w1 , w2 ∈ {a, b}∗ , w1 6= w2 R }

Department of Computer Science and Engineering CFG and PDA March 2, 2022 30/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a

Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a
Solution: Step-1 Convert the grammar into Greibach normal
form

Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a
Solution: Step-1 Convert the grammar into Greibach normal
form
S → aSA |a
A → bB, B → b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a
Solution: Step-1 Convert the grammar into Greibach normal
form
S → aSA |a
A → bB, B → b
Put Start symbol to stack
δ(q0 , λ, Z ) = (q1 , SZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a
Solution: Step-1 Convert the grammar into Greibach normal
form
S → aSA |a
A → bB, B → b
Put Start symbol to stack
δ(q0 , λ, Z ) = (q1 , SZ )
For production S → aSA |a
δ(q1 , a, S) = (q1 , SA), (q1 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a
Solution: Step-1 Convert the grammar into Greibach normal
form
S → aSA |a
A → bB, B → b
Put Start symbol to stack
δ(q0 , λ, Z ) = (q1 , SZ )
For production S → aSA |a
δ(q1 , a, S) = (q1 , SA), (q1 , λ)
For other productions
δ(q1 , b, A) = (q1 , B), δ(q1 , b, B) − (q1 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Construct a PDA that accepts the language generated by the
grammar with productions
S → aSbb |a
Solution: Step-1 Convert the grammar into Greibach normal
form
S → aSA |a
A → bB, B → b
Put Start symbol to stack
δ(q0 , λ, Z ) = (q1 , SZ )
For production S → aSA |a
δ(q1 , a, S) = (q1 , SA), (q1 , λ)
For other productions
δ(q1 , b, A) = (q1 , B), δ(q1 , b, B) − (q1 , λ)
Appearance of λ on top of stack implies completion of
derivation and PDA is put to final state by
δ(q1 , λ, Z ) = (q2 , λ)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 31/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ ) ` (q1 , aabc, AZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ ) ` (q1 , aabc, AZ )
` (q1 , abc, ABCZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ ) ` (q1 , aabc, AZ )
` (q1 , abc, ABCZ ) ` (q1 , bc, BCZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ ) ` (q1 , aabc, AZ )
` (q1 , abc, ABCZ ) ` (q1 , bc, BCZ ) ` (q1 , c, CZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ ) ` (q1 , aabc, AZ )
` (q1 , abc, ABCZ ) ` (q1 , bc, BCZ ) ` (q1 , c, CZ ) ` (q1 , λ, Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

Push Down Automata for CFL


Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: The Grammar is already in Greibach normal form.
Putting Start Symbol on stack
δ(q0 , λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , λ, Z ) = (qf , Z )
Now, according to the productions
δ(q1 , a, S) = (q1 , A), δ(q1 , a, A) = (q1 , ABC ),
δ(q1 , b, A) = (q1 , B), δ(q1 , a, A) = (q1 , λ), δ(q1 , b, B) = (q1 , λ),
δ(q1 , c, C ) = (q1 , λ)
Let the derivation be
S → aA → aaABC → aaaBC → aaabC → aaabc
Therefore, the sequence of moves by M for the processing of
”aaabc” is:
(q0 , aaabc, Z ) ` (q1 , aaabc, SZ ) ` (q1 , aabc, AZ )
` (q1 , abc, ABCZ ) ` (q1 , bc, BCZ ) ` (q1 , c, CZ ) ` (q1 , λ, Z )
` (qf , λ, Z )
Department of Computer Science and Engineering CFG and PDA March 2, 2022 32/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB) ` (q,0000,SB)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB) ` (q,0000,SB) `
(q,000,BBB)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB) ` (q,0000,SB) `
(q,000,BBB) ` (q, 00,BB)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB) ` (q,0000,SB) `
(q,000,BBB) ` (q, 00,BB) ` (q,0,B)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB) ` (q,0000,SB) `
(q,000,BBB) ` (q, 00,BB) ` (q,0,B) ` (q,λ,Z)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

CFG to PDA
Question Construct a PDA ’A’ equivalent to the following Context
free grammar:
S → 0BB, B → 0S |1S |0.
Test whether 010000 is in N(A).
Solution: Let A =({q},{0,1},{S,B,0,1},δ,q,S,φ)
δ is defined by following rules:
δ(q, λ, Z ) = (q, SZ )
δ(q, 0, S) = (q, BB)
δ(q, 0, B) = (q, S)
δ(q, 1, B) = (q, S)
δ(q, 0, B) = (q, λ)
δ(q, λ, Z ) = (qf , λ)
Let the derivation be:
S → 0BB → 01SB → 010BBB → 010000
Acceptability of 010000:
(q,010000,Z) ` (q,010000,SZ) ` (q,10000,BB) ` (q,0000,SB) `
(q,000,BBB) ` (q, 00,BB) ` (q,0,B) ` (q,λ,Z) ` (qf , λ)
Department of Computer Science and Engineering CFG and PDA March 2, 2022 33/48
Relationship between PDA and CFL

PDA to CFG

1 δ(q0 , a, Z0 )= (q0 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 34/48
Relationship between PDA and CFL

PDA to CFG

1 δ(q0 , a, Z0 )= (q0 , λ)
[q0 , Z0 , q0 ] → a

Department of Computer Science and Engineering CFG and PDA March 2, 2022 34/48
Relationship between PDA and CFL

PDA to CFG

1 δ(q0 , a, Z0 )= (q0 , λ)
[q0 , Z0 , q0 ] → a
2 δ(q0 , a, Z0 ) = (q0 , aZ0 )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 34/48
Relationship between PDA and CFL

PDA to CFG

1 δ(q0 , a, Z0 )= (q0 , λ)
[q0 , Z0 , q0 ] → a
2 δ(q0 , a, Z0 ) = (q0 , aZ0 )
[q0 Z0 . . .] → a[q0 a . . .][. . . Z0 . . .]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 34/48
Relationship between PDA and CFL

PDA to CFG

1 δ(q0 , a, Z0 )= (q0 , λ)
[q0 , Z0 , q0 ] → a
2 δ(q0 , a, Z0 ) = (q0 , aZ0 )
[q0 Z0 . . .] → a[q0 a . . .][. . . Z0 . . .]
3 δ(q0 , a, Z0 ) = (q1 aXX )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 34/48
Relationship between PDA and CFL

PDA to CFG

1 δ(q0 , a, Z0 )= (q0 , λ)
[q0 , Z0 , q0 ] → a
2 δ(q0 , a, Z0 ) = (q0 , aZ0 )
[q0 Z0 . . .] → a[q0 a . . .][. . . Z0 . . .]
3 δ(q0 , a, Z0 ) = (q1 aXX )
[q0 Z0 . . .] = a[q1 a . . .][. . . X . . .][. . . X . . .]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 34/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 35/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )
Solution: Let G=(VN , {a, b}, P, S)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 35/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )
Solution: Let G=(VN , {a, b}, P, S)
VN = {S, [q0 , Z0 , q0 ], [q0 , Z0 , q1 ], [q1 , Z0 , q0 ], [q1 , Z0 , q1 ],
[q0 , Z , q0 ], [q0 , Z , q1 ], [q1 , Z , q0 ], [q1 , Z , q1 ]}

Department of Computer Science and Engineering CFG and PDA March 2, 2022 35/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )
Solution: Let G=(VN , {a, b}, P, S)
VN = {S, [q0 , Z0 , q0 ], [q0 , Z0 , q1 ], [q1 , Z0 , q0 ], [q1 , Z0 , q1 ],
[q0 , Z , q0 ], [q0 , Z , q1 ], [q1 , Z , q0 ], [q1 , Z , q1 ]}
The Productions are:
Initial: S → [q0 , Z0 , q0 ], [q0 , Z0 , q1 ]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 35/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )
Solution: Let G=(VN , {a, b}, P, S)
VN = {S, [q0 , Z0 , q0 ], [q0 , Z0 , q1 ], [q1 , Z0 , q0 ], [q1 , Z0 , q1 ],
[q0 , Z , q0 ], [q0 , Z , q1 ], [q1 , Z , q0 ], [q1 , Z , q1 ]}
The Productions are:
Initial: S → [q0 , Z0 , q0 ], [q0 , Z0 , q1 ]
For δ(q0 , b, Z0 ) = (q0 , ZZ0 )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 35/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )
Solution: Let G=(VN , {a, b}, P, S)
VN = {S, [q0 , Z0 , q0 ], [q0 , Z0 , q1 ], [q1 , Z0 , q0 ], [q1 , Z0 , q1 ],
[q0 , Z , q0 ], [q0 , Z , q1 ], [q1 , Z , q0 ], [q1 , Z , q1 ]}
The Productions are:
Initial: S → [q0 , Z0 , q0 ], [q0 , Z0 , q1 ]
For δ(q0 , b, Z0 ) = (q0 , ZZ0 )
[q0 Z0 . . .] → b[q0 Z . . .][. . . Z0 . . .]
[q0 Z0 . . .] → b[q0 Z . . .][. . . Z0 . . .]
[q0 Z0 . . .] → b[q0 Z . . .][. . . Z0 . . .]
[q0 Z0 . . .] → b[q0 Z . . .][. . . Z0 . . .]
Department of Computer Science and Engineering CFG and PDA March 2, 2022 35/48
Relationship between PDA and CFL

PDA to CFG
Question: Construct a Context free grammar G which accepts
N(A), where A=({q0 , q1 },{a,b},{Z0 ,Z}, δ, q0 , Z0 , φ) and δ is given
by:
δ(q0 , b, Z0 ) = (q0 , ZZ0 ), δ(q0 , λ, Z0 ) = (q0 , λ)
δ(q0 , b, Z ) = (q0 , ZZ ), δ(q0 , a, Z ) = (q1 , Z )
δ(q1 , b, Z ) = (q1 , λ), δ(q1 , a, Z0 ) = (q0 , Z0 )
Solution: Let G=(VN , {a, b}, P, S)
VN = {S, [q0 , Z0 , q0 ], [q0 , Z0 , q1 ], [q1 , Z0 , q0 ], [q1 , Z0 , q1 ],
[q0 , Z , q0 ], [q0 , Z , q1 ], [q1 , Z , q0 ], [q1 , Z , q1 ]}
The Productions are:
Initial: S → [q0 , Z0 , q0 ], [q0 , Z0 , q1 ]
For δ(q0 , b, Z0 ) = (q0 , ZZ0 )
[q0 Z0 q0 ] → b[q0 Zq0 ][q0 Z0 q0 ]
[q0 Z0 q0 ] → b[q0 Zq1 ][q1 Z0 q0 ]
[q0 Z0 q1 ] → b[q0 Zq0 ][q0 Z0 q1 ]
[q0 Z0 q1 ] → b[q0 Zq1 ][q1 Z0 q1 ]
Department of Computer Science and Engineering CFG and PDA March 2, 2022 36/48
Relationship between PDA and CFL

PDA to CFG

For δ(q0 , ∧, Z0 ) = (q0 , ∧)


[q0 Z0 q0 ] → ∧

Department of Computer Science and Engineering CFG and PDA March 2, 2022 37/48
Relationship between PDA and CFL

PDA to CFG

For δ(q0 , ∧, Z0 ) = (q0 , ∧)


[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Z . . .] → b[q0 Z . . .][. . . Z . . .]
[q0 Z . . .] → b[q0 Z . . .][. . . Z . . .]
[q0 Z . . .] → b[q0 Z . . .][. . . Z . . .]
[q0 Z . . .] → b[q0 Z . . .][. . . Z . . .]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 37/48
Relationship between PDA and CFL

PDA to CFG

For δ(q0 , ∧, Z0 ) = (q0 , ∧)


[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 38/48
Relationship between PDA and CFL

PDA to CFG

For δ(q0 , ∧, Z0 ) = (q0 , ∧)


[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]
For (q0 , a, Z ) = (q1 , Z )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 38/48
Relationship between PDA and CFL

PDA to CFG

For δ(q0 , ∧, Z0 ) = (q0 , ∧)


[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]
For (q0 , a, Z ) = (q1 , Z )
[q0 Z . . .] → a[q1 Z . . .]
[q0 Z . . .] → a[q1 Z . . .]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 38/48
Relationship between PDA and CFL

PDA to CFG
For δ(q0 , ∧, Z0 ) = (q0 , ∧)
[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]
For (q0 , a, Z ) = (q1 , Z )
[q0 Zq0 ] → a[q1 Zq0 ]
[q0 Zq1 ] → a[q1 Zq1 ]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 39/48
Relationship between PDA and CFL

PDA to CFG
For δ(q0 , ∧, Z0 ) = (q0 , ∧)
[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]
For (q0 , a, Z ) = (q1 , Z )
[q0 Zq0 ] → a[q1 Zq0 ]
[q0 Zq1 ] → a[q1 Zq1 ]
For δ(q1 , b, Z ) = (q1 , ∧)
[q1 , Z , q1 ] → b

Department of Computer Science and Engineering CFG and PDA March 2, 2022 39/48
Relationship between PDA and CFL

PDA to CFG
For δ(q0 , ∧, Z0 ) = (q0 , ∧)
[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]
For (q0 , a, Z ) = (q1 , Z )
[q0 Zq0 ] → a[q1 Zq0 ]
[q0 Zq1 ] → a[q1 Zq1 ]
For δ(q1 , b, Z ) = (q1 , ∧)
[q1 , Z , q1 ] → b
For δ(q1 , a, Z0 ) → (q0 , Z0 )
[q1 , Z0 , . . .] → a[q0 Z0 . . .]
[q1 , Z0 , . . .] → a[q0 Z0 . . .]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 39/48
Relationship between PDA and CFL

PDA to CFG
For δ(q0 , ∧, Z0 ) = (q0 , ∧)
[q0 Z0 q0 ] → ∧
(q0 , b, Z ) = (q0 , ZZ )
[q0 Zq0 ] → b[q0 Zq0 ][q0 Zq0 ]
[q0 Zq0 ] → b[q0 Zq1 ][q1 Zq0 ]
[q0 Zq1 ] → b[q0 Zq0 ][q0 Zq1 ]
[q0 Zq1 ] → b[q0 Zq1 ][q1 Zq1 ]
For (q0 , a, Z ) = (q1 , Z )
[q0 Zq0 ] → a[q1 Zq0 ]
[q0 Zq1 ] → a[q1 Zq1 ]
For δ(q1 , b, Z ) = (q1 , ∧)
[q1 , Z , q1 ] → b
For δ(q1 , a, Z0 ) → (q0 , Z0 )
[q1 , Z0 , q0 ] → a[q0 Z0 q0 ]
[q1 , Z0 , q1 ] → a[q0 Z0 q1 ]

Department of Computer Science and Engineering CFG and PDA March 2, 2022 40/48
Relationship between PDA and CFL

PDA to CFG

1 Let M=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)


where productions are
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , ∧)
δ(q1 , b, a) = (q1 , ∧)
δ(q1 , ∧, Z0 ) = (q1 , ∧)
Find grammar G.
2 Find a context free grammar that generates the language
accepted by NPDA
M=({q0 , q1 },{a,b},{A,Z},δ,q0 ,Z,{q1 }) with transitions
δ(q0 , a, Z ) = (q0 , AZ )
δ(q0 , b, A) = (q0 , AA)
δ(q0 , a, A) = (q1 , λ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 41/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)
δ(q1 , b, a) = (q1 , a)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)
δ(q1 , b, a) = (q1 , a)
δ(q1 , a, a) = (q1 , ∧)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)
δ(q1 , b, a) = (q1 , a)
δ(q1 , a, a) = (q1 , ∧)
δ(q1 , ∧, Z0 ) = (q1 , ∧)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)
δ(q1 , b, a) = (q1 , a)
δ(q1 , a, a) = (q1 , ∧)
δ(q1 , ∧, Z0 ) = (q1 , ∧)
Let the required grammar G=(VN , {a, b}, P, S)

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)
δ(q1 , b, a) = (q1 , a)
δ(q1 , a, a) = (q1 , ∧)
δ(q1 , ∧, Z0 ) = (q1 , ∧)
Let the required grammar G=(VN , {a, b}, P, S)
VN =
(S, [q0 Z0 q0 ], [q0 Z0 q1 ], [q1 Z0 q0 ], [q1 Z0 q1 ], q0 aq0 ], [q0 aq1 ], [q1 aq0 ], [q1 aq1 ])

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

PDA to CFG
Example: Construct a PDA accepting {an b m an |m, n ≥ 1} by null
store. Construct the corresponding CFG accepting the same set.
Solution: The PDA ’A’ accepting {an b m an |m, n ≥ 1} is defined as
A=({q0 , q1 },{a,b},{a,Z0 }, δ, q0 , Z0 , φ)
where δ is defined by:
δ(q0 , a, Z0 ) = (q0 , aZ0 )
δ(q0 , a, a) = (q0 , aa)
δ(q0 , b, a) = (q1 , a)
δ(q1 , b, a) = (q1 , a)
δ(q1 , a, a) = (q1 , ∧)
δ(q1 , ∧, Z0 ) = (q1 , ∧)
Let the required grammar G=(VN , {a, b}, P, S)
VN =
(S, [q0 Z0 q0 ], [q0 Z0 q1 ], [q1 Z0 q0 ], [q1 Z0 q1 ], q0 aq0 ], [q0 aq1 ], [q1 aq0 ], [q1 aq1 ])
...

Department of Computer Science and Engineering CFG and PDA March 2, 2022 42/48
Relationship between PDA and CFL

1 What do you understand by LL(k) grammar? Explain with a


suitable example.
2 What do you understand by Parsing? How Top-Down Parsing
is different from Bottom-Up Parsing? Explain with suitable
example.
3 What is left factoring? How is it different from Left recursion?
4 Construct a PDA accepting the set of ll even-length
palindromes over {a,b} by the empty store.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 43/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language

Let ’L’ be an infinite cotext free language. Then, there exists some
positive integer ’m’ such that any w ∈ L with | w |≥ m can be
decomposed as w=uvxyz with | vxy |≤ m, | vy |≥ 1 such that
uv i xy i z ∈ L for all i=0,1,2,. . . .

Department of Computer Science and Engineering CFG and PDA March 2, 2022 44/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language

Let ’L’ be an infinite cotext free language. Then, there exists some
positive integer ’m’ such that any w ∈ L with | w |≥ m can be
decomposed as w=uvxyz with | vxy |≤ m, | vy |≥ 1 such that
uv i xy i z ∈ L for all i=0,1,2,. . . .
Application:
The punmping lemma is useful for showing that a language
doesnot belong to the family of Context free languages.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 44/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language

Let ’L’ be an infinite cotext free language. Then, there exists some
positive integer ’m’ such that any w ∈ L with | w |≥ m can be
decomposed as w=uvxyz with | vxy |≤ m, | vy |≥ 1 such that
uv i xy i z ∈ L for all i=0,1,2,. . . .
Application:
The punmping lemma is useful for showing that a language
doesnot belong to the family of Context free languages.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 44/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language


Ques: Show that the language
L={an b n c n : n ≥ 0} is not context free.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 45/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language


Ques: Show that the language
L={an b n c n : n ≥ 0} is not context free.
Solution: Let L be Context free
Let w be a string in L
For n=4, string becomes aaaabbbbcccc

Department of Computer Science and Engineering CFG and PDA March 2, 2022 45/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language


Ques: Show that the language
L={an b n c n : n ≥ 0} is not context free.
Solution: Let L be Context free
Let w be a string in L
For n=4, string becomes aaaabbbbcccc
By Pumping Lemma, w=uvxyz
w=aaaabbbbcccc

Department of Computer Science and Engineering CFG and PDA March 2, 2022 45/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language


Ques: Show that the language
L={an b n c n : n ≥ 0} is not context free.
Solution: Let L be Context free
Let w be a string in L
For n=4, string becomes aaaabbbbcccc
By Pumping Lemma, w=uvxyz
w=aaaabbbbcccc
Case 1: If vxy contain only ”a’s”, then on pumping. It won’t be in
L
Case 2: If string contains equal number of a’s and b’s, then
pumped string will contain ak b k c m with k6= m which does not
belong to L

Department of Computer Science and Engineering CFG and PDA March 2, 2022 45/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language


Ques: Show that the language
L={an b n c n : n ≥ 0} is not context free.
Solution: Let L be Context free
Let w be a string in L
For n=4, string becomes aaaabbbbcccc
By Pumping Lemma, w=uvxyz
w=aaaabbbbcccc
Case 1: If vxy contain only ”a’s”, then on pumping. It won’t be in
L
Case 2: If string contains equal number of a’s and b’s, then
pumped string will contain ak b k c m with k6= m which does not
belong to L
∴ L is not context free.

Department of Computer Science and Engineering CFG and PDA March 2, 2022 45/48
Relationship between PDA and CFL

Pumping Lemma for Context Free Language


Ques: Show that the language
L={an b n c n : n ≥ 0} is not context free.
Solution: Let L be Context free
Let w be a string in L
For n=4, string becomes aaaabbbbcccc
By Pumping Lemma, w=uvxyz
w=aaaabbbbcccc
Case 1: If vxy contain only ”a’s”, then on pumping. It won’t be in
L
Case 2: If string contains equal number of a’s and b’s, then
pumped string will contain ak b k c m with k6= m which does not
belong to L
∴ L is not context free.
Show that following L are not Context free
1 L={ww : w ∈ {a, b}∗}
2 L={an b j : n = j 2 }
3 L= {an! : n ≥ 0}
Department of Computer Science and Engineering CFG and PDA March 2, 2022 45/48
Decision Algorithms

Design Algorithms

Algorithm for deciding whether a context-free language L is


empty
Algorithm for deciding whether a context-free language L is
finite
Algorithm for deciding whether a regular language L is empty
Algorithm for deciding whether a regular language L is infinite

Department of Computer Science and Engineering CFG and PDA March 2, 2022 46/48
Questions?
[email protected]
Thank you.

You might also like