0% found this document useful (0 votes)
81 views17 pages

Pushdown Automata

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)
81 views17 pages

Pushdown Automata

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/ 17

Formal Languages &

Autometa Theory

Lecture Material on
PUSHDOWN AUTOMATA

PNRL Chandra Sekhar

Department of Computer Science & Engineering GST, GITAM

October 11, 2023


October 11, 2023 1 INTRODUCTION

The following material will give you an overview of what you learn in the class

1 Introduction
Pushdown automata are nondeterministic finite state machines augmented with addi-
tional stack memory, which is why the term “pushdown” is used, as elements are pushed
down onto the stack. Pushdown automata are computational models like finite state ma-
chines and can do more than a finite state machine but less than a Turing machine.

Figure 1: Pushdown Automaton

Pushdown automata accept context-free languages, which include the set of regular lan-
guages. Pushdown automata can be useful when considering parser design and any area
where context-free grammars are used, such as in computer language design.
In pushdown automata, state transitions include adding a symbol to the string generated,
as in FSMs. Still, state transitions can also include instructions about pushing and popping
elements to and from the stack. At each transition, a pushdown automaton can push a
symbol to the stack, pop a symbol from the stack, do both, or do no operations to the stack.
The pushdown automaton starts with an empty stack and accepts if it ends in an ac-
cepting state at the end or if the stack can be empty.
Pushdown automata can be modeled as a state transition diagram with added instruc-
tions about the stack. Where in the finite state machine, the arrows between states were
labeled with a symbol that represented the input symbol from the string, a pushdown au-
tomaton includes information in the form of input symbol followed by the symbol that is
currently at the top of the stack, followed by the symbol to replace the top of the stack with.
Commas, slashes, or arrows sometimes separate these instructions.

Figure 2: State Transitions on Pushdown Automaton

1
October 11, 2023 1 INTRODUCTION

1.1 Formal Definition


A pushdown automaton is formally defined as a 7-tuple: M (Q, Σ, Γ, δ, q0 , Z0 , F ) Where:

• Q is the finite set of states.

• Σ is the finite alphabet of the input alphabet.

• Γ is the finite stack alphabet.

• δ is the set of transition functions mapped as QX(Σ ∪ {ϵ})XΓ ⊢ (QXΓ).

• q0 is the initial state of the PDA, ∈ Q

• Z0 is the initial stack symbol, ∈ Γ

• F is the set of accepting states, ⊂ Q

1.2 Instataneous Description


Instantaneous Description (ID) is an informal notation describing how a PDA “com-
putes” an input string and decides whether the string is accepted or rejected.
An ID is a triple (qi , w, α), where:

• qi is the current state of read head.

• ’w’ is the position of the string still to be read on input.

• α is the string on the pushdown store. (It is customary to not write w&α completely
but to write the first symbol of w and the top most symbol of α)

The transition of a PDA can be defined as: δ(q0 , 0, Z0 ) ⊢ (q1 , 0Z0 )


At each transition, a pushdown automaton can push a symbol to the stack, pop a symbol
from the stack, do both, or do no operations to the stack.

1.3 Acceptance of a String


The acceptance of a string in PDA is of two types:

• Final State: At the end of the string, the read head is in one of the final states F.

• Null Store: The pushdown store is empty at the end of the string.

Problem 1

Design a pushdown automata for language {an bn |n > 0}

Solution. Design M as follows:

2
October 11, 2023 1 INTRODUCTION

Figure 3: Pushdown Automaton for an bn

where Q = { q0, q1 } and Σ = { a, b } and Γ = { A, Z } and δ is defined as:

• {δ(q0 , a, Z) ⊢ (q0 , AZ),

• δ(q0 , a, A) ⊢ (q0 , AA),

• δ(q0 , b, A) ⊢ (q1 , ϵ),

• δ(q1 , b, A) ⊢ (q1 , ϵ),

• δ(q1 , ϵ, Z) ⊢ (q1 , ϵ)}

