You are on page 1of 19

Theory of Computation (With Automata Theory)

TOPIC TITLE: Equivalence of PDA and CFG (Part 1)


Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
1. Explain the relationship between pushdown automata and
context-free grammars.
2. Convert any context-free grammar into its equivalent pushdown
automaton.
Affective:
1. Listen to others with respect.
2. Participate in class discussions actively.

MATERIALS/EQUIPMENT:
o
o

topic slides
OHP

TOPIC PREPARATION:
o
o
o
o

Equivalence of PDA and CFG (Part 1)

Have the students review related topics that discussed in


previous courses.
Prepare the slides to be presented in class.
It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics.
Prepare additional examples on the topic to be presented.

*Property of STI
Page 1 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 1 of 31

Equivalence of Pushdown Automata and Context-Free Grammars


Pushdown Automata and Context-Free Grammars
Introduction
Based on previous discussions, a context-free grammar can generate the strings of
the context-free language it represents. While a pushdown automaton can
recognize a context-free language since it can determine if a certain string belongs
to that language. Technically speaking, a PDA and a CFG are then equal in
power.
This section will formally explore the relationship between pushdown automata and
context-free grammars. Specifically, the discussions will now try to establish
whether there is equivalence between the two.

Equivalence of PDA and CFG


Page 2 of 31

Recall that a context-free language is any language whose strings can be


generated by some context-free grammar.
And since PDAs recognize context-free languages, can it then be claimed that
A language is context-free if and only if some
pushdown automaton recognizes it?

To answer the question, it must be shown that:


1.

If a language is context free, then some pushdown automaton recognizes


it.
Assume that there is a context-free language L. Since it is context-free,
there is some context-free grammar G that describes it. To show that
there is a pushdown automaton that recognizes L, a procedure must be
established to construct a PDA for language L based on grammar G.
This will be discussed in this section.

2.

If a pushdown automaton recognizes some language, then it is contextfree.


Assume that there is a PDA P which recognizes a certain language L. To
determine if language L is a context-free language, it must be shown that
there is some context-free grammar that describes it. There must then be
a procedure established for constructing a CFG based on PDA P.
This will be discussed in the next section.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 2 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 3 of 31

Context-Free Grammar to Pushdown Automaton


The following discussions will show that a language is context-free if and only if
some pushdown automaton recognizes it.
Assume that there is a context-free language L. Since it is context-free, there is
some context-free grammar G that can be used to generate or derive the strings of
the language.
To show that context-free language L has some pushdown automaton that
recognizes it, a procedure must be established to convert context-free grammar G
of language L into its equivalent pushdown automaton P.

Equivalence of PDA and CFG


Page 4 of 31

To facilitate the discussion, a case study will be used. Assume a certain contextfree language L1 is represented by context-free grammar G1:
S AB
A 0A 0
B 0B1
i j

This grammar actually represents the language L1 = {0 1 i > j}. Take note that this
language is composed of strings that start with a block of consecutive 0s followed
by a block of consecutive 1s where the total number of consecutive 0s is greater
than the total number of consecutive 1s.
Recall how a grammar is used to generate a string of the language. As a review,
the leftmost derivation of string 0001 using grammar G1 is shown below:
S AB
0AB
00B
000B1
0001

substituted A with 0A
substituted A with 0
substituted B with 0B1
substituted B with

Recall that a string can be derived from a grammar by performing a series of


substitutions wherein the right-hand side variables are replaced by variables and/or
terminals using the appropriate rules of the grammar until no variables are left.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 3 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 5 of 31

To show that language L1 has a pushdown automaton that recognizes it, a PDA P1
must be constructed based on context-free grammar G1. In other words, the
context-free grammar must be converted to a pushdown automaton.
The PDA P1 that will be constructed from grammar G1 should be able to determine
if an input string belongs to context-free language L1.
To do this, P1 must have the capability to determine whether there is a series of
substitutions using the rules of grammar G1 that can generate the input string. P1
should be able to simulate the leftmost derivation of the input string. If there is
such a derivation, the string will be accepted. Otherwise, it will be rejected.
In the simulation of the leftmost derivation of a string, the stack of the PDA can be
used to store or record the steps of the derivation.

Equivalence of PDA and CFG


Page 6 of 31

Here is an example of how the stack of a PDA is used in simulating the leftmost
derivation of the string 0001 using grammar G1:
1.

