Download - Pushdown Automata - by Emmanuel Afriyie
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
1/30
CMSC 214 Principles of Programming Language
Pushdown automata
2nd Semester 2013-2014
Presented by EOA
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
2/30
Motivation
regularexpression DFANFA
syntactic computational
CFG pushdown automaton
syntactic computational
is more powerful than
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
3/30
Pushdown automata versus NFA
state control0 1 0 0
input
NFA
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
4/30
Pushdown automata
state control0 1 0 0
input
pushdown automaton (PDA)
stack
A PDA is like an NFA with but with an infinite stack
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
5/30
Pushdown automata
state control0 1 0 0
input
pushdown automaton (PDA)
$ 0 1
stack
1
As the PDA is reading the input, it canpush / pop symbols from the top of the stack
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
6/30
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
7/30
Building a PDA
L = { 0 n 1 n : n 1} state control
We remember each 0
by pushing x onto the stack
read 1pop x
read 0
push x
pop $
push $
read 1pop x When we see a 1 , we
pop an x from the stack
We want to accept whenwe hit the stack bottom
Well use t$ mark bottom
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
8/30
A PDA in action
L = { 0 n 1 n : n 1} state control
0 0 0 1
input
$ x x
stackx
1 1
read 0
push xread 1
read 1pop x
pop $
push $
pop x
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
9/30
Notation for PDAs
read, pop / push
read 1pop x
read 0push x
pop $
push $
read 1pop x
1 , x /
0 , / x
, $ /
, / $
1 , x /
q 0
q 1
q 2
q 3
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
10/30
Definition of a PDA
A pushdown automaton is (Q, , , , q 0, F) where: Q is a finite set of states ;
is the input alphabet ; is the stack alphabet
q 0 in Q is the initial state ; F Q is a set of final states;
is the transition function
: Q ( { }) ( { }) subsets of Q ( { })state input symbol pop symbol state push symbol
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
11/30
Example
: Q ( { }) ( { }) subsets of Q ( { })
(q 0, , ) = {(q 1, $ )}(q 0, , $ ) = (q 0, , x ) = (q 0, 0 , ) =
...
= { 0 , 1 } = { $ , x }
1 , x /
0 , / x
, $ /
, / $
1 , x /
q 0
q 1
q 2
q 3
state input symbol pop symbol state push symbol
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
12/30
The language of a PDA
A PDA is nondeterministicMultiple transitions on same pop/input allowed
Transitions may but do not have to push or pop
The language of a PDA is the set of all
strings in * that can lead the PDA to anaccepting state
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
13/30
Example 1
L = { w # w R : w {0, 1}*} = {0, 1, #}
#, 0#0, 01#10 L, 01#1, 0##0 L
0, / 0
q 1
1, / 10, 0 /1, 1 /
, / $q 0 q 3, $ /q 2
#, /
write w on stack read w R from stack
= {0, 1}
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
14/30
Example 2
0, / 0
q 1
1, / 1
, / $q 0 q 3, $ /
0, 0 /1, 1 /
q 2, /
L = { ww R : w *} = {0, 1}
, 00, 0110 L1, 011, 010 L
guess middle of string
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
15/30
Example 3
L = { w : w = w R , w *} = {0, 1}
, 1, 00, 010, 0110 L
011 L0110110110 011010110or
x x x Rx R
0, / 0
q 1
1, / 1
, / $q 0 q 3, $ /
, /0, /1, /
0, 0 /1, 1 /
q 2
middle symbol can be , 0, or 1
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
16/30
Example 4
L = {0n
1m
0m
1n
| n 0, m 0} = {0, 1}
0, / 0
q 1, /
1, / 1
q 2, / $q 0
, $ /q 5
, /
q 3
0, 1 /
, /q 4
1, 0/
0n 1m 0m 1n
0n 1m
input:
stack:
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
17/30
Example 5
L = { w : w has same number 0s and 1s} = {0, 1}
Strategy:Stack keeps track of excess of 0s or 1s
If at the end, stack is empty, number is equal
0, / 0
q 1
1, / 1
, / $q 0 q 3, $ /
0, 1 / 1, 0 /
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
18/30
Example 5
L = { w : w has same number 0s and 1s} = {0, 1}
Invariant: In every execution of the PDA:
0, / 0
, / $q 0 q 1
1, / 1
q 3, $ /
0, 1 / 1, 0 /
#1 #0 on stack = #1 #0 in input so far
If w is not in L , it must be rejected
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
19/30
Example 5
L = { w : w has same number 0s and 1s} = {0, 1}
Property: In some execution of the PDA:
0, / 0
, / $q 0 q 1
1, / 1
q 3, $ /
0, 1 / 1, 0 /
stack consists only of 0s or only of 1s (or )
If w is in L , some execution will accept
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
20/30
Example 5
L = { w : w has same number 0s and 1s} = {0, 1}
0, / 0
, / $q 0 q 1
1, / 1
q 3, $ /
0, 1 / 1, 0 /
w = 001110 read stack0 $00 $001 $01 $1 $10 $
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
21/30
Example 6
= { w : w has two 0-blocks with same number of 0s
Strategy: Detect start of first 0-blockPush 0s on stack
01011, 001011001, 10010101001 01001000, 01111allowed not allowed
Detect start of second 0-blockPop 0s from stack
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
22/30
Example 6
1 Detect start of first 0-block 2 Push 0s on stack
3 Detect start of second 0-block 4 Pop 0s from stack
11
1
0, 1
0, 0 /
0, / 0
0, / 0q 0 q 1
0, 1
1
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
23/30
1 2
3
4
Example 6
= { w : w has two 0-blocks with same number of 0s
q 2
0, / 0
q 30, / 0
1, / q 4
1, /
1, /
0, /1, /
q 5
0, 0 /
0, /
, / $q 0 q 1
1, /
1, /
, / $
1, /q 6
0, /1, /
q 7
, $ /
, $ /
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
24/30
CFG PDA conversions
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
25/30
CFGs and PDAs
L has a context-free grammar if and only if itis accepted by some pushdown automaton.
context-free grammar pushdown automaton
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
26/30
A convention
When we have a sequence of transitions like:
We will abbreviate it like this:
x , a / bcdq 0 q 1
, / cq 0 q 1
x , a / b , / d
popa
, then pushb
,c
, andd
replace a by bcd on stack
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
27/30
Converting a CFG to a PDA
Idea: Use PDA to simulate derivations A 0 A1 A BB # A 0 A1 00 A11 00 B11 00#11
write start variable $Areplace production in reverse $1A0pop terminal and match $1A
, / A
0, 0 /
, A / 1A0
00#11
00#11
0#11
replace production in reverse $11A0, A / 1A0 0#11pop terminal and match $11A0, 0 / #11
replace production in reverse $11B, A / B #11
PDA control: stack: input:
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
28/30
Converting a CFG to a PDA
A 0 A1 A BB #
CFG
0 , 0 /1 , 1 /# , # /
, A / 1A0, A / B, B / #
q 0 , /$A , $ /
q 1 q 2
$A
00#11
$1A0
00#11
$1A
0 0#11
$11A0
0 0#11
$11A
00 #11
$11B
00 #11
$11#
00 #11
$11
00# 11
$1
00#1 1
$
00#11
inputstack
A 0 A1 00 A11 00 B11 00#11
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
29/30
General PDA to CFG conversion
q 0 q 1 q 2, / $S
a , a /for every terminal a
, A / k ... 1for every production A 1... k
, $ /
-
8/13/2019 Pushdown Automata - by Emmanuel Afriyie
30/30
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130 The Chinese University of Hong Kong
REFERENCES
F. D. Lewishttp://www.cs.uky.edu/~lewis/texts/theory/automata/pd-auto.pdf Departmentof Computer Science -University of Kentucky
http://www.cs.uky.edu/~lewis/texts/theory/automata/pd-auto.pdfhttp://www.cs.uky.edu/~lewis/texts/theory/automata/pd-auto.pdfhttp://www.cs.uky.edu/~lewis/texts/theory/automata/pd-auto.pdfhttp://www.cs.uky.edu/~lewis/texts/theory/automata/pd-auto.pdf