Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

In the rst state, q

0
, we simply push a symbols. Then we move to state
q
1
where we either
pop an a when a b is seen on the input, or
push a b for every b seen.
If i > j then there will be more a symbols on the stack than consecutive b
symbols remaining in the input. In this case, some a symbols will be left on
the stack when leaving q
1
. This situation is not catered for in state q
2
, so the
machine will block and the input will not be accepted. This is the correct
behaviour.
On the other hand, if i j, there are more consecutive b symbols than
there are a symbols on the stack, so in state q
1
the stack of i a symbols will
become empty and then be lled with j i b symbols. Entering state q
2
with
such a stack will result in acceptance only if there are j i c symbols left in
the input.
Example 99. Give a PDA for L = {x {a, b}

| count(a, x) = count(b, x)}.


a, b
b, a
a, a
b, b
Example 100. Give a PDA for L = {x {a, b}

| count(a, x) < 2 count(b, x)}.


This problem can be rephrased as: x is in L if, after doubling the number
of bs in x, we have more bs than as. We can build a machine to do this
explicitly: every time it sees a b in the input string, it will treat it as 2
consecutive bs.
144
b, b
, b
a, a
a, b
b, a
, a
, b
, b
, b
Example 101. Give a PDA for L = {a
i
b
j
| i j 2i}.
L is equivalent to {a
i
b
i
b
k
| k i}. This is equivalent to {a
k
a

b
2k
| k, 0}.
A machine dealing with this language is easy to build, provided that dier-
ent functionality is put in dierent states. Non-deterministic transitions take
care of guessing the right time to make a transition. In the rst state, we
deal with a
k
; in the second we deal with a

; and in the last we consume b