Let us trace the above PDA with a string w = aaabbb as follows:


(q0 , aaabbb, Z0 ) =⇒ (q0 , aabbb, aZ0 ) – push a unto stack

• =⇒ (q0 , abbb, aaZ0 ) – push a unto stack

• =⇒ (q0 , bbb, aaaZ0 ) – push a unto stack

• =⇒ (q1 , bb, aaZ0 ) – pop a from top of stack

• =⇒ (q1 , b, aZ0 ) – pop a from top of stack

• =⇒ (q1 , ϵ, Z0 ) – pop a from top of stack

• =⇒ (q1 , ϵ, ϵ) – pop Z0 from stack


∵ at the end of the string, the stack becomes completely empty as well as it reaches
to the final state q1 , the string w = aaabbb is accepted by the given PDA.

1.4 Language accepted by a PDA


• For a PDA M (Q, Σ, Γ, δ, q0 , Z0 , F ), we define L(M), the language accepted by a PDA
by the final state as:
L(M ) = {w|(q0 , w, Z0 ) ⊢∗ (p, ϵ, γ) for some p ∈ F &γ ∈ Γ∗ }

• For a PDA M (Q, Σ, Γ, δ, q0 , Z0 , Φ), we define L(M), the language accepted by a PDA
by Null Store as:
L(M ) = {w|(q0 , w, Z0 ) ⊢∗ (p, ϵ, ϵ) for some p ∈ Q }

3
October 11, 2023 2 EQUIVALENCE OF PDA’S

Problem 2

Practice designing PDA for the following languages and trace the acceptance of relevant
strings.

a) L = {an ban |n ≥ 0}

b) L = {an b2n |n ≥ 0}

c) L = {0n 1n 2m |n, m ≥ 0}

d) L = {0n 1n+2 |n ≥ 0}

e) L = {wcwR |w ∈ {a, b}∗ }

2 Equivalence of PDA’s
Let M1 be a PDA by final state and is formally defined as a 7-tuple: M (Q, Σ, Γ, δ, q0 , Z0 , F )
and its transition diagram as:

Figure 4: Pushdown Automaton for M1

The acceptance of a string or configuration of the machine as: (q1 , w, Z1 ) ⊢∗M1 (qf , ϵ, γ)
Let M2 be a PDA by null store and is formally defined as a 7-tuple: M (Q, Σ, Γ, δ, q0 , Z0 , Φ)
and its transition diagram as:

Figure 5: Pushdown Automaton for M2

The acceptance of a string or configuration of the machine as: (q1 , w, Z1 ) ⊢∗M1 (qk , ϵ, ϵ)

2.1 Equivalence of PDA by Final state to Null Store


Given M1 PDA by final state which recognizes a string w, obtain an equivalent machine
Mn to accept the same string w by null store as:

4
October 11, 2023 3 DETERMINISTIC AND NON-DETERMINISTIC PDA

Mn (Q1 ∪ {q0 , qk }, Σ, Γ1 ∪ {Z}, δ1 ∪ P, q0 , Z, ϕ)


where P = {δ(q0 , ϵ, Z) ⊢ (q1 , Z1 Z), δ(qf , ϵ, γ1 ) ⊢ (qk , ϵ), δ(qk , ϵ, γi ) ⊢ (qk , ϵ)}
The corresponding transition diagram for Mn is:

Figure 6: Pushdown Automaton for Mn

The configuration of the machine Mn is:


δ(q0 , w, Z) ⊢ (q1 , w, Z1 Z) ⊢∗M1 (qf , ϵ, γ) ⊢ (qk , ϵ, γi ) ⊢∗ (qk , ϵ, ϵ)
Since both machines accepted the strings they are called as equivalent
M1 ≡ Mn =⇒ L(M1 ) = L(Mn )

2.2 Equivalence of PDA by Null store to Final State


