Processing math: 100%
Close
Close Window

OpenDSA Stand-alone Modules

Chapter 0 modules

Show Source |    | About   «  0.274. Pushdown Automata   ::   Contents   ::   0.276. Deterministic Pushdown Automata  »

PDAs and Context Free Languages

1. PDAs and Context Free Languages

Now we want to show that NPDA's and CFG both represent CFL's. Show that we can take any CFG and construct a NPDA and vice versa.

Theorem: For any CFL L not containing λ, a NPDA M such that L=L(M).

Proof (sketch)

Given (λ -free) CFL L.
CFG G such that L=L(G).
G in GNF, such that L(G)=L(G).
G=(V,T,S,P). All productions in P are of the form:
Aax
AV,aT,xV
Construct NPDA M=(Q,Σ,Γ,δ,q0,z,F)
Q={q0,q1,qf},Σ=T,Γ=V{z},F={qf}
1. M starts by putting S on the stack
2. For each production
AaX1X2Xn
put (q1,X1X2Xn) in δ(q1,a,A)
(Pop A from the stack, read "a" from tape, and push X1X2Xn onto the stack)
3. Accept if SwΣ (all variables on the stack are replaced by terminals or λ)
Show wL(G) iff wL(M). QED.

Example 1

Let G=(V,T,S,P),P= | SaSA | aAA | b | AbBBB | Bb

QUESTION: Is this grammar in GNF? Yes.

lt7pf3

Trace abbbbb in grammar and pda.

Note

Argue why wL(G) iff wL(M). QED.

Now we want to show that given an NPDA, we can construct a CFG. But first, we will show a result to make the next proof easier.

Theorem: Given a NPDA M, a NPDA M such that all transitions have the form δ(qi,a,A)={c1,c2,cn} where

ci=(qj,λ)or ci=(qj,BC)

Each move either increases or decreases stack contents by a single symbol.

Proof: (sketch)

lt7pf4

Theorem: If L=L(M) for some NPDA M, then L is a CFL.

Note

Want to show that each NPDA represents a CFL, so we will take a NPDA M and convert it to a CFG.

It will be an easier construction if we take the NPDA and put all the transitions in a simpler form.

Proof:

Given NPDA M, first, construct an equivalent NPDA M that will be easier to work with. Construct M such that
1. M accepts if stack is empty
2. Each move increases or decreases stack content by a single symbol. (Can only push 2 variables or no variables with each transition.)
M=(Q,Σ,Γ,δ,q0,z,F)
Construct G=(V,Σ,S,P) where
V={(qicqj) | qi,qjQ,cΓ}
(Some of these variables will be useless.)
(qicqj) represents "starting at state qi the stack contents are cw,wΓ, some path is followed to state qj and the contents of the stack are now w".
Goal: (q0zqf) which will be the start symbol in the grammar.
Meaning: We start in state q0 with z on the stack and process the input tape. Eventually we will reach the final state qf and the stack will be empty. (Along the way we may push symbols on the stack, but these symbols will be popped from the stack).
(NOTE: Machine accepts by empty stack, but it is such that there is only 1 final state in which the machine accepts by final state.)
To construct the productions in P:
1) Replace
lt8pf5
by
(qiAqj)a
where the stack changes are:
qi  (some path )  qjstack:Astack:X1X1X2X2Xn_Xn_
2) Replace
lt8pf6
by
(qiAqk)a(qjBql)(qlCqk) for all ql,qkQ
qi  (path )  qj  (path )  ql  (path )  qkBACCX1X1X1X1X2X2X2X2Xn_Xn_Xn_Xn_
This will create some useless variables, but that's ok.
Must show that the constructed grammar G is such that L(G)=L(M). That is, wL(G)iff wL(M). (see book) QED.

Example 2

L(M)={aab}, M=(Q,Σ,Γ,δ,q0,z,F), Q={q0,q1,q2,q3}, Σ={a,b},Γ={A,z}, F={}. M accepts by empty stack.

lt8pda7
Construct the grammar G=(V,T,S,P),
V={(q0Aq0),(q0zq0),(q0Aq1),(q0zq1),}
NOTE: some variables may be useless.
T=Σ
S=(q0zq2)
P=
From transition 1(q0Aq1)bFrom transition 2(q1zq2)λFrom transition 3(q0Aq3)aFrom transition 4(q0zq0)a(q0Aq0)(q0zq0)|a(q0Aq1)(q1zq0)|a(q0Aq2)(q2zq0)|a(q0Aq3)(q3zq0)(q0zq1)a(q0Aq0)(q0zq1)|a(q0Aq1)(q1zq1)|a(q0Aq2)(q2zq1)|a(q0Aq3)(q3zq1)(q0zq2)a(q0Aq0)(q0zq2)|a(q0Aq1)(q1zq2)|a(q0Aq2)(q2zq2)|a(q0Aq3)(q3zq2)(q0zq3)a(q0Aq0)(q0zq3)|a(q0Aq1)(q1zq3)|a(q0Aq2)(q2zq3)|a(q0Aq3)(q3zq3)From transition 5(q3zq0)(q0Aq0)(q0zq0)|(q0Aq1)(q1zq0)|(q0Aq2)(q2zq0)|(q0Aq3)(q3zq0)(q3zq1)(q0Aq0)(q0zq1)|(q0Aq1)(q1zq1)|(q0Aq2)(q2zq1)|(q0Aq3)(q3zq1)(q3zq2)(q0Aq0)(q0zq2)|(q0Aq1)(q1zq2)|(q0Aq2)(q2zq2)|(q0Aq3)(q3zq2)(q3zq3)(q0Aq0)(q0zq3)|(q0Aq1)(q1zq3)|(q0Aq2)(q2zq3)|(q0Aq3)(q3zq3)
Recognizing aaab in M:
(q0,aaab,z)(q0,aab,Az)(q3,ab,z)(q0,ab,Az)(q3,b,z)(q0,b,Az)(q1,λ,z)(q2,λ,λ)
At this point stack is empty.
Derivation of string aaab in G:
(q0zq2)a(q0Aq3)(q3zq2)aa(q3zq2)aa(q0Aq3)(q3zq2)aaa(q3zq2)aaa(q0Aq1)(q1zq2)aaab(q1zq2)aaab
Meaning of first line in derivation is: (q0zq2)axy where (q0Aq3)x (which in the example above will eventually derive a) and (q3zq2)y (which in the example above will eventually derive ab).
Must show that the constructed grammar G is such that L(G)=L(M). That is, wL(G) iff wL(M). (see book) QED.

   «  0.274. Pushdown Automata   ::   Contents   ::   0.276. Deterministic Pushdown Automata  »

nsf
Close Window