2k
.
a, a
, ,
a, a
b, a
b, a
b,
Example 102. Build a PDA to recognize
L = {a
i
b
j
| 2i = 3j}
Thus L = {, a
3
b
2
, a
6
b
4
, a
9
b
6
, . . .}. The idea behind a solution to this
problem is that we wish to push and pop multiple a symbols. On seeing an
a in the input, we want to push two a symbols on the stack. When we see
a b, we want to pop three a symbols. The technical problem we face is that
pushing and popping only deal with one symbol at a time. Thus in order to
deal with multiple symbols, we will need to invent new states.
145
q
0
q
1
q
2
q
3
q
4
a, a
, a
,
b, a
, a
, a
Example 103. Build a PDA to recognize
L = {a
i
b
j
| 2i = 3j}
This is a more dicult problem. We will be able to re-use the basic idea of
the previous example, but must now take extra cases into account. Much as
in the complementation of DFAs, the success state q
2
of the previous example
will change into a reject state. But there is much more going on. We will
build the solution incrementally. The basic skeleton of our answer is
q
0
q
1
q
2
q
3
q
4
a, a
, a
,
b, a
, a
, a
If we arrive in q
2
where the input has been exhausted and the stack is
empty, we should reject, and that is what the above machine does. The other
cases in q
2
are
There is remaining input and the stack is not empty. This case is
already covered: go to q
3
.
There is remaining input and the stack is empty. We can assume that
the head of the remaining input is a b. (All the leading a symbols have
already been dealt with in the q
0
, q
1
pair.) We need to transition to an
accept state where we ensure that the rest of the input is all b symbols.
Thus we invent a new accept state q
5
where we discard the remaining
b symbols in the input.
146
q
0
q
1
q
2
q
3
q
4
q
5
a, a
, a
,
b, a
, a
, a
b,
b,
We further notice that this situation can happen in q
3
and q
4
, so we
add -transitions from them to q
5
:
q
0
q
1
q
2
q
3
q
4
q
5
a, a
, a
,
b, a
, a
,
,
, a
b,
b,
The input is exhausted, but the stack is not empty. Thus we have
excess a symbols on the stack and we need to jettison them before
accepting. This is handled in a new nal state q
6
:
q
0
q
1
q
2
q
3
q
4
q
5
q
6
a, a
, a
,
b, a
, a
,
,
, a
b,
b,
, a
, a
This is the nal PDA.
147
4.4 Equivalence of PDAs and CFGs
The relationship between PDAs and CFGs is similar to that between DFAs
and regular expressions; namely, the languages accepted by PDAs are just
those that can be generated by CFGs.
Theorem 15. Suppose L is a context-free language. Then there is a PDA
M such that L(M) = L.
Theorem 16. Suppose M is a PDA. Then there is a grammar G such that
L(G) = L(M), i.e., L(M) is context-free.
The proofs of these theorems take a familiar approach: given an arbitrary
grammar, we construct the corresponding PDA; and given an arbitrary PDA,
we construct the corresponding grammar.
4.4.1 Converting a CFG to a PDA
The basic idea in the construction is to build M so that it simulates the
leftmost derivation of strings using G. The machine we construct uses the
terminals and non-terminals of the grammar as stack symbols. What we
conceptually want to do is to use the stack to hold the sentential form that
evolves during a derivation. At each step, the topmost variable in the stack
will get replaced by the rhs of some grammar rule. Of course, there are
several problems with implementing this concept. For one, the PDA can
only access the top of its stack: it cant nd a variable below the top. For
another, even if the PDA could nd such a variable, it couldnt t the rhs
into a single stack slot. But these are not insurmountable. We simply have
to arrange things so that the PDA always has the leftmost variable of the
sentential form on top of the stack. If that can be set up, the PDA can use
the technique of using extra states to push multiple symbols all at once.
The other consideration is that we are constructing a PDA after all, so
it needs to consume the input string and give a verdict. This ts in nicely
with our other requirements. In brief, the PDA will use -transitions to push
the rhs of rules into the stack, and will use normal transitions to consume
input. In consuming input, we will be able to remove non-variables from
the top of the stack, always guaranteeing that a variable is at the top of the
stack.
148
Here are the details. Let G = (V, , R, S). We will construct M =
(Q, , V

, , q
0
, {q}) where
Q = {q
0
, q} RuleStates;
q
0
is the start variable;
q is a dispatch state at the center of a loop. It is also the sole accept
state for the PDA
has rules for getting started, for consuming symbols from the input,
and for pushing the rhs of rules onto the stack.
getting started. (q
0
, , ) = {(q, S)}. The start symbol is pushed on
the stack and a transition is made to the loop state.
consuming symbols. (q, a, a) = {(q, )}, for every terminal a .
pushing rhs of rules For each rule R
i
= V w
1
w
2
w
n
in
R, where each w
i
may be a terminal or non-terminal, add n 1
states to RuleStates. Also add the loop (from q to q)
q q
, V w
n
, w
n1
, w
2
, w
1
which pushes the rhs of the rule, using the n 1 states. Note
that the symbols in the rhs of the rule are pushed on the stack in
right-to-left order.
Example 104. Let G be given by the grammar
S aS | aSbS |
The corresponding PDA is
149
A B
C D E
F
, S
, S
a, a
b, b
, S S
, a
, S S
, b
, S
, a
Consider the input aab. A derivation using G is
S aS aaSbS aabS aab = aab
As a sequence of machine congurations, this looks like
(A, aab, ) (B, aab, S)
(C, aab, S)
(B, aab, aS)
(B, ab, S)
(D, ab, S)
(E, ab, bS)
(F, ab, SbS)
(B, ab, aSbS)
(B, b, SbS)
(B, b, bS)
(B, , S)
(B, , )
And so the machine would accept aab.
4.4.2 Converting a PDA to a CFG
The previous construction, spelled out in full would look messy, but is in
fact quite simple. Going in the reverse direction, i.e., converting a PDA to
a CFG, is more dicult. The basic idea is to consider any two states p, q of
PDA M and think about what strings could be consumed in executing M
150
from p to q. Those strings will be represented by a variable V
pq
in G, the
grammar we are building. By design, the strings generated by V
pq
would be
just those substrings consumed by M in going from p to q. Thus S, the start
variable, will stand for all strings consumed in going from q
0
to an accept
state. This is clear enough, but as always for PDAs, we must consider the
stack, hence the story will be more involved; for example, we will use funky
variables of the form V
pAq
, where A represents the top of the stack.
The construction goes as follows: given PDA M = (Q, , , , q
0
, F), we
will construct a grammar G such that L(G) = L(M). Two main steps achieve
this goal:
M will be modied to an equivalent M