Given M2 PDA by null store which recognizes a string w, obtain an equivalent machine
Mf to accept the same string w by final as:
Mf (Q2 ∪ {q0 , qf }, Σ, Γ2 ∪ {Z}, δ2 ∪ P, q0 , Z, {qf })
where P = {δ(q0 , ϵ, Z) ⊢ (q1 , Z2 Z), δ(qk , ϵ, Z) ⊢ (qf , Z)}
The corresponding transition diagram for Mf is:

Figure 7: Pushdown Automaton for Mf

The configuration of the machine Mn is:


δ(q0 , w, Z) ⊢ (q2 , w, Z2 Z) ⊢∗M2 (qk , ϵ, Z) ⊢ (qf , ϵ, Z)
Since both machines accepted the strings they are called as equivalent
M2 ≡ Mf =⇒ L(M2 ) = L(Mf )

3 Deterministic and Non-Deterministic PDA


A deterministic pushdown automaton is formally defined as a 7-tuple: M (Q, Σ, Γ, δ, q0 , Z0 , F )
such that:

5
October 11, 2023 3 DETERMINISTIC AND NON-DETERMINISTIC PDA

• ∀q ∈ Q, a ∈ Σϵ and X ∈ Γ then δ(q, a, X)has at most one element. In other words,


the PDA has only one move from the state q on input symbol a when X is on top of
the stack.

• ∀q ∈ Q, X ∈ Γ, if δ(q, ϵ, X) ̸= Φ then δ(q, a, X) = Φ, ∀a ∈ Σ. In other words, if there


is a move on ϵ from a state q, there should be no other move on any input symbol.

For any PDA to be deterministic, both the above conditions must be satisfied; otherwise, if
any condition fails, then the PDA is non-deterministic. In other words, if there are multiple
moves from the same state on the same input symbol as well as from the same state there
are transitions on both input symbols as well as ϵ then such PDA is called non-deterministic.
The PDA in problem 1 is deterministic as the two conditions are satisfied.

Problem 3

Design PDA for L={wwR |w ∈ {a, b}∗ } and show it is non-deterministic.

Solution. The language L = {aa,bb,abba,baba, abaaba,.....} where each string is a palin-


drome. Nod design a PDA to accept such types of strings as:

Figure 8: Pushdown Automaton for wwR

where the transition are :

• {δ(q0, a, z) ⊢ (q0, az),

• δ(q0, a, a) ⊢ {(q0, aa), (q1, ϵ)},

• δ(q0, b, z) ⊢ {(q0, bz), (q1, ϵ)},

• δ(q0, b, b) ⊢ (q0, bb),

• δ(q0, a, b) ⊢ (q0, ab),

• δ(q0, b, a) ⊢ (q0, ba),

6
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA

• δ(q1, a, a) ⊢ (q1, ϵ),

• δ(q1, b, b) ⊢ (q1, ϵ),

• δ(q1, ϵ, z) ⊢ (qf, z)}

To trace out the string ”abbbba” the ID is as follows: δ(q0, abbbba, z) ⊢ δ(q0, bbbba, az) ⊢
δ(q0, bbba, baz) ⊢ δ(q0, bba, bbaz) ⊢ δ(q1, ba, baz) ⊢ δ(q1, a, az) ⊢ δ(q1, ϵ, z) ⊢ δ(q2, ϵ, z)
∵ after reading the string ”abbba”, the pda reaches to a final state, the string is accepted.
Since the transitions have multiple actions:

• δ(q0, a, a) ⊢ {(q0, aa), (q1, ϵ)},

• δ(q0, b, z) ⊢ {(q0, bz), (q1, ϵ)},

the above PDA is considered to be non-deterministic.

3.1 Difference between DPDA and NPDA

Table 1: Difference between DPDA and NPDA

DPDA NPDA
(Deterministic Pushdown Automata) (Non-deterministic Pushdown Automata)

Less powerful than NPDA More powerful than DPDA


