Anda di halaman 1dari 38

Lecture Pushdown Automata

tape

tape head
stack head

finite
stack
control
a l p h a b e t

The tape is divided into finitely many cells.


Each cell contains a symbol in an alphabet
.
The stack head always scans the top
symbol of the stack. It performs two
basic operations:

Push: add a new symbol at the top.

Pop: read and remove the top symbol.

Alphabet of stack symbols:


a

The head scans at a cell on the tape and


can read a symbol on the cell. In each
move, the head can move to the right cell.
The finite control has finitely many states
which form a set Q. For each move, the
state is changed according to the
evaluation of a transition function
: Q x ( U {}) x ( U {}) 2 .Q x ( U {})
a a

q v p u

(p, u) (q, a, v) means that if the tape head


reads a, the stack head read v, and the finite
control is in the state q, then one of possible
moves is that the next state is p, v is replaced
by u at stack, and the tape head moves one cell
to the right.
a a

q v p u

(p, u) (q, , v) means that this a -move.


a a

q p u

(p, u) (q, a, ) means that a push operation


performs at stack.
a a

q v p

(p, ) (q, a, v) means that a pop operation


performs at stack
s

There are some special states: an initial


state s and a final set F of final states.
Initially, the PDA is in the initial state s and
the head scans the leftmost cell. The tape
holds an input string. The stack is empty.
x

When the head gets off the tape, the PDA


stops. An input string x is accepted by the
PDA if the PDA stops at a final state and
the stack is empty.
Otherwise, the input string is rejected.
The PDA can be represented by
M = (Q, , , , s, F)
where is the alphabet of input symbols
and is the alphabet of stack symbols.
The set of all strings accepted by a PDA M
is denoted by L(M). We also say that the
language L(M) is accepted by M.
The transition diagram of a PDA is an
alternative way to represent the PDA.
For M = (Q, , , , s, F), the transition diagram
of M is an edge-labeled digraph G=(V, E)
satisfying the following:

V = Q (s = ,f= for f F)

E={q a, v/u p | (p,u) (q, a, v) }.


Example 1. Construct PDA to accept
n n
L= {0 1 | n > 0}

Solution 1.

1, 0/

0, /0 1, 0/
Solution 2.

Consider a CFG
G = ({S}, {0,1}, {S | 0S1}, S).

, S/1
, /S
, /S
, /0
, S/

0, 0/
1, 1/
Theorem Every CFL can be accepted by a PDA.

Proof. Consider a CFL L = L(G) for a CFG


G = (V, , R, S).

, A/xn A x1 x2 xn in R
, /S

, /x1
a, a/ for a in

Theorem
A language L is CFL it can be accepted by a PDA.
Sometimes, constructing the PDA is easier than
constructing CFG.
Example 2

Show that {x {a, b} | #b ( x) #a ( x) 2#b ( x)} is a CFL

Construct a PDA or a CFG?

PDA!!!

Sometimes, constructing the PDA is easier than


constructing CFG.
Idea
To check if #b ( x) # a ( x) 2#b ( x), we need to cancel a with b.
For each b, we need to cancel sometimes one a and sometimes
two a. Do we need to make a determinis tic choice at each
cancellati on? No, the concept of nondetermi nistic computatio n
solves this trouble : As long as a correct choice exists, the input
string x would be accepted!
a a

s p s

(p, s) (s, , )

, / s
a a

p p a
s
s
(q, s) (p, a, s)
(p, a) (q, , )
a, s / s

, / a
a a

p p a
a
a
(q1, a) (p, a, a)
(p, a) (q1, , )
a, a / a

, / a
a a

p p
b

(p, ) (p, a, b)

a,b /
a a

p p b
b

(p, b) (p, a, b)

a, b / b'
a a

p p
b

(p, ) (p, a, b)

a,b' /
b b

p p b
s
s
(r1, s) (p, b, s)
(p, b) (r1, , )
b, s / s

, / b
b b

p p b
b
b
(r2, b) (p, b, b)
(p, b) (r2, , )
b, b / b

, / b
b b

p p b
b
b
(r3, b) (p, b, b)
(p, b) (r3, , )
b, b ' / b '

, / b
b b

p p
a

(p, ) (p, b, a)

b, a /
b b

p p
a
a

(t, ) (p, b, a) (p, ) (t, , a)


b, a /

,a /
b b

p p b
a
z
z za

(t, ) (p, b, a) (u, x) (t, , x) (p, b) (u, , )


b, a /

, z / z
,b' / za
p p
s

(p, ) (p, , s)

,s /
a,b /
a, b / b'
a,b' /
, / s a, z / z
z b,b'

, / a
,s /
b, a /
b, a / b, z / z
za
,a /
, z / z , / b
za , / b'
Example 3
Construct PDA accepting {x {a, b} | #b ( x) #a ( x) 2#b ( x)}.

Idea

To have #b ( x) #a ( x), we must have a b which cancel two a' s.


Let the first b do the job.
a,b /
Solution
a, b / b' a, z / z
a,b' / 1
z b,b'
, / s a, b / b' a, z / z
11 z b,b' , / a
b, a / ,a /
, / b'
, / a
, z / z
za ,s /
b, a /
b, a / b, z / z
za
b, z / z
,a /
za
1
, z / z , / b
za , / b'
, / b
Example 4
Construct PDA accepting {x {a, b} | #b ( x) #a ( x) 2#b ( x)}.

Idea

To have #a ( x) 2#b ( x), we must have a b which cancel one a.


Let the first b do the job.
a,b /
Solution
a, b / b' a, z / z
a,b' / 1
z b,b'
, / s a,b / a, z / z
11 z b,b' , / a
b, a /
, / a
,s /
b, a /
b, a / b, z / z
za
b, z / z
,a /
za
1
, z / z , / b
za , / b'
, / b

Anda mungkin juga menyukai