Grammars and Regular Grammars

Download Report

Transcript Grammars and Regular Grammars

Chapter 5 Pushdown Automata
5.1 Pushdown Automata
In this section, we shall introduce pushdown automata as
machines to accept context-free languages.
For a context-free grammar G, there is an equivalent
pushdown automaton M to recognize the language generated
by the grammar G.
Here the pushdown automaton is nondeterministic, the
deterministic version accepts only a subset of all CFL's.
Description of pushdown automaton (PDA)
A PDA has
an input tape,
a finite control, and
a stack.
0
0
0
1
0
0
0
(input tape)
step1: read 0, push 0, and move to right
q1 q1 q1
(finite control)
0
0
(stack)
step2: read 0, push 0, and move to right
There are 2 types of moves:
1. According to an input symbol, current state, and top
symbol of the stack, a decision is made, then the input
head is moved ahead one symbol.
2. Next move is decided by the current state and the top
symbol of the stack only. The input symbol is not used.
Hence the input head is not moved at all after the
decision is made. This type of move is called ε-move
that allows the PDA to manipulate the stack without
reading input symbol.
Languages accepted by PDA's
There are two ways to accept inputs:
1. The PDA accepts an input if after reading the input
and the machine empty its stack.
The set of inputs accepted by the PDA is the language
accepted by empty stack.
2. Some states of the PDA are final states. The PDA
accepts an input if the machine enters a final state.
The set of inputs accepted by the PDA is the language
accepted by final state.
Definition of PDA's
A pushdown automaton M is a system (Q, Σ, Γ, δ, q0, Z0 , F),
where
1. Q is a finite set of states;
2. Σ is an alphabet called the input alphabet;
3. Γ is an alphabet, called the stack alphabet;
4. q0 in Q is the initial state;
5. Z0 in Γ is a particular stack symbol called the start symbol;
6. F, a subset of Q, is the set of final states;
7. mapping δ: Q×(Σ∪{ε}) ×Γ → finite subset of Q×Γ*.
Moves:
δ(q, a, Z) = {(p1, r1), (p2, r2), ..., (pm, rm)}, where
q and pi, 1≦i ≦ m, are states, a is in , Z is a stack
symbol, and ri is in Γ*, 1≦i ≦ m,
means
PDA in state q, reading input symbol a with top symbol Z
on the stack, can enter any of the state pi and replaces top
symbol Z by ri, and advances reading head one symbol.
Example: δ(q1, 0, G) = {(q1, BG)}
0
0
q1
1
0
δ
G
R
0
0
1
q1
0
B
G
R
-Moves:
δ(q, , Z) = {(p1, r1), (p2, r2), ..., (pm, rm)}, where
q and pi, 1≦i ≦ m, are states, Z is a stack symbol,
and ri is in Γ*, 1≦i ≦ m,
means
PDA in state q, without reading any input symbol with top
symbol Z on the stack, can enter any of the state pi and
replaces top symbol Z by ri, and the reading head remains
at the same place.
Example: δ(q1, , G) = {(q1, BG)}
0
0
q1
1
0
δ
G
R
0
0
q1
1
0
B
G
R
Instantaneous descriptions:
If δ(q, a, Z) = contains (p, ), we say
(q, a, Z)  M(p, ,  ).
Example: δ(q1, 0, G) = {(q1, BG)}
0
0
q1
1
0
δ
0
G
R
0
1
0
G
q1
R
For the above case, we have that
(q1, 010, GR)  M(q1, 01, BGR).
Let  * be the reflexive and transitive closure of
M
B
 M.
Languages accepted by final states:
L(M) = {| (q0, , Z0)
 * (p, , ) for some p in F and  in Γ*}
M
Languages accepted by empty stack:
N(M) = {| (q0, , Z0)
 * (p, , ) for some p in Q}