, with a more desirable form.


An important aspect of M

is that it always looks at the top symbol


on the stack in each move. Thus, no transitions of the form a,
b (pushing b on the stack), , , or a, (ignoring stack)
are allowed. How can these be eliminated from without changing
the behaviour? First we need to make sure that the stack is never
empty, for if M

is going to look at the top element of the stack, the


stack had better never be empty. This can be ensured by starting the
computation with a special token ($) in the stack and then maintaining
an invariant that the stack never thereafter becomes empty. It will also
be necessary to allow M

to push two stack symbols in one move: since


M

always looks at the top stack symbol, we need to push two symbols
in order to get the eect of a push operation on a stack. This can be
implemented by using extra states, but we will simply assume that M

has this extra convenience.


Furthermore, we are going to add a new start state s and the transition
, $, which pushes $ on the stack when moving from the new start
state s to the original start state q
0
. We also add a new nal state
q
f
, with , $ transitions from all members of F to q
f
. Thus the
machine M

has a single start state and a single end state, always


examines the top of its stack, and behaves the same as the machine M.
Construct G so that it simulates the working of M

. We rst construct
the set of variables of G.
V = {V
pAq
| p, q Q {q
f
} A {$}}
151
Thus we create a lot of new variables: one for each combination of
states and possible stack elements. The intent is that each variable
V
pAq
will generate the following strings:
{x

| (p, a, A)

(q, , )}
Now, because of the way we constructed M

, there are three kinds of


transitions to deal with:
1.
p q
a, A B
Add the rule V
pAr
aV
qBr
for all r Q {q
f
}
2.
p p
a, A BA
Add the rule V
pAr
aV
qBr
V
r

Ar
for all r, r

Q {q
f
}
3.
p p
a, A
Add the rule V
pAq
a.
The above construction works because the theorem
V
pAq

w i (p, w, A)

(q, , )
can be proved (its a little complicated though). From this we can
immediately get
V
q
0
$q
f

w i (q
0
, w, $)

(q
f
, , )
Thus by making V
q
0
$q
f
the start symbol of the grammar, we have
achieved our goal.
152
4.5 Chomsky Normal Form
It is sometimes helpful to eliminate various forms of redundancy in a gram-
mar. For example, a grammar with a rule such as V occurring in it
might be thought to be simpler if all occurrences of V in the right hand side
of a rule were eliminated. Similarly, a rule such as P Q is an indirection
that can seemingly be eliminated. A grammar in Chomsky Normal Form
2
is one in which these redundancies do not occur. However, the simplica-
tion steps are somewhat technical, so we will have to take some care in their
application.
Denition 34 (Chomsky Normal Form). A grammar is in Chomsky Normal
Form if every rule has one of the following forms:
A BC
A a
where A, B, C are variables, and a is a terminal. Furthermore, in all rules
A BC, we require that neither B nor C are the start variable for the
grammar. Notice that the above restrictions do not allow a rule of the form
A ; however, this will disallow some grammars. Therefore, we allow the
rule S , where S is the start variable.
The following algorithm translates grammar G = (V, , R, S) to Chomsky
Normal Form:
1. Create a new start variable S
0
and add the rule S
0
S. Now the
start variable is not on the right hand side of any rule.
2. Eliminate all rules of the form A . For each rule of the form
V uAw, where u, w (V )

, we add the rule V uw. It is


important to notice that we must do this for every occurrence of A in
the right hand side of the rule. Thus the rule
V uAwAv
2
In theoretical computer science, a normal form of an expression x is an equivalent
expression y which is in reduced form, i.e., y cannot be further simplied.
153

You might also like