Professional Documents
Culture Documents
a, b → ε b, a → ε: count count
a, b → ε b, a → ε: count count
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}
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
, , 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
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
. 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
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 )