M
Example: N(M) = {wwR| w in (0+1)*}, where
M = ({q1, q2}, {0, 1}, {Z0, 0, 1}, δ, q1, Z0, {}), and δ is as
follows:
δ(q1, 0, Z0) ={(q1, 0Z0)},
δ(q2, 0, 0) ={(q2, )},
δ(q1, 1, Z0) ={(q1, 1Z0)},
δ(q2, 1, 1) ={(q2, )},
δ(q1, 0, 0) ={(q1, 00), (q2, )},
δ(q2, , Z0) ={(q2, )}.
δ(q1, 0, 1) ={(q1, 01)},
δ(q1, 1, 0) ={(q1, 10)},
δ(q1, 1, 1) ={(q1, 11), (q2, )},
δ(q1, , Z0) = {(q2, )},
Example: L(M) = {wwR| w in (0+1)*}, where
M = ({q1, q2, q3}, {0, 1}, {Z0, 0, 1}, δ, q1, Z0, {q3}), and δ is
as follows:
δ(q1, 0, Z0) ={(q1, 0Z0)},
δ(q2, 0, 0) ={(q2, )},
δ(q1, 1, Z0) ={(q1, 1Z0)},
δ(q2, 1, 1) ={(q2, )},
δ(q1, 0, 0) ={(q1, 00), (q2, )},
δ(q2, , Z0) ={(q3, Z0)}.
δ(q1, 0, 1) ={(q1, 01)},
δ(q1, 1, 0) ={(q1, 10)},
δ(q1, 1, 1) ={(q1, 11), (q2, )},
δ(q1, , Z0) = {(q3, Z0)},
Equivalence of acceptance by final states and empty stack
Theorem 1 L =L(M1) for some PDA M1  L = N(M2) for
some PDA M2.
Proof
Let M1 = (Q, Σ, Γ, δ, q0, Z0 , F), and L(M1) = L.
Construct M2= (Q{qe, q0'}, Σ, Γ{Z'}, δ', q0, Z' , {}), where
δ'(q0', , Z') ={(q0, Z0Z')},
δ'(q, a, X) = δ(q, a, X), for all q in Q, a in Σ{}, X in Γ.
For all q in F, and X in Γ {Z'}, δ'(q, , X) contains (qe, ).
For all X in Γ {Z'}, δ'(qe, , X) contains (qe, ).
Theorem 2 L = N(M2) for some PDA M2  L = L(M1) for
some PDA M1.
Proof
Let M2 = (Q, Σ, Γ, δ, q0, Z0 , {}), and N(M2) = L.
Construct M1= (Q{qf, q0'}, Σ, Γ{Z'}, δ', q0', Z', {qf}), where
δ'(q0', , Z') ={(q0, Z0Z')},
δ'(q, a, X) = δ(q, a, X), for all q in Q, a in Σ{}, X in Γ.
For all q in Q, δ'(q, , Z') contains (qf, ).
5.2 Equivalence of PDA's and CFL's
Theorem 3 L is a context-free language  L = N(M) for some
PDA M.
Proof
Assume  is not in L and L = L(G) for some CFG in
Greiback normal form G = (V, T, P, S).
Let M = ({q}, T, V, δ, q, S , {}), where
δ(q, a, A) contains (q, ), whenever Aa is in P.
We have that
δ(q, a, A) contains (q, ), whenever Aa is in P.
Example L ={anbn | n≧1} is a context-free language .
L = L(G), G = (V, T, P, S) a CFG in Greiback normal form, where
S  aSB,
S  aB,
B  b.
Let M = ({q}, T, V, δ, q, S , {}), a PDA, where
δ(q, a, S) contains (q, SB), where SaSB is in P,
δ(q, a, S) contains (q, B), where SaB is in P,
δ(q, b, B) contains (q, ), where Bb is in P.
a
a
b
δ(q, a, S) := (q, SB)
b
q
a
a
b
b
S
B
q
S
δ(q, a, S) := (q, B)
a
a
b
b
B
B
q
δ(q, b, B) := (q, )
a
b
b
q
a
b
b
q
δ(q, b, B) := (q, )
a
a
B
Theorem 4 L = N(M) for some PDA M  L is a context-free
language.
Proof Let M = (Q, T, Γ,δ, q0, Z0, {}) be the PDA.
Let G = (V, T, P, S) be a context-free grammar, where
V is the set of elements of the form [q, A, p], q and p in Q, and A
in Γ, plus the new symbol S.
P is the set of productions:
S  [q0, Z0, q] for each q in Q;
[q, A, qm+1]  a[q1, B1, q2][q2, B2, q3]… [qm, Bm, qm+1] for
each q, q1, q2, …,qm+1 in Q,
a in {}, and
A, B1, B2, …, Bm in Γ,
if (q, a, A) contains (q1, B1B2…Bm), for m>0.
If m = 0, the production is [q, A, q1]  a.
The key concept of the construction is the following:
A leftmost derivation in G of a sentence x is a simulation of
the PDA M when the input is x.
The variables that appear in any step of a leftmost derivation
in G correspond to the symbols on the stack of M at a time
when M has scanned as much of the input as the grammar has
already generated.
In another word, [q, A, p] derives x if and only if x causes M
to erase the symbol A from its stack by some sequence of
moves beginning in state q and ending in state p.
Example L ={anbn | n≧1} is a context-free language .
Let PDA M = ({q}, {a, b}, {S}, δ, q, S , {}), where
δ(q, a, S) = {(q, Sb), (q, b)}
δ(q, b, b) = {(q, )}
Construct G = (V, T, P, S) from M as follows:
V = {S, [q, S, q]}
P contains the following productions:
S  [q, S, q]
[q, S, q]  a[q, S, q][q, b, q]
[q, b, q]  b
[q, S, q]  a[q, b, q]