Lecture 14 Nondeterministic Push Down Automata and Context-free Languages

advertisement
Lecture 14
Nondeterministic Push Down
Automata and Context-free
Languages
COT 4420
Theory of Computation
Section 7.2
Theorem
Context-free languages
=
Languages
accepted by
NPDAs
Part 1) Convert any context-free grammar G to
an NPDA M with: L(G) = L(M)
Part2) Convert any NPDA M to a context-free
grammar G with L(G) = L(M)
Part 1
Theorem: for any Context-free language L, there
exists an NPDA M such that L = L(M)
Let G = (V, T, S, P) be a context-free grammar
such that L(G) = L. We construct an NPDA M
such that L(M) = L(G).
NPDA M simulates leftmost derivations of G.
Part 1
CFG grammar to NPDA (Example)
Example in Greibach Normal Form:
S  aSA | a
A  bB
Bb
What’s the leftmost derivation for aaabbbb?
S => aSA => aaSAA => aaaAA => aaabBA =>
aaabbA => aaabbbB => aaabbbb
Part 1
CFG grammar to NPDA (Example)
Push start
symbol S on the
stack
λ, λ  S
Put npda into
its final state
λ, z  λ
q2
Part 1
CFG grammar to NPDA (Example)
S  aSA
Sa
A  bB
Bb
λ, λ  S
λ, z  λ
q2
Part 1
CFG grammar to NPDA (Example)
S  aSA
Sa
A  bB
Bb
a, S  SA
λ, λ  S
λ, z  λ
q2
Part 1
CFG grammar to NPDA (Example)
S  aSA
Sa
A  bB
Bb
a, S  SA
a, S  λ
λ, λ  S
λ, z  λ
q2
Part 1
CFG grammar to NPDA (Example)
S  aSA
Sa
A  bB
Bb
a, S  SA
a, S  λ
λ, λ  S
b, A  B
λ, z  λ
q2
Part 1
CFG grammar to NPDA (Example)
S  aSA
Sa
A  bB
Bb
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
S  aSA
Sa
A  bB
Bb
Input:
aabb
z
Stack
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
Derivation: S
Input:
aabb
S
z
Stack
S  aSA
Sa
A  bB
Bb
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
Derivation: S => aSA
Input:
aabb
S
A
z
Stack
S  aSA
Sa
A  bB
Bb
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
Derivation: S => aSA => aaA
Input:
aabb
A
z
Stack
S  aSA
Sa
A  bB
Bb
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
Derivation: S => aSA => aaA => aabB
Input:
aabb
B
z
Stack
S  aSA
Sa
A  bB
Bb
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
Derivation: S => aSA => aaA => aabB => aabb
S  aSA
Sa
A  bB
Bb
Input:
aabb
z
Stack
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  λ
q2
Derivation: S => aSA => aaA => aabB => aabb
S  aSA
Sa
A  bB
Bb
Input:
aabb
z
Stack
a, S  SA b, A  B
a, S  λ
b, B  λ
λ, λ  S
λ, z  z
q2
Part 1
CFG grammar to NPDA
In general for an arbitrary Context-free grammar G:
For each production
For each terminal
Av
a
Add transition
λ, A v
λ, λ  S
Add transition
a, a λ
λ, z  z
q2
Grammar:
S  aSTb
S b
T  Ta
Tλ
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
z
Stack
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
S
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
a
S
T
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
S
T
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
b
T
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
T
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
T
a
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
a
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
b
z
Stack
Grammar:
S  aSTb
S b
T  Ta
Tλ
a, a  λ
b, b  λ
λ, z  z
q2
Grammar:
S  aSTb
S b
T  Ta
Tλ
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
z
Stack
a, a  λ
b, b  λ
λ, z  z
q2
Grammar:
S  aSTb
S b
T  Ta
Tλ
Input:
abab
λ, S aSTb
λ, S b
λ, T Ta
λ, T λ
λ, λ  S
z
Stack
a, a  λ
b, b  λ
λ, z  z
q2
Grammar
Leftmost Derivation
S
⇒ aSTb
⇒ abTb
⇒ abTab
⇒ abab
PDA Computation
(q0, abab, z)
⊢ (q1, abab, Sz)
⊢ (q1, abab, aSTbz)
⊢ (q1, bab, STbz)
⊢ (q1, bab, bTbz)
⊢ (q1, ab, Tbz)
⊢ (q1, ab, Tabz)
⊢ (q1, ab, abz)
⊢ (q1, b, bz)
⊢ (q1, λ, z)
⊢ (q2, λ, z)
Part 2
Theorem: for any NPDA M, there exists a
Context-free grammar G such that L(G) = L(M)
Let M be an npda, We construct a grammar G
such that L(M) = L(G).
Language of NPDA
If M is a PDA, then L(M) is the set of strings w such
that (q0, w, z) ⊦* (qf , λ, u) for final state qf and any
u.
If M is a PDA, then N(M) is the set of strings w such
that (q0, w, z) ⊦* (q, λ, λ) for any state q.
Equivalence of Language Definitions
1. If L = L(M), then there is another PDA M1
such that L = N(M1).
2. If L = N(M), then there is another PDA M2
such that L = L(M2).
Proof: L(M) -> N(M1) Intuition
• M1 simulates M.
• If M accepts, M1 will empty its stack.
• M1 has to avoid accidentally emptying its
stack. It uses a special bottom marker to catch
the case where M empties its stack without
accepting.
Proof: L(M) -> N(M1)
•
M1 has all the states, symbols, and moves of
M, plus:
1. Stack symbol X0 (the start symbol of M1), used to
guard the stack bottom.
2. New start state qs and “erase” state qe.
3. δ(qs, λ, X0) = {(q0, Z0X0)}. Get M started.
4. Add {(qe, λ)} to δ(qf, λ, X) for any final state qf of
M and any stack symbol X, including X0.
5. δ(qe, λ, X) = {(qe, λ)} for any X.
Proof: N(M) -> L(M2) Intuition
•
•
•
M2 simulates M.
M2 has a special bottom-marker to catch
the situation where M empties its stack.
If so, M2 accepts.
36
Proof: N(M) -> L(M2)
•
M2 has all the states, symbols, and moves
of M, plus:
1. Stack symbol X0 (the start symbol), used to
guard the stack bottom.
2. New start state qs and final state qf.
3. δ(qs, λ, X0) = {(q0, Z0X0)}. Get M started.
4. δ(q, λ, X0) = {(qf, λ)} for any state q of M.
37
Part 2
NPDA to CFG grammar
First modify NPDA so that:
• It has a single final state qf that is entered if
and only if the stack is empty
• Each transition either pushes a symbol or pops
a symbol (either increases or decreases the
stack content by a single symbol).
Modify the NPDA so that it empties
the stack and has a unique final state
NPDA
Old
final
states
λ, x  λ
∀x ∈ Γ – {z}
λ, z  λ
qf
Each transition either increases or
decreases the stack content by a single
symbol
Convert:
qi
qi
a, A  B
a, A  XB
qj
a, X  λ
qj
NPDA to CFG grammar
Grammar construction
Stack symbol
Variables:
(qiAqj)
States
Terminals: Input Symbols of NPDA
NPDA to CFG grammar
Grammar construction
For each transition:
qi
Add production:
a, A  λ
(qiAqj)  a
qj
NPDA to CFG grammar
Grammar construction
• For each transition:
qi
a, A  BC
qj
• Add productions:
(qiAqk)  a(qjBql)(qlCqk)
for all possible states qk and ql in the
automaton
NPDA to CFG grammar
Grammar construction
Stack bottom
symbol
• Start Variable: (q0Zqf)
Start state
Final state
Example of an NPDA in correct form:
L(M) = { w: na = nb }
a, Z 0Z
a, 0 00
a, 1 λ
b, Z 1Z
b, 1 11
b, 0  λ
λ, Z  λ
qf
a, Z 0Z
a, 0 00
a, 1 λ
b, Z 1Z
b, 1 11
b, 0  λ
λ, Z  λ
(q01q0)  a
qf
a, Z 0Z
a, 0 00
a, 1 λ
b, Z 1Z
b, 1 11
b, 0  λ
λ, Z  λ
(q0Zq0)  b (q01q0)(q0Zq0)
(q0Zq0)  b (q01qf)(qfZq0)
(q0Zqf)  b (q01q0)(q0Zqf)
(q0Zqf)  b (q01qf)(qfZqf)
qf
a, Z 0Z
a, 0 00
a, 1 λ
b, Z 1Z
b, 1 11
b, 0  λ
λ, Z  λ
(q0Zqf)  λ
qf
Resulting Grammar
Start symbol: (q0Zqf)
(q0Zq0)  a (q00q0)(q0Zq0) | a (q00qf)(qfZq0)
(q0Zqf)  a (q00q0)(q0Zqf) | a (q00qf)(qfZqf)
(q00q0)  a (q00q0)(q00q0) | a (q00qf)(qf0q0)
(q00qf)  a (q00q0)(q00qf) | a (q00qf)(qf0qf)
(q0Zq0)  b (q01q0)(q0Zq0) | b (q01qf)(qfZq0)
(q0Zqf)  b (q01q0)(q0Zqf) | b (q01qf)(qfZqf)
Resulting Grammar
(q01q0)  b (q01q0)(q01q0) | b (q01qf)(qf1q0)
(q01qf)  b (q01q0)(q01qf) | b (q01qf)(qf1qf)
(q01q0)  a
(q00q0)  b
(q0Zqf)  λ
NPDA to CFG grammar
In general:
(qiAqj) =>* w
if and only if the NPAD goes from qi to qj by
reading string w and the stack does not
change below A and then A is removed from
stack.
NPDA to CFG grammar
Therefore:
(q0Zqf) =>* w
if and only if
w is accepted by the NPDA
Summary
• For any context-free language L, there exists
an npda M such that L = L(M).
• If L = L(M) for some npda M, then L is a
context-free language.
Download