Upload
meljun-cortes
View
75
Download
4
Tags:
Embed Size (px)
DESCRIPTION
MELJUN CORTES Automata Theory (Automata10)
Citation preview
CSC 3130: Automata theory and formal languages
Pushdown automata
Fall 2008MELJUN P. CORTES, MBA,MPA,BSCS,ACSMELJUN P. CORTES, MBA,MPA,BSCS,ACS
MELJUN CORTESMELJUN CORTES
Motivation
• We had two ways to describe regular languages
• How about context-free languages?
regularexpression
DFANFA
syntactic computational
computational
CFG pushdown automaton
syntactic computational
computational
Pushdown automata versus NFA
• Since context-free is more powerful than regular, pushdown automata must generalize NFAs
state control
0 1 0 0
input
NFA
Pushdown automata
• A pushdown automaton has access to a stack, which is a potentially infinite supply of memory
state control
0 1 0 0
input
pushdown automaton (PDA)
…
stack
Pushdown automata
• As the PDA is reading the input, it can push / pop symbols in / out of the stack
state control
0 1 0 0
input
pushdown automaton (PDA)
Z0 0 1stack
…1
Rules for pushdown automata
• The transitions are nondeterministic
• Stack is always accessed from the top
• Each transition can pop a symbol from the stack and / or push another symbol onto the stack
• Transitions depend on input symbol and on last symbol popped from stack
• Automaton accepts if after reading whole input, it can reach an accepting state
Example
L = {0n#1n: n ≥ 0}state control
0 0 0 #
input
Z0 a astack
a …
1 1 1
read 0push a
read #
read 1pop a
pop Z0
Shorthand notation
read 0push a
read #
read 1pop a
pop Z0
0, / a
#, /
, Z0 /
1, a /
read, pop / push
Formal definition
A pushdown automaton is (Q, , , , q0, Z0, F):– Q is a finite set of states; is the input alphabet; is the stack alphabet, including a special symbol Z0;
– q0 in Q is the initial state;
– Z0 in is the start symbol;
– F Q is a set of final states; is the transition function
: Q ( {}) ( {}) → subsets of Q ( {})stateinput symbol pop symbol statepush symbol
Notes on definition
• We use slightly different definition than textbook
• Example
0, / a
#, / , Z0 /
1, a /
: Q ( {}) ( {}) → subsets of Q ( {})
q0 q1 q2
(q0, 0, ) = {(q0, a)} (q0, 1, ) = ∅
(q0, #, ) = {(q1, )} (q0, 0, ) = ∅
...
A convention
• Sometimes we denote “transitions” by:
• This will mean:
– Intuitively, pop b, then push c1, c2, and c3
a, b/ c1c2c3
q0 q1
, / c2
q0 q1a, b/ c1 , / c3
intermediatestates
Examples
• Describe PDAs for the following languages:– L = {w#wR: w∈*}, = {0, 1, #}– L = {wwR: w∈*}, = {0, 1}– L = {w: w has same number of 0s and 1s}, =
{0, 1}– L = {0i1j: i ≤ j ≤ 2i}, = {0, 1}
Main theorem
A language L is context-free if and only if it is accepted by some pushdown automaton.
context-free grammar pushdown automaton
From CFGs to PDAs
• Idea: Use PDA to simulate (rightmost) derivations
A → 0A1A → BB → #
A 0A1 00A11 00B11 00#11
PDA control:
CFG:
write start variable
stack:
Z0A
replace production in reverse Z01A0
pop terminals and match Z01A
e, e / A
0, 0 / e
e, A / 1A0
input:
00#11
00#11
0#11
replace production in reverse Z011A0e, A / 1A0 0#11
pop terminals and match Z011A0, 0 / e #11
replace production in reverse Z011Be, A / B #11
From CFGs to PDAs
• If, after reading whole input, PDA ends up with an empty stack, derivation must be valid
• Conversely, if there is no valid derivation, PDA will get stuck somewhere– Either unable to match next input symbol,– Or match whole input but stack non empty
Description of PDA for CFGs
• Repeat the following steps:– If the top of the stack is a variable A:
Choose a rule A → and substitute A with – If the top of the stack is a terminal a:
Read next input symbol and compare to aIf they don’t match, reject (die)
– If top of stack is Z0, go to accept state
Description of PDA for CFGs
q0 q1 q2, / S
a, a / for every terminal a
, A / k...1for every production A → 1...k
, Z0 /
From PDAs to CFGs
• First, we simplify the PDA:– It has a single accept state qf
– Z0 is always popped exactly before accepting
– Each transition is either a push, or a pop, but not both
context-free grammar pushdown automaton
✓
From PDAs to CFGs
• We look at the stack in an accepting computation:
a
Z0 Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0 Z0
b a c c c
a
portions that preserve the stack
q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7
A03 = {x: x leads from q0 to q3 and preserves stack}
1 1 0 1 0 00input
state
stack
qf
From PDAs to CFGs
a
Z0 Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0 Z0
b a c c c
a
q0 q1 q3 q1 q7 q1 q2 q1 q7
1 1 0 1 0 00input
state
stack
A11
A03
0 A11 → 0A03
q0 q3 qf
From PDAs to CFGs
a
Z0 Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0
a
Z0 Z0
b a c c c
a
q0 q1 q3 q1 q7 q1 q2 q1 q7
1 1 0 1 0 00input
state
stack
A03
A13
A13 → A10A03
q0 q3
A10
qf
From PDAs to CFGs
qi
qj
a, / t
b, t /
qi’
qj’ Aij → aAi’j’b
qi qj qk Aik → AijAjk
qi Aii →
variables:Aij
qfa, Z0 /qi A0f → A0ia
start variable:A0f
Example
0, / a
#, / , Z0 /
1, a /
q0 q1 q2
start variable: A02
productions:
A00 → A00A00
A00 → A01A10A00 → A03A30A01 → A01A11
A01 → A02A21
A00 →
...
A11 → A22 →
A01 → 0A011A01 → #A33
A33 →
0, / a
#, / $ , Z0 /
1, a /
q0 q1 q2q3, $ /
A02 → A01