CS5371 Theory of Computation: Lecture 8: Automata Theory VI (Pda, Pda CFG)
CS5371 Theory of Computation: Lecture 8: Automata Theory VI (Pda, Pda CFG)
Theory of Computation
Lecture 8: Automata Theory VI
(PDA, PDA = CFG)
Objectives
•Introduce Pushdown Automaton (PDA)
•Show that PDA = CFG
–In terms of descriptive power
Pushdown Automaton (PDA)
•Roughly speaking, PDA = NFA + stack with
unlimited size
•How does a PDA operate?
•In each step, it can read a character from
the input string, can pop (remove) a
symbol from the stack
•Then, depending on the character and the
symbol, the PDA enters another state and
can push (place) a symbol to the stack
A stack is a “
last in, first out”storage device
Stack is powerful
•Recall that an NFA cannot recognize the
language {0n1n | n 0}
•However, a PDA can do so (informally):
–Read the characters from input
–For any 0 it reads, push it onto the stack
–As soon as 1s are seen**, pop a 0 off the stack for
each 1 read
** after this point, if we read a 0 again, we
reject the string immediately
- Accept the string if the stack is just empty after
the last 1 is read; Reject the string otherwise
PDA (Example)
•The following state diagram gives the
PDA that recognizes {0n1n | n 0}.
, $
0, 0
1, 0
1, 0
,$
•Condition 3: rm 2 F
This ensures PDA accepts only when the PDA is in an
accept state after processing the whole input string
PDA (example 1)
•Recall that the following PDA
recognizes {0n1n | n 0}
, $
0, 0
1, 0
1, 0
,$
PDA (example 1)
Some points to notice:
–The formal definition of PDA does not allow us to
test if the stack is empty
–The previous PDA tries to get the same effect by
first placing $ to the stack, so that if it ever sees
$ again, it knows the stack is empty
–Similarly, PDA cannot test if the input has all
been processed
–The previous PDA can have the same effect
because it can stay at the accept states only at
the end of the input
PDA (example 1)
•We can also write the formal definition of
the previous PDA, call it M, as follows:
M = ({q1,q2,q3,q4}, {0,1}, {0,$},
, q1, {q1,q4}),
where is given by
(q1,
, ) = { (q2, $) },
(q2, 0, ) = { (q2, 0) }
(q2, 1, 0) = { (q3,
) }, (q3, 1, 0) = { (q3, )}
(q3,
, $) = { (q4, ) },
and for the remaining combinations of (q, x, y),
(q, x, y) = { }
PDA (example 2)
Give a PDA that recognizing the language
{ aibjck | i,j,k 0 and i=j or i=k }
How to construct it?
•First, for each ‘ a’read, we should push it
onto the stack (for later matching)
•Then, we guess whether ‘ a’should be
matched with ‘ b’or matched with ‘ c’
,$
,
, $
,
, ,$
a, a b, c, a
PDA (example 3)
Give a PDA recognizing { wwR | w in {0,1}* }
How to construct it?
•First, the PDA should match the first
character with the last character, the second
character with the second last character, and
so on…So, we push each character that is read
to the stack in case it will be matched later
•At each point, we guess the middle of the
string has been reached. We match the
remaining characters with those stored in the
stack (how?)
CFG = PDA
Theorem: (1) If a language is generated by a
CFG, it can be recognized by some PDA. (2)
If a language is recognized by a PDA, it can
be generated by some CFG.
100 1 00 0
0 0
Next char 0 Next char 0 Next char 0
$ $ $
PDA uses the rule Input char is Input char is
S 10 matched matched
An Example Run (cont.)
Input: 1100
CFG: S SS | 1S0 | 10
,Az
, y
, A xyz
, x
, S$
, $
Example of Conversion
•Convert the following CFG into an
equivalent PDA
S aTb | b
T Ta |
Proof of (2)
(2) If a language is recognized by a PDA,
it can be generated by some CFG.
•How to do so?
Creating Apq
•Note that PDA can get from p (with an
empty stack) to q (with an empty stack) in
two ways:
(1) The stack gets empty before reaching q
–This implies we get from p to some r (with
empty stack) and then to q
(2) The stack never gets empty before
reaching q
–This implies at p, we push some char t in stack,
and then at q, we pop the same char t
Creating Apq (cont.)
•For each p, q, r, add the rule
Apq AprArq
That is, if we can get from p to r, and also from r to q, then we can
get from p to q (Here, all starts and ends are with empty stack)
•For each p, q, r, s with (r, t) 2
(p, a,
)
and (q,
)2
(s, b, t), add the rule
Apq aArsb
That is, if we can get from p to r by reading a and pushing t, and
we can get from s to q by reading b and popping t, then, if we
start with p with an empty stack, we can reach q with an empty
stack by reading a, going from r to s, then reading b.
Creating Apq (cont.)
•For each p, we also add the rule
App
That is, if we can get from p to p by reading nothing