every DPDA can be converted to a corre- Not possible to convert every NPDA to a corre-
sponding NPDA sponding DPDA.
DPDA accepts DCFL(Deterministic The language NPDA accepts is called
Context-free Language), a subset of NCFL(Non-deterministic Context-free Lan-
NCFL(Non-deterministic Context-free guage).
Language) accepted by NPDA.
DCFL ⊆ NFCL NCFL ⊈ DCFL

4 Correspondence between CFG and PDA


CFG and PDA are equivalent in power: a CFG generates a context-free language and
a PDA recognizes a context-free language. So, one is given we can convert into other to
recognize the same context-free language.
i.e., if a grammar G is context-free, we can build an equivalent nondeterministic PDA
which accepts the language that is produced by the context-free grammar G. A parser can
be built for the grammar G. Also, if P is a pushdown automaton, an equivalent context-free
grammar G can be constructed where L(G) = L(P)
This is a useful application where it allows a CFG to be used to specify a programming
language and the equivalent PDA to be used to implement its compiler.

7
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA

4.1 Equivalence of CFG to PDA


Algorithm: If L is a context-free language obtained from the grammar G, obtain an
equivalent PDA M to recognize the same L. Method to convert given CFG G into
PDA M: Input: CFG G = (V, T, P, S)
Output: PDA M = (Q, Σ, Γ, δ, q0 , Z, F )

• Bring the productions of the G into GNF (Optional).

• Let q be the only state of the non deterministic PDA M.

• The start symbol of CFG S will act as initial pushdown store symbol of the PDA

• All non-terminals or variables of the CFG will be the stack symbols ’Γ’ of the PDA
and all the terminals of the CFG will be the input symbols ’Σ’ of the PDA.

• For each production in the form A → α in the CFG G


(where A ∈ V, α ∈ (V ∪ T )∗ ) add a transition to M as δ(q, ϵ, A) ⊢ (q, α).

• For each production in the form A → aα in the CFG G


(where A ∈ V, α ∈ V ∗ ) add a transition to M as δ(q, a, A) ⊢ (q, α).

• For every terminal a ∈ T add the transition (q, a, a) ⊢ (q, ϵ)

Problem 4

Obtain an equivalent non deterministic PDA from the given CFG G({S, X}, {a, b}, P, S)
where where the productions P= {S → XS|ϵ, A → aXb|Xb|ab}.

Solution. Let the equivalent PDA M = ({q}, {a, b}, {a, b, X, S}, δ, q, S, Φ) where δ is defined
as follows:

• For all productions A → α

• S → XS|ϵ =⇒ δ(q, ϵ, S) ⊢ {(q, XS), (q, ϵ)}

• A → aXb|Xb|ab =⇒ δ(q, ϵ, A) ⊢ {(q, aXb), (q, Xb), (q, ab)}

• For all Terminals

• δ(q, a, a) ⊢ (q, ϵ), δ(q, b, b) ⊢ (q, ϵ)

Problem 5

Convert the following grammars into equivalent PDA’s 1. G1 = {S → 0S1|A, A →


1A0|S|ϵ}
2. G2 = {S → 0BB, B → 0S|1S|0}
3. G3 = {S → aSb|a|b|ϵ}
4. G4 = {S → ASB|ab, A → aA|a, B → bB|b}

8
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA

5. G5 = {S → 0CC|ab, C → 1S|0S|0}

4.2 Equivalence of PDA to CFG


Given a PDA M(Q, ΣΓ, δ, q0 , Z0 , Φ) accepting a language L(M) by null store, the corre-
sponding CFG G(V, T, P, S) can be generated as follows:

• Rule1: The set of Variables (V) is constructed as follows:


V = {S} ∪ {[qi , Z, qj ]} ∀qi , qj ∈ Q&Z ∈ Γ

• Rule2: The production fo start symbol S are defined as follows:


S → [q0 , Z0 , q] where q0 is the initiate state of PDA M,
The S production is created for every q ∈ Q

• Rule3: The transition function involving the pop symbol from the pushdown store
such as δ(qi , a, Z) ⊢ (q, ϵ) leads to the production
[qi , Z, q] → a

• rule4: The transition function that involves pushing a symbol unto the stack my
either keep the pushdown store unchanged or push a string of symbols unto the stake
as represented by the following transition:
δ(qi , a, Z) ⊢ (q, Zz1 Z2 ....Zm ] leads to the production
[qj , Z, q] → a[q0 , z1 , q1 ][q1 , Z2 , q2 ], ....[qm − 1, Zm , Qm ]
where q1 , q2 ....qm is the possible list of states for the PDA.

• Rule5: After obtaining all the productions, identify the variables and productions
that are not participating in the grammar and remove them.
Finally, rename all the remaining variables and present the obtained grammar?

Problem 6

Convert the following PDA M to a CFG G. M = (q0 , q1 , 0, 1, X, Z0 , δ, q0 , Z0 , Φ)


with δ:

– δ(q0 , 0, Z0 ) ⊢ (q0 , XZo ) (1a)


– δ(q0 , 0, X) ⊢ (q0 , XX) (1b)
– δ(q0 , 0, X) ⊢ (q1 , ϵ) (2a)
– δ(q1 , 1, X) ⊢ (q1 , ϵ) (2b)
– δ(q1 , ϵ, X) ⊢ (q1 , ϵ) (2c)
– δ(q1 , ϵ, Z0 ) ⊢ (q1 , ϵ) (2d)

Solution. We need to obtain a CFG G (V,T,P,S) as follows: From the given PDA
M (Σ), T = 0,1. Let us make the Start Symbol as S. The possible variables (V) are:

9
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA

V = (S, [q0 , X, q0 ], [q0 , X, q1 ], [q1 , X, q0 ], [q1 , X, q1 ], [q0 , Z0 , q0 ], [q0 , Z0 , q1 ], [q1 , Z0 , q0 ], [q1 , Z0 , q1 ])


The remaining are productions. Let us apply the rules from 2 to 4 as follows:
Based on rule 2, the S productions are: S → [q0 , Z0 , q0 ] ,S → [q0 , Z0 , q1 ]
Based on rule 3, the production where the transition involves popping of a symbol
from the stack are:
The transition δ(q0 , 0, X) ⊢ (q1 , ϵ) leads to the production [q0 , X, q1 ] → 0.
The transition δ(q1 , 1, X) ⊢ (q1 , ϵ) leads to the production [q1 , X, q1 ] → 1.
The transition δ(q1 , ϵ, X) ⊢ (q1 , ϵ) leads to the production [q1 , X, q1 ] → ϵ.
The transition δ(q1 , ϵ, Z0 ) ⊢ (q1 , ϵ) leads to the production [q1 , Z0 , q1 ] → ϵ.
Based on rule 4, the production where the transition involves pushing of a symbol unto
the stack are:
The transition δ(q0 , 0, Z0 ) ⊢ (q0 , XZo ) leads to the following productions:

– [q0 , Z0 , q0 ] → 0[q0 , X, q0 ][q0 , Z0 , q0 ] (1a1)


– [q0 , Z0 , q0 ] → 0[q0 , X, q1 ][q1 , Z0 , q0 ] (1a2)
– [q0 , Z0 , q1 ] → 0[q0 , X, q0 ][q0 , Z0 , q1 ] (1a3)
– [q0 , Z0 , q1 ] → 0[q0 , X, q1 ][q1 , Z0 , q1 ] (1a4)

The transition δ(q0 , 0, X) ⊢ (q0 , XX) leads to the following productions:

– [q0 , X, q0 ] → 0[q0 , X, q0 ][q0 , X, q0 ] (1b1)


– [q0 , X, q0 ] → 0[q0 , X, q1 ][q1 , X, q0 ] (1b2)
– [q0 , X, q1 ] → 0[q0 , X, q0 ][q0 , X, q1 ] (1b3)
– [q0 , X, q1 ] → 0[q0 , X, q1 ][q1 , X, q1 ] (1b4)

Now, we can revisit the obtained production and remove which are not necessary:
The variables [q1 , X, q0 ] and [q1 , Z0 , q0 ] never appear as a LHS of productions, drop
the respective production which contains them: 1a2, 1b2.
Similarly, [q0 , X, q0 ] and [q0 , Z0 , q0 ] always appear on the RHS of any of their own
productions (or just on the RHS), so we can drop these productions: 1a1, 1a3, 1b1,
1b3 as well as the first production from the start symbol.
After removing the productions, the remaining are:

– S → [q0 , Z0 , q1 ]
– [q0 , Z0 , q1 ] → 0[q0 , X, q1 ][q1 , Z0 , q1 ] (A)
– [q1 , Z0 , q1 ] → ϵ. (B)
– [q0 , X, q1 ] → 0[q0 , X, q1 ][q1 , X, q1 ] (C)
– [q0 , X, q1 ] → 0. (C)
– [q1 , X, q1 ] → 1. (D)
– [q1 , X, q1 ] → ϵ. (D)

10
October 11, 2023 5 DECISION ALGORITHMS FOR CFL

After renaming, the productions (P) for the obtained grammar G({S,A,B,C,D},{0,1},P,S)
as follows:

– S→A
– A → 0CB
– B→ϵ
– C → 0CD|0
– D → 1|ϵ

Problem 7

Convert the following PDA’s to equivalent CFG.


1. M = {

• δ(q0 , a, Z0 ) ⊢ (q0 , aZ0 ),

• δ(q1 , a, a) ⊢ (q2 , ϵ),

• δ(q0 , a, a) ⊢ (q0 , aa),

• δ(q2 , a, a) ⊢ (q2 , ϵ),

• δ(q0 , c, a) ⊢ (qa , a),

• δ(q2 , ϵ, Z0 ) ⊢ (q2 , ϵ)}

2. M = {

• δ(q0 , 1, Z0 ) ⊢ (q0 , KZ0 ),

• δ(q0 , 0, K) ⊢ (q1 , K),

• δ(q0 , ϵ, Z0 ) ⊢ (q0 , ϵ),

• δ(q0 , 1, K) ⊢ (q0 , KK),

• δ(q1 , 0, K) ⊢ (q1 , ϵ),

• δ(q1 , 0, Z0 ) ⊢ (q0 , Z0 )}

5 Decision Algorithms for CFL


The following are the fundamental questions that need to e answered about CFL’s.

5.1 Deciding whether the CFL is non-empty or not


Let a CFG G(V,T,P,S) be corresponds to CFL L. L(G) is non empty if and only if that
start symbol generates or derives some strings of terminals.

i.e., if S =⇒ w - then L(G) is non-empty otherwise L(G) is empty.

11
October 11, 2023 5 DECISION ALGORITHMS FOR CFL

5.2 Deciding whether the CFL is finite or infinite


First bring the given CFG into CNF. Then draw a direct acyclic graph for the CFG in
such a way that all the variables are nodes and
if there is a production A → BC then draw an edge for A → B&A → C.
In the obtained DAG, if there is a cycle found, then the L(G) is infinite otherwise if there
is no cycle then the L(G) is finite.

5.3 Deciding wheter a given string is in the CFL or not


For a given CFG G(V,T,P,S) and a string w ∈ T ∗ , is w ∈ L(G)?
To find it in a better way the scientist named Cocke-Younger-Kasami proposed an algo-
rith also known as CYK algorithm. To apply the algorithm the grammar must be in CNF.
It uses dynamic programming where it is based on the principle that the solution to problem
[i, j] can be constructed from solution to sub-problem [i, k] and solution to sub problem [k,
j]. Its time complexity is O(n3 |w|)
For that a triangular table where

• Each row of the table corresponds to one particular length of sub strings.

• Bottom most row corresponds to strings of length-1.

• Second row from bottom corresponds to strings of length-2.

• Third row from bottom corresponds to strings of length-3.

• Top most row from bottom corresponds to the given string of length-n.

Example: For a given string “x” of length four units, the triangular table looks like-

7cm

In the above table each cell of xij with it Vij where xij represents the sub-string of x starting
from location i and has length j.
Example: For the string x = abcd is a strin, there are n(n+1)/2 possible sub-strings.
i.e., 4 x (4+1) / 2 = 10. They are:

x14 = abcd
x13 = abc x23 = bcd
x12 = ab x22 = bc x32 = cd
x11 = a x21 = b x31 = c x41 = d

Table 2: CYK Table

and Vij represents a set of variables in the grammar which can derive the sub string xij .
If the set of variables consists of the start symbol, then it becomes sure-

• Sub string xij can be derived from the given grammar.

• Sub string xij is a member of the language of the given grammar.

12
October 11, 2023 5 DECISION ALGORITHMS FOR CFL

Problem 8

For the given grammar, check the acceptance of string w = baaba using CYK Algo-
rithm. G={S → AB|BC, A → BA|a, B → CC|b, C → AB|a}

Solution. First, let us draw the triangular table for the given string x = baaba. Since the
length of given string = |x| = 5, the possible sub strings = (5 x 6) / 2 = 15.
The triangular table looks like:

Now fill the cells of the matrix row by row as follows: Filling of each row means finding the
possibility its parent while constructing a parse tree from bottom-up (starting from leaves)
Filling first row
let x11 , x12 , x13 , x14 , x15 = {b,a,a,b,a} be the sub-strings of length 1. (leaves of parse
tree)

• For sub-string x11 = b and ∵ B → b so, the possible parent(s) V11 = {B}

• For sub-string x12 = a and ∵ A → a & C → a so, the possible parent(s) V21 = {A, C}

• For sub-string x13 = a and ∵ A → a & C → a so, the possible parent(s) V31 = {A, C}

• For sub-string x14 = b and ∵ B → b so, the possible parent(s)V41 = {B}

• For sub-string x15 = a and ∵ A → a & C → a so, the possible parent(s)V51 = {A, C}

Filling Second row


Now the sub-strings of x21 , x22 , x22 , x24 = {ba,aa,ab,ba}

• The sub-string x21 = ba obtained from the variables V11 , V12 .


V11 , V12 = {B}.{A, C} = {BA, BC}.
From G the corresponding productions which derives {BA, BC} are A → BA&S →
BC.
So, the possible parent(s) V12 = {S, A}

• The sub-string x22 = aa obtained from the variable V12 , V13 .


V12 , V13 = {A, C}.{A, C} = {AA, AC, CC}.
From G the corresponding productions which derives {AA, AC, CC} are B → CC.
So, the possible parent(s) V22 = {b}

13
October 11, 2023 5 DECISION ALGORITHMS FOR CFL

• For sub-string x23 = ab obtained from the variable V13 , V14 .


V13 , V14 = {A, C}.{B} = {AB, CB}.
From G the corresponding productions which derives {AB, CB} are
S → AB&C → AB.
So, the possible parent(s) V32 = {S, C}

• For sub-string x24 = ba obtained from the variable V14 , V15 .


V14 , V15 = {B}.{A, C} = {BA, BC}.
From G the corresponding productions which derives {BA, BC} are A → BA&S →
BC.
So, the possible parent(s) V42 = {S, A}

Filling Third row


Now the sub-strings of x31 , x32 , x33 = {baa,aab,aba}

• The sub-string x31 = baa obtained from the different combinations of sub-strings such
as {b,aa} and {ba ,a} and the corresponding variables are {V11 , V22 } and {V21 , V13 }
{V11 , V22 } = {B}.{B} = {BB} and {V13 , V21 } = {S, A}.{A, C} = {sA, SC, AA, AC}
From G the corresponding productions which derives {BB}&{sA, SC, AA, AC} are
{}
So, the possible parent(s) V31 = {Φ}

• The sub-string x32 = aab obtained from the different combinations of sub-strings such
as {a,ab} and {aa ,b} and the corresponding variables are {V12 , V23 } and {V14 , V22 }
{V12 , V23 } = {A, C}.{S, C} = {AS, AC, CS, CC} and {V14 , V22 } = {B}.{B} = {BB}
From G the corresponding productions which derives {AS, AC, CS, CC}&{BB} are
{B → CC}
So, the possible parent(s) V32 = {B}

• The sub-string x33 = aba obtained from the different combinations of sub-strings such
as {a,ba} and {ab,a} and the corresponding variables are {V13 , V24 } and {V15 , V23 }
{V13 , V24 } = {A, C}.{S, A} = {AS, AA, CS, CA} and {V15 , V23 } = {A, C}.{S, C} =
{AS, AC, CS, CC}
From G the corresponding productions which derives {AS, AA, CS, CA}&{AS, AC, CS, CC}
are {B → CC}
So, the possible parent(s) V32 = {B}

Filling fourth row


Now the sub-strings of x41 , x42 = {baab,aaba}

14
October 11, 2023 5 DECISION ALGORITHMS FOR CFL

• The sub-string x41 = baab obtained from the different combinations of sub-strings
such as {b,aab}, {ba ,ab}, {baa,b} and the corresponding variables are {V11 , V32 },
{V21 , V23 }, {V31 , V14 }
{V11 , V32 } = {B}.{B} = {BB}
{V21 , V23 } = {S, A}.{S, C} = {sS, SC, AS, AC}
{V31 , V14 } = Φ.{B} = Φ
From G the corresponding productions which derives {BB}, {sS, SC, AS, AC}&Φ are
{}
So, the possible parent(s) V41 = {Φ}

• The sub-string x42 = aaba obtained from the different combinations of sub-strings
such as {a,aba}, {aa ,ba}, {aab,a} and the corresponding variables are {V12 , V33 },
{V22 , V24 }, {V32 , V15 }
{V12 , V33 } = {A, C}.{B} = {AB, CB}
{V22 , V24 } = {B}.{S, A} = {BS, BA}
{V32 , V15 } = {B}.{A, C} = {BA, BC}
From G the corresponding productions which derives {AB, CB}, {BS, BA}&{BA, BC}
are {S → AB, A → BA, C → AB, S → BC}
So, the possible parent(s) V42 = {S, A, C}

Filling fifth row


Now the sub-string of x51 = {baaba}

• The sub-string x51 = baaba obtained from the different combinations of sub-strings
such as {b,aaba}, {ba ,aba}, {baa,ba}, {baab,a} and the corresponding variables are
{V11 , V42 }, {V21 , V33 }, {V31 , V24 }, {V41 , V15 }
{V11 , V42 } = {B}.{S, A, C} = {BS, BA, BC}
{V21 , V33 } = {S, A}.{B} = {sB, AB}
{V31 , V24 } = Φ.{S, A} = Φ
{V41 , V15 } = Φ.{A, C} = Φ
From G the corresponding productions which derives {BS, BA, BC}, {sB, AB} are
{A → BA, S → BC, C → AB}
So, the possible parent(s) V51 = {S, A, C}

After filling the entries of triangular table :

15
October 11, 2023 5 DECISION ALGORITHMS FOR CFL

Since the root entry such as V51 ={S,A,C} contains start symbol, the string x=baaba is
belongs to the given CFL or can be derived from the grammar G.

Problem 9

Solve the following problems using CYK algorithm.


1. G1={S → XY, T → ZT |a, X → T Y, Y → Y T |b, Z → T Z|b} with a string
x=abab.
2. G2={S → AB|BB, A → CC|AB|a, B|CA|b, C → BA|AA|b} with a string
x=baaba.

16

You might also like