Upload
alexis-walton
View
215
Download
0
Embed Size (px)
Citation preview
CS 3240: Languages and Computation
Pushdown Automata & CF GrammarsNOTE: THESE ARE ONLY PARTIAL SLIDES RELATED TO WEEKS 9 AND 10 . PLEASE REFER TO THE TEXTBOOK FOR COMPLETE COVERAGE
Project 1
Pushdown Automata
Similar to finite automata, but for CFG’s Finite automata are not adequate for CFG’s
because we cannot keep track of what we’ve doneAt any point, we only know the current state, not
previous states Need memory
PDA’s are finite automata with a stack
FA and PDA Schematics
State control
a a b b
State control
a a b b
xyz
FA
PDA
Stack: Infinite LIFO (last in first out) device
Formal Definition of PDA
A pushdown automaton is a 6-tuple (Q, Σ, , , q0, F), where Q, Σ, , and F are all finite sets, and Q is the set of states Σ is the set of input alphabet is the stack alphabet :Q×Σ× ,P(Q×) is the transition function q0 is the start state and FQ is the set of accept states
How does PDA work?
Start in start state Read input character, perform state transition
and update stack Clone a machine if multiple transitions are
possible At end of input if any machine accepts and the
stack is empty then accept
Example
read 0 & push 0 on stack
read ε & push ε on stack
read ε & push $ on stack
read 1 & pop 0 off stack
read ε & pop $ off stack
Language accepted: {0n1n | n 0}
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()S$
qloop
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
(S)$
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
S)$
(
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
SS)$
(
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
()S)$
(
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
)S)$
((
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
S)$
(()
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
())$
(()
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
))$
(()(
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
)$
(()()
Example
Read (()())
qstart qacceptε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
$
(()())
Example
Read (()())
qstartε, ε S$ ε, $ ε
(,(ε
),)ε
ε,SSS
ε,S(S)
ε,S()
qloop
(()())
qaccept
More Examples
L= {aibjck, i=j or j=k>, and i,j,k>=0}.
L= {wwR | w in {0,1}* }
Differences between PDA’s and NFA’s Transitions read one symbol of the string
and push a string onto or pop a symbol off of the stack
Stack alphabet is not necessarily the same as the alphabet for the languagee.g., $ marks bottom of stack in previous
(0n1n) example
Instantaneous descriptions
Equivalence of pda and cfg
Equivalence of PDA’s and CFG’s
Theorem: A language is context free if and only if some pushdown automaton accepts it by e.s.
Proved in two lemmas – one for the “if” direction and one for the “only if” direction
We will only do the “only if” step – i.e., show that every context-free language has an associated PDA
CFG’s are recognized by PDA’s
Lemma: If a language is context free, then some pushdown automaton accepts it by emp.st.
Proof idea: Construct a PDA following CFG rules
Constructing the PDA
You can read any symbol in when that symbol is at the top of the stack Transitions of the form a,aε
The rules will be pushed onto the stack – when a variable A is on top of the stack and there is a rule Aw, you pop A and push w
You accept only if the input is exhausted and the stack is empty
Idea of PDA construction for AxBz
State control
a b
At
State control
a b
xBzt
Given G build PDA
Deterministic PDA