First push the start variable S onto the stack. The stack contents will be

top of the
stack

This step is simply showing that the derivation begins with the start
variable S.
2.

The next step in the derivation process is to replace the variable S with
the right-hand side string of a rule for S. Using the rule S AB, remove
S from the stack and replace it with the string AB. The stack contents will
now be

top of the
stack

A
B

Since this is the leftmost derivation, the right-hand side string should be
pushed onto the stack such that the leftmost part of the string is at the top
of the stack. This is done so that the PDA can easily access the leftmost
variable for subsequent substitutions. If this were using the rightmost
derivation, then the rightmost part of the string should be at the top of the
stack.
At this point in time, the derivation string is AB.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 4 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 7 of 31

3.

The next step is to replace the leftmost variable A of the derivation string
with the right-hand side string of a rule for A. Using the rule A 0A,
remove A from the stack and replace it with the string 0A. The stack
contents will now be

top of the
stack

0
A
B

The derivation string at this point is the string 0AB.


4.

Since the leftmost part of the derivation string is a terminal (0), this will be
the first symbol of the string being derived. The next step is then to
remove 0 from the stack. Therefore, the string derived so far is 0 and the
stack contents will now be

top of the
stack

string
derived

B
The derivation string at this point is the string AB.
Equivalence of PDA and CFG
Page 8 of 31

5.

The next step is to replace the leftmost variable A of the derivation string
with the right-hand side string of a rule for A. Using the rule A 0,
remove A from the stack and replace it with 0. The stack contents will
now be

top of the
stack

string
derived

B
The derivation string at this point is the string 0B.
6.

Since the leftmost part of the derivation string is a terminal (0), this will be
the second symbol of the string being derived. The next step now is to
remove 0 from the stack. Therefore, the string derived so far is 00 and
the stack contents will now be

string
derived
top of the
stack

0 0

The derivation string at this point is the string B.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 5 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 9 of 31

7.

The next step is to replace the leftmost variable B of the derivation string
with the right-hand side string of a rule for B. Using the rule B 0B1,
remove B from the stack and replace it with the string 0B1. The stack
contents will now be

top of the
stack

0
B

string
derived

0 0

1
The derivation string at this point is the string 0B1.
8.

Since the leftmost part of the derivation string is a terminal (0), this will be
the third symbol of the string being derived. The next step now is to
remove 0 from the stack. Therefore, the string derived so far is 000 and
the stack contents will now be

top of the
stack

string
derived

0 0 0

1
The derivation string at this point is the string B1.
Equivalence of PDA and CFG
Page 10 of 31

9.

The next step is to replace the leftmost variable B of the derivation string
with the right-hand side string of a rule for B. Using the rule B ,
remove B from the stack (no need to push anything onto the stack since
B is being replaced by the empty string). The stack contents will now be

string
derived
top of the
stack

0 0 0

The derivation string at this point is the string 1.


10. Since the leftmost part of the derivation string is a terminal (1), this will be
the fourth symbol of the string being derived. The next step now is to
remove 1 from the stack. Therefore, the string derived so far is 0001 and
the stack contents will now be

string
derived

0 0 0 1

top of the
stack

Since the stack is now empty, the PDA was therefore able to simulate the
leftmost derivation of the string 0001 using grammar G1.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 6 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 11 of 31

Hence, to determine if an input string is to be accepted, the PDA P1 that will be


constructed should be able to perform the following actions:

Equivalence of PDA and CFG


Page 12 of 31

1.

Before any computation starts, P1 pushes the stack empty symbol $ onto
the stack.

2.

P1 then pushes the start symbol of the grammar onto the stack.

3.

The following actions are then repeated:


a.

If the top of stack is a variable, pop that variable from the stack.
Then, select one of the rules for that variable and push the righthand side string of that rule onto the stack.

b.

If the top of stack is a terminal, pop that terminal from the stack.
Then, compare that terminal with the next symbol of the input
string. If they match, repeat step 3 and processing continues. If
they do not match, reject the string.

c.

If the top of stack is the stack empty symbol $ (the stack is


empty) and there are no more input symbols. Accept the input
string (the PDA goes to a final state).
If the stack is empty and there are input symbols remaining, or if
the stack is not empty and there are no more input symbols,
reject the input string.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 7 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 13 of 31

Based on the series of actions given, the following will now be performed by P1 in
order to determine if the string 0001 belongs to language L1:
1.

P1 will push the stack empty symbol $ onto the stack. The stack contents
will now be

top of the
stack

2.

P1 will push the start symbol S onto the stack. The stack contents will
now be

top of the
stack

S
$

Equivalence of PDA and CFG


Page 14 of 31

3.

Since the symbol at the top of the stack is a variable (S), pop it out from
the stack and push the right-hand side string of a rule for S (S AB)
onto the stack. The stack contents will now be

top of the
stack

A
B
$

4.

Since the symbol at the top of the stack is a variable (A), pop it from the
stack and push the right-hand side string of a rule for A (A 0A) onto the
stack. The stack contents will now be

top of the
stack

0
A
B
$

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 8 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 15 of 31

5.

Since the symbol at the top of the stack is a terminal (0), pop this from the
stack and compare it with the first incoming input symbol (0). Since they
match, the computation continues. The stack contents will now be

top of the
stack

A
B
$

6.

Since the symbol at the top of the stack is a variable (A), pop it from the
stack and push the right-hand side string of a rule for A (A 0) onto the
stack. The stack contents will now be

top of the
stack

0
B
$

Proof of Induction
Page 16 of 31

7.

Since the symbol at the top of the stack is a terminal (0), pop this from the
stack and compare it with the second incoming input symbol (0). Since
they match, the computation continues. The stack contents will now be

top of the
stack

B
$

8.

Since the symbol at the top of the stack is a variable (B), pop it out from
the stack and push the right-hand side string of a rule for B (B 0B1)
onto the stack. The stack contents will now be

top of the
stack

0
B
1
$

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 9 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 17 of 31

9.

Since the symbol at the top of the stack is a terminal (0), pop this from the
stack and compare it with the third incoming input symbol (0). Since they
match, the computation continues. The stack contents will now be

top of the
stack

B
1
$

10. Since the symbol at the top of the stack is a variable (B), pop it from
stack and push the right-hand side string of a rule for B (B ) onto
stack. But since the right-hand side string of the selected rule is
empty string, there is no need to push anything. The stack contents
now be

top of the
stack

the
the
the
will

1
$

Equivalence of PDA and CFG


Page 18 of 31

11. Since the symbol at the top of the stack is a terminal (1), pop this from the
stack and compare it with the fourth incoming input symbol (1). Since
they match, the computation continues. The stack contents will now be

top of the
stack

12. Since the stack is empty and there are no more input symbols left, the
string is accepted by PDA P1.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 10 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 19 of 31

One problem in the series of actions outlined is that there will be situations where
there will be several choices on which rule to use in substituting a certain variable.
As an example, take note that there are two rules for variable A in grammar G1.
One is the rule A 0A while the other one is the rule A 0. Hence, if the symbol
at the top of the stack is A, the PDA has two options:
1.

One option is to pop variable A from the stack and then push the righthand side string of the rule A 0A onto the stack. In other words, pop A
and then push 0A.

2.

The other option is to pop variable A from the stack and then push the
right-hand side string of the rule A 0 onto the stack. In other words,
pop A and then push 0.

The same case is also true for variable B.


The question now is which option will the PDA choose? One option may cause the
PDA to accept the string while the other may not.
The answer lies in the nondeterministic nature of PDAs. The PDA will make
nondeterministic guesses on which rule to use by doing several computations at
the same time. In the given example, the PDA will make two copies of itself. One
copy will proceed with the computation using the rule A 0A while the second
copy will continue processing the input string using the rule A 0.
In order words, the PDA will try to use all possible rules for substituting a certain
variable by making nondeterministic guesses. If the string is indeed part of the
language of the PDA, then one or more copies of the PDA will end up in a final
state. If the string does not belong to the language, then all copies of the PDA will
just die.
Equivalence of PDA and CFG
Page 20 of 31

Observe that the PDA to be constructed requires that an entire string is pushed
onto the stack.
Example:
In step 8 of the previous example, when the symbol at the top of the stack was B, it
was popped out of the stack and the string 0B1 was pushed onto the stack. This is
shown in the figure below:

0
B
B

before

after

Take note again that the leftmost symbol of the string to be pushed should be at
the top of the stack. In this example, push first the symbol 1, then B, and then
finally 0.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 11 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 21 of 31

If this action is represented by a state diagram, it would look like:

The transition edge from state qi to state qj is , B 0B1. Recall that this label
means that even without an input symbol, if the symbol at the top the stack is B,
then the PDA will pop B from the stack and then move from state qi to state qj, and
at the same time push the string 0B1 onto the stack. This transition can be written
as
(qi, , B) = (qj, 0B1)
This transition equation means that if the PDA is at state qi and there is no input
symbol, and the symbol at the top of the stack is B, the PDA will move to state qj
and push 0B1 onto the stack. Remember that this implies that the symbol at the
top of the stack was popped before moving to the new state.
However, PDAs can push only one symbol onto the stack as it moves from one
state to another. There is no provision for the PDA to push an entire string in only
one state transition.
Equivalence of PDA and CFG
Page 22 of 31

The pushing of an entire string onto the stack can be implemented by introducing
intermediate states between states qi and qj. Instead of having the PDA move from
state qi to state qj and push the string 0B1 onto the stack, the PDA will perform the
following:
1.

2.

3.

If the symbol at the top of the stack is B and there is no input symbol,
move from state qi to an intermediate state qx and then push 1 onto the
stack. In other words, (qi, , B) = (qx, 1).
Then, from intermediate state qx, move right away to another intermediate
state qy while pushing B onto the stack. In other words, (qx , ) = (qy,
B).
Last, from intermediate state qy, move right away to the destination
state qj while pushing 0 onto the stack. In other words, (qy , ) = (qj, 0).

The state diagram can now be revised so that only one symbol is pushed onto the
stack for each state transition. This is seen in the state diagram below:

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 12 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 23 of 31

Recall the procedure presented earlier on how the PDA to be constructed


processes input strings.
1.

Before any computation starts, P1 pushes the stack empty symbol $ onto
the stack.
2. P1 pushes the start symbol of the grammar onto the stack.
3. Repeat the following actions:
a. If the top of stack is a variable, pop that variable from the stack.
Then, select one of the rules for that variable and push the righthand side string of that rule onto the stack.
b. If the top of stack is a terminal, pop that terminal from the stack.
Then, compare that terminal with the next symbol from the input.
If they match, repeat step 3 and processing continues. If they
do not match, reject the string.
c. If the top of stack is the stack empty symbol $ (the stack is
empty) and there are no more input symbols, accept the input
string (the PDA goes to a final state).

This PDA will therefore have three major states representing the actions it has to
perform in determining whether a string belongs to a certain language:

Equivalence of PDA and CFG


Page 24 of 31

1.

qstart this is the start state where the stack empty symbol $ and then the
start variable S of the grammar will be pushed onto the stack. This takes
care of steps 1 and 2 listed above.

2.

qloop this state is where the major processing occurs. This takes care of
steps 3a, 3b, and 3c listed above

3.

qfinal this is the final or accept state. The PDA goes to this state from
state qloop when the stack is empty and there are no more input symbols.

The transition function of the PDA to be constructed should be as follows:


The start state qstart will have a transition edge to state qloop with the label
(qstart, , ) = (qloop, S$)
This transition simply states that when the PDA is at state qstart, it will move
right away to state qloop and at the same time push the string S$ onto the
stack. The stack empty symbol $ goes inside the stack first followed by the
start variable S of the grammar.

The transition edges for state qloop should be able to handle the following
situations:
1.
2.
3.

Equivalence of PDA and CFG (Part 1)

The symbol at the top of the stack is a variable (step 3a).


The symbol at the top of the stack is a terminal (step 3b).
The symbol at the top of the stack is the stack empty symbol $
indicating that the stack is already empty (step 3c).

*Property of STI
Page 13 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 25 of 31

Case 1:
If the top of the stack symbol is a variable A, there should be a transition edge
with label
(qloop, , A) = (qloop, w)
where w is the right-hand side string of the rule A w used to replace A.
Hence, if the symbol at the top of the stack is a variable A, the PDA moves
from state qloop to qloop and pushes the right-hand side string (w) of a rule for
variable A. As mentioned earlier, if there are several rules for variable A, the
nondeterministic nature of the PDA will allow it to try out all possible rules.
Hence, there must be a transition edge of this type for each rule for each
variable.

Case 2:
If the top of the stack symbol is a terminal a, there should be a transition edge
with label
(qloop, a, a) = (qloop, )
Hence, if the symbol at the top of the stack is a terminal a, the PDA must pop
this terminal and compare it to next incoming input symbol. If the input
symbol is also a (they match), then the PDA simply moves from qloop to qloop
without pushing anything onto the stack. Since there is a match, processing
therefore continues. If the terminal at the top of the stack does not match the
incoming input symbol, the PDA cannot go anywhere and processing then
dies. The input string is therefore rejected. Take note that there must be a
transition edge of this type for each terminal in the grammar.

Case 3:
If the top of the stack symbol is the stack empty symbol $, there should be a
transition edge with label
(qloop, , $) = (qfinal, )
If the symbol at the top of the stack is $ (the stack is empty) and there are no
more input symbols, the PDA moves from qloop to qfinal. The string is therefore
accepted.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 14 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 26 of 31

The generic state diagram for the PDA P to be constructed from context-free
grammar G will be

The following are the transition labels of the given PDA:


1.

From qstart to qloop: , S$


This allows the PDA to move right away from qstart to qloop and push the
string S$ onto the stack.

2.

From qloop to qloop: , A w


If the symbol at the top of the stack is a variable A, this allows the PDA to
move right away from qloop to qloop and substitute variable A with the
string w using the rule A w. In other words, the variable A is popped
out of the stack and the right-hand string of the selected rule is pushed
onto the stack. In the PDA to be constructed, there should be a transition
edge (from qloop to qloop) with this type of label for each rule for each
variable in the grammar to be converted. In converting grammar G1 to
PDA P1, the following transition edges must be added:
, S AB
, A 0A
,A0
, B 0B1
,B

3.

for the rule S AB


for the rule A 0A
for the rule A 0
for the rule B 0B1
for the rule B

From qloop to qloop: a, a


If the symbol at the top of the stack is a terminal a, this allows the PDA to
compare this terminal with the next incoming input symbol. The PDA
pops the terminal from the stack and compares it with the input symbol. If
they are the same, the PDA moves from qloop to qloop and processing
continues. Otherwise, processing dies. In the PDA to be constructed,
there should be a transition edge (from qloop to qloop) with this type of label
for each terminal symbol in the grammar to be converted. In converting
grammar G1 to PDA P1, the following transition edges must be added:
0, 0
1, 1

4.

for the terminal 0


for the terminal 1

From qloop to qfinal: , $


If the symbol at the top of the stack is $ (stack is empty) and there are no
more input symbols, the PDA moves to the final state qfinal and the input
string is accepted.

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 15 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 27 of 31

Therefore, converting the context-free grammar G1 into PDA P1 will result in the
following state diagram:

Equivalence of PDA and CFG


Page 28 of 31

However, the PDA presented still has state transitions wherein an entire string is
pushed onto the stack instead of just one symbol. The transitions being referred to
are the ones with the labels
, S$
, S AB
, A 0A
, B 0B1
Revising PDA P1 so that intermediate states are used to ensure that only one
symbol is pushed onto the stack per state transition will result in the following state
diagram:

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 16 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 29 of 31

As a second example, convert the following context-free grammar G2 to a PDA P2:


S 0T1 1
T T0
The state diagram of the initial PDA P2 is given below:

Equivalence of PDA and CFG


Page 30 of 31

However, the PDA still has state transitions wherein an entire string, instead of just
one symbol, is pushed onto the stack. The transitions being referred to are the
ones with the labels
, S$
, S 0T1
, T T0
Revising PDA P2 so that intermediate states are used to ensure that only one
symbol is pushed onto the stack per state transition results in

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 17 of 19

Theory of Computation (With Automata Theory)

Equivalence of PDA and CFG


Page 31 of 31

Since there is now a procedure to convert a CFG to a PDA, it can therefore be


concluded that if a language is context-free, then some pushdown automaton
recognizes it.

Exercises:
Convert the following context-free grammars to PDAs:
1.

Grammar G3
S AB
A aAb
B cB

2.

Grammar G4
S SS (S)

3.

Grammar G5
S 0 1

0S0 1S1

Solution for # 1:
Initial PDA:

Equivalence of PDA and CFG (Part 1)

Revised PDA:

*Property of STI
Page 18 of 19

Theory of Computation (With Automata Theory)

Solution for # 2:
Initial PDA:

Revised PDA:

Solution for # 3:
Initial PDA:

Revised PDA:

[Equivalence of PDA and CFG, Pages 131 of 31]

Equivalence of PDA and CFG (Part 1)

*Property of STI
Page 19 of 19