We have defined grammars, in particular, CFG in 3.4.0.7 . We will see in this section that CFG are an alternative formalism to that of PDA. They define exactly the same languages. First off we note that for any CFG, G =
, Σ, S ,
, we can assume without loss of generality that its instructions have two possible forms: A → a , where a
Σ ∪ {
}, and A → X 1 X 2 · · · X n —the right hand side being a string of X i , each of which is a nonterminal.
Indeed, if G does not already have the desired rule structure, we add new nonterminals, Y ( a ) , one for each a
Σ. We then replace each rule A → α—where α contains at least one nonterminal—with A → α′ , where α′ is the result of replacing each a occurring in α ( a
Σ) by Y ( a ) . Finally, we add the rules Y ( a ) → a , for all a
Σ.
We will prove that if the so constructed grammar is G′ , then L ( G ) = L ( G′ ), which justifies the “without loss of generality” above. Indeed, let x = a 1 a 2 · · · a k
L ( G ), where k = 0 means that x =
. Thus we have
in G . Let us replace, everywhere , every a i that occurs in the derivation (1), by its “alias” Y ( a i ) . This will transform the G -derivation (1) to a G′ -derivation
Utilizing the rules of the type Y ( a ) → a that we have placed in G′ we obtain
Y ( a 1 ) Y ( a 2 ) . . . Y ( a k )
a 1 Y ( a 2 ) . . . Y ( a k )
a 1 a 2 Y ( a 3 ) . . . Y ( a k )
k a 1 a 2 · · · a k
Along with (2), this says that x
L ( G ′).
Conversely, let y
L ( G ′). Thus, we have a G ′-derivation
We will show that we also have a G -derivation of y . In order to use induction on derivation length, we prove a somewhat more general statement:
For n = 1, the G ′-derivation in (4) can only be A
y , where y
Σ ∪ {
}. But this is also a G -derivation, since A
. We take as I.H. that the statement (4) holds for all derivation lengths < n . Note that the G ′-derivation in (4) can be elaborated as:
where for i = 1 , . . . , m , we have
and
n i \= n – 1. Thus
Every X i in (6) that is in
obeys the I.H. due to (7), thus
On the other hand, any X j in (5) that is not in
is a Y aj . We replace each such X j in (5) by a j . Thus the corresponding u j is a j .
Let us call
the so modified X 1 . . . X m . By the construction of G ′, A →
is a rule of G. This observation and (8) transform (5) into a G -derivation
We can now apply what we just proved to (3).
We these preliminaries out of the way, we can now turn to the equivalence of the CFG and PDA formalisms.
4.3.0.5 Theorem. For any CFG G =
, Σ, S ,
we can construct a PDA M =
Q , Σ, Γ, q 0 , δ , F
that accepts by ES such that L ( G ) = L ( M ).
Proof. Given the discussion preceding the theorem, without loss of generality, the rules of G are of either the type A → X 1 X 2 · · · X m or A → a , where capital Latin letters denote, as usual, nonterminals and a
Σ ∪ {
}. We have drawn below the PDA that we claim will work. We note that in the diagram below b
Σ.
The idea of how M “parses” an input string x is already embodied in the preamble of Section 3.4. The stack is used to, well, stack “to do” items on top of each other in the proper sequence. The first “to do”, given the input x , is to attempt to show that S —the start symbol—“covers” x , in the jargon used in the discussion of said preamble, or, technically, derives it. Now, if S → ABC is the right rule to use (nondeterminism!) next, we replace and refine the task for S by the subtasks that A , B , and C will have to cover appropriate parts of x : a prefix, a middle part, and the remaining tail. Note that A, appropriately, will be on top of the stack. Note that these tasks do not allow us to consume input. Input is consumed once a “ b ” (from Σ) is found on top of the stack; it is something we uncovered, and it better match the input symbol scanned at that point!
Technically, we will prove that, for any z
Σ*, γ
Γ*, A
, and x
Σ*, we have
For the (→) in (1) we do induction on n of
For the basis, let n = 1. The only way for (2) to happen in one move is if the move involved is
, A → x where x =
. But then A
x , as needed. Assume the claim for all values less than n . For n > 1 (the n = 1 case was done) we have two cases, one that does not need the I.H. and one that does.
-
n = 2: The first move is “
, A → a ”, where x = a
Σ. Then the corresponding IDs are connected as follows: ( p , az , A γ)
( p , az , a γ)
( p , z , γ). The first move requires A → a to be a rule of G , hence we have A
a , as needed. -
n > 2: We elaborate on (2) above: ( p , xz , A γ)
( p , xz , X 1 X 2 · · · X m γ)
n −1 ( p , z , γ). We further elaborate on “
n −1 ”: There is a decomposition of x as x = u 1 u 2 . . . u m , such that ( p , u 1 . . . u m z , A γ)
( p , u 1 . . . u m z , X 1 X 2 · · · X m γ)
k 1
( p , u 2 . . . u m z , X 2 · · · X m γ)
k 2 ( p , u 3 . . . u m z , X 3 · · · X m γ)
k 3
( p , u 4 . . . u m z , X 4 · · · X m γ)
k 4 . . . k m −1
( p , u m z , X m γ)
k m ( p , z , γ) For each i it takes k i < n steps (cf. 4.2.0.11 ) to lose X i from the stack top and to consume the portion u i of the input, that is, we have
By the I.H., we must conclude that
Coupling (3′) with the fact the first
in the second bullet above requires that A → X 1 · · · X m , is a G -rule, we obtain A
X 1 · · · X m
* u 1 . . . u m \= x , as needed.
For the (←) in (1) we do induction on n of
For n = 1 we have two cases:
-
x =
. Then we have A →
is a rule, thus we have ( p , xz , A γ)
( p , z , γ) using the move pictured at the top of the loop of the PDA ( p. 307 ). -
x = a . Then we have that A → a is a rule, thus we have ( p , az , A γ)
( p , az , a γ)
( p , z , γ), using the moves pictured at the top and top right of the loop of the PDA.
For n > 1 we take the obvious I.H. and elaborate on (4):
where each X j is responsible for the substring u j of x . More precisely,
where
< n means
k , for some k < n. The I.H. is applicable to (6), thus, from (5) we get
With (1) proved, let S
* x . Then
Note that ( p ,
,
) is terminal as all moves from p require a non-empty stack. Next, let ( q , x ,
)
* ( p ,
,
). The only move for q is ( q , x ,
)
( p , x , S ), thus our assumption implies ( p , x , S )
* ( p ,
,
). By (1), S
* x holds.
4.3.0.6 Theorem. For any PDA M =
Q , ∑, Γ, q 0 , δ, F
that accepts by ES + AS we can construct a CFG G =
, ∑, S ,
such that L ( G ) = L ( M ).
Proof . Without loss of generality, F = { q acc }. The nonterminals of G will be ordered pairs of states of M :
q , p
for all q , p in Q (think of them as BNF nonterminals!). We aim to design G so that we can prove
To achieve this we include the rules
(i)
q , q
→
, for all q
Q
(ii)
q , p
→
q , r
r , p
, for all q , p , and r in Q
(iii)
q , p
→ a
r , s
b , for all q , p , r , and s in Q and a , b in ∑ ∪ {
}, for all pairs of moves such as the ones below. The first pushes some X
Γ to the stack on input a and the second, later , pops the same X , on input b .
(iv) The start symbol, S , is
q 0 , q acc
.
Let us prove (1), splitting it into two directions:
(→): Assume
q , p
n x and conclude the right hand side of “iff”. We use induction on n . Given the kinds of rules that we adopted for G , the only string (over ∑) that can be produced for n = 1 is x =
. That means
q , p
,
q , p
→
being the responsible rule. Therefore p = q . But ( q ,
z , γ)
* ( q , z , γ) holds since
* includes equality. Of course, this subcomputation never pops γ.
Let n > 1. We assume the claim (1) for all derivation lengths k < n (the I.H.) Now
q , p
n x can be elaborated under two cases:
-
For some r
Q , we have
With reference to (2), the I.H. gives us the subcomputation
( q , uvz , γ)
* ( r , vz , γ)
* ( p , z , γ) where (still under the I.H.) none of the two “
*” pop from γ, hence nor does the entire subcomputation ( q , uvz , γ)
* ( p , z , γ). -
x = aub —{ a , b } ⊆ ∑ ∪ {
}—and, for some r and s in Q , we have
The first
entails that
q , p
→ a
r , s
b is a rule in G , and this in turn means that, for some X
Γ, we have the two moves in M , depicted in the preceding figure. On the other hand, by the I.H. we have, for any γ, z , and the X mentioned above, ( r , ubz , X γ)
* ( s , bz , X γ) where the stack X γ was never popped. Combining with the moves involving X , we obtain
( q , xz , γ) = ( q , aubz , γ)
( r , ubz , X γ)
* ( s , bz , X γ)
( p , z , γ) Clearly, since X γ was never popped in the above, nor was γ.
This completes what we set out to do.
For the (←) direction of (1),
we assume that we have ( q , xz , γ)
n ( p , z , γ)— where we never popped γ (4)
and prove
q , p
* x .
For n = 0 the assumption is ( q , xz , γ)
0 ( p , z , γ). But
0 is the identity, therefore q = p and x =
. Since
q , q
→
is a rule, we have
q , q
( = x ), as it was needed.
We next fix n > 0, adopt the I.H. that the claim is true for all k < n , and address the case for n . We have two subcases
-
Case where the computation in (4)—while it never pops γ—nevertheless “dips down” to (stack contents) γ in at least one intermediate instance (that is, it pops from stack (contents) Y γ, for some Y ). Say this happens for the first time after a prefix u of x has been consumed:
( q , xz , γ) = ( q , uvz , γ)
< n ( r , vz , γ)
< n ( p , z , γ) By the I.H. we have
q , r
* u and
r , p
* v . Since
q , p
→
q , r
r , p
is a rule and thus
q , p
q , r
r , p
, we conclude
q , p
* x , as was needed. -
Case where the computation in (4)—not only it never pops γ—but also never “dips down” to (stack contents) γ in any instance except at the end of the very last move that leads to ID ( p , z , γ). Thus the first move must push in the stack some symbol X , and the last move of (4) must pop that very same X .
We are proving this metatheoretical result for the formal PDA model as this was defined in 4.2.0.5 . As such, this type of PDA has only push or pop moves; it does not have “ignore the stack” moves—
→
—that we introduced post facto as derived moves or macros in Example 4.2.0.6 . Thus, pop being ruled out in the first move, by the conditions of this case, push it is !
Thus, in (4), x = aub for some a and b in ∑ ∪ {
} and we refine (4) as ( q , aubz , γ)
( r , ubz , X γ)
< n ( s , bz , X γ)
( p , z , γ) By I.H. we have
r , s
* u . Since the first and last move put the rule
q , p
→ a
r , s
b in G , we have
q , p
* x and we are done with the proof of (1).
With (1) settled, we have in particular
( q 0 , x ,
)
* ( q acc ,
,
) iff
q 0 , q acc
* x
and this concludes the proof that L ( M ) = L ( G ).
The convenience stemming from Theorems 4.3.0.6 and 4.3.0.5 is significant as they allow us two distinct tools to use toward proving properties of PDA-acceptable languages: PDA and CFG.
4.3.0.7 Example. If L = L ( M ) for some PDA M , then there is a PDA N such that L * = L ( N ). That is, “PDA-acceptable languages are closed under Kleene star”.
We prove so (in outline) for CFLs instead!
Let then G be a CFG such that L = L ( G ). We built—rather than building N —a CFG G ′ such that L * = L ( G ′). In going from G to G ′ we just add a new nonterminal Z and make it the (new) start symbol. If S is the start symbol of G , we add the rules
Z →
| SZ
to G to finalize the construction of G ′.
The reader can easily show that Z
* S n , for all n ≥ 0, from which the result follows. The details are left to the reader.
4.3.0.8 Example. Are CFL closed under intersection? It turns out that they are not. Consider L = {0 n 1 n 2 k : n ≥ 0 ∧ k ≥ 0} over {0, 1, 2}. Also consider L ′ = {0 n 1 k 2 k : n ≥ 0 ∧ k ≥ 0}.
It is easily seen that they are CFL.
For L either give explicit rules, or, alternatively, note that it is the concatenation of two context free languages, L 1 and L 2 over {0, 1, 2}, and invoke Exercise 4.5.11 .
L 1 = {0 n 1 n : n ≥ 0}
and
L 2 = {2 k : k ≥ 0}
( L 2 is actually regular, being L (2*))
One similarly sees that L ′ is a CFL. However, L ∩ L ′ = {0 n 1 n 2 n : n ≥ 0} and we will see in the next section that this language is not context free.