Module 2
Module 2
Module 2
Automata
Module 2
Dr. A K Yadav
Department of Computer Science & Engineering
Context-Free Grammars
Example:CFG
Example:CFG
Example:CFG
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
Derivation 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.
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
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
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
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 .
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
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
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 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:
Ambiguity in CFG
Ambiguity in CFG
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
Ambiguity in CFG
Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.
Ambiguity in CFG
Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.
For w = abababa
Ambiguity in CFG
Example:
If G is grammar S → SbS|a. Show that the given grammar is
ambigious.
For w = abababa
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.
Simplification of CFG
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
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
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
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
Simplifying CFG
Simplifying CFG
Simplifying CFG
Simplifying CFG
Lemma 1:
Let G = (V , Σ, P, S) be a CFG. Let A → Bγ be an A-production
in P. Let the B-productions be B → β1 |β2 | . . . |βk . Define
P1 = (P − {A → Bγ}) ∪ {A → βi γ|1 ≤ i ≤ k}. Then
G1 = (V , Σ, P1 , S) is a context-free grammar equivalent to G.
Decision Algorithms
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.
Right Linear Grammar- A grammar G = (V .T , P, S) 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 ∗
Linear Grammar- A grammar is said to be linear grammar if
all the productions are of the form:
A → vBw , A → w
where A, B ∈ V ; v , w ∈ T ∗
Dr. A K Yadav CFG and PDA December 21, 2022 32/63
Linear Grammar
Linear Grammar
A regular grammar is always linear but not all linear grammars
are regular.
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.
Example: G1 = ({S}, {a, b}, P1 , S) where P1 given as
S → abS|a is right linear.
Example: G2 = ({S, S1 , S2 }, {a, b}, P2 , S) with productions
S → S1 ab, S1 → S1 ab, S1 → S2 , S2 → a is left linear
Linear Grammar
G = ({S, A, B}, {a, b}, P, S) 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
Linear Grammar
Pushdown Automata
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.
Instantaneous Description (ID) Let A = (Q, Σ, τ, δ, q0 , Z0 , F ) be
a pda. An instantaneous description (ID) is (q, w , α), where
q ∈ Q, w ∈ Σ∗ and α ∈ τ ∗ .
An initial ID is (q o, w , Z0 ). This means that initially the pda
is in the initial state q0 , the input string to be processed is w
and the PDS has only one symbol, namely Z0 .
In an ID (q, ∧, Z ), In this case the pda makes a ∧-move.
Pushdown Automata
A move relation, denoted by ⊢ between IDs is defined as
(q, a1 a2 . . . an , Z1 Z2 . . . Zm ) ⊢ (q ′ , a2 . . . an , βZ2 . . . Zm )
if δ(q, a1 , Z1 ) = (q ′ , β)
if (q1 , x, α) ⊢∗ (q2 , ∧, β) then for every y ∈ Σ∗ ,
(q1 , xy , α) ⊢∗ (q2 , y , β)
Conversely, if (q1 , xy , α) ⊢∗ (q2 , y , β) for some y ∈ Σ∗ , then
(q1 , x, α) ⊢∗ (q2 , ∧, β)
if (q1 , x, α) ⊢∗ (q2 , ∧, β) then for every γ ∈ τ ∗ ,
(q1 , x, αγ) ⊢∗ (q2 , ∧, βγ)
NPDA: Example
Theorem
If A = (Q, Σ, τ, δ, q0 , Z0 , F ) accepts L by final state, we can find a
pda B accepting L by empty store: i.e. L = T (A) = N(B).
CFG to PDA
Theorem
If L is a context-free language, then we can construct a pda A
accepting L by empty store, i.e. L = N(A).
CFG to PDA
Construct a PDA that accepts the language generated by the
grammar with productions
S → aSA|a, A → bB, B → b
Solution: Step-1 The given productions are:
S → aSA|a
A → bB
B→b
δ is defined by the following rules:
S-productions
δ(q, ∧, S) = {(q, aSA), (q, a)}
A-productions
δ(q, ∧, A) = {(q, bB)}
B-productions
δ(q, ∧, B) = {(q, b)}
Productions for Σ
CFG to PDA
δ(q, a, a) = {(q, ∧)}
δ(q, b, b) = {(q, ∧)}
Appearance of Λ on top of stack implies completion of
derivation and PDA is put to final state by
δ(q, Λ, Z ) = (qf , Λ)
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: Putting Start Symbol on stack
δ(q0 , Λ, Z ) = (q1 , SZ )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: Putting Start Symbol on stack
δ(q0 , Λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , Λ, Z ) = (qf , Z )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: Putting Start Symbol on stack
δ(q0 , Λ, Z ) = (q1 , SZ )
Final Production: δ(q1 , Λ, Z ) = (qf , Z )
Now, according to the productions
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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),
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 ),
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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),
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 , Λ),
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 , Λ),
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 , Λ)
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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:
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
CFG to PDA
Question: Consider the grammar
S → aA, A →aABC |bB |a, B →b, C →c
Solution: 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 )
Dr. A K Yadav CFG and PDA December 21, 2022 48/63
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).
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,ϕ)
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 )
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)
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)
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)
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, Λ)
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 , Λ)
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
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:
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)
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)
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)
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)
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)
Dr. A K Yadav CFG and PDA December 21, 2022 49/63
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)
Dr. A K Yadav CFG and PDA December 21, 2022 49/63
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)
Dr. A K Yadav CFG and PDA December 21, 2022 49/63
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)
Dr. A K Yadav CFG and PDA December 21, 2022 49/63
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 , Λ)
Dr. A K Yadav CFG and PDA December 21, 2022 49/63
Relationship between PDA and CFL
PDA to CFG
Theorem
If A = (Q, Σ, τ, δ, q0 , Z0 , F ) is a pda then there exists a
context-free grammar G such that L(G ) = N(A).
PDA to CFG
R3 Each transition not erasing a pushdown symbol giving by
δ(q, a, Z ) = (q1 , Z1 Z2 . . . Zm ) induces the production
[q, Z , q ′ ] → a[q1 , Z1 , q2 ][q2 , Z2 , q3 ] . . . [qm , Zm , q ′ ], where each
of the states q ′ , q2 , . . . , qm can be any state in Q
PDA to CFG
1 S → [q0 , Z0 , qi ]
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
4 δ(q, a, Z ) = (q ′ , b)
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
4 δ(q, a, Z ) = (q ′ , b)
[q, Z , qi ] → a[q ′ , b, qi ]
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
4 δ(q, a, Z ) = (q ′ , b)
[q, Z , qi ] → a[q ′ , b, qi ]
5 δ(q, a, Z ) = (q ′ , bX )
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
4 δ(q, a, Z ) = (q ′ , b)
[q, Z , qi ] → a[q ′ , b, qi ]
5 δ(q, a, Z ) = (q ′ , bX )
[q, Z , qi ] → a[q ′ , b, . . .][. . . , X , qi ]
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
4 δ(q, a, Z ) = (q ′ , b)
[q, Z , qi ] → a[q ′ , b, qi ]
5 δ(q, a, Z ) = (q ′ , bX )
[q, Z , qi ] → a[q ′ , b, . . .][. . . , X , qi ]
6 δ(q, a, Z ) = (q ′ , bXY )
PDA to CFG
1 S → [q0 , Z0 , qi ]
2 δ(q, a, Z )= (q ′ , Λ)
[q, Z , q ′ ] → a
3 δ(q, Λ, Z )= (q ′ , Λ)
[q, Z , q ′ ] → Λ
4 δ(q, a, Z ) = (q ′ , b)
[q, Z , qi ] → a[q ′ , b, qi ]
5 δ(q, a, Z ) = (q ′ , bX )
[q, Z , qi ] → a[q ′ , b, . . .][. . . , X , qi ]
6 δ(q, a, Z ) = (q ′ , bXY )
[q, Z , qi ] = a[q ′ , b, . . .][. . . , X , . . .][. . . , Y , qi ]
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 )
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)
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 ]}
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 ]
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 )
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 . . .]
Dr. A K Yadav CFG and PDA December 21, 2022 53/63
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 ]
Dr. A K Yadav CFG and PDA December 21, 2022 54/63
Relationship between PDA and CFL
PDA to CFG
PDA to CFG
PDA to CFG
PDA to CFG
PDA to CFG
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 ]
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
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 . . .]
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 ]
PDA to CFG
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.
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
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 , ϕ)
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 )
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)
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)
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)
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 , ∧)
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 , ∧)
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)
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 ])
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 ])
...
Exercise