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 = Images Images , Σ, S , Images Images , we can assume without loss of generality that its instructions have two possible forms: A → a , where a Images Σ ∪ { images }, 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 Images Σ. 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 Images Σ) by Y ( a ) . Finally, we add the rules Y ( a ) → a , for all a Images Σ.

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 images L ( G ), where k = 0 means that x = images . Thus we have

Images

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

Images

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 ) Images a 1 Y ( a 2 ) . . . Y ( a k ) Images a 1 a 2 Y ( a 3 ) . . . Y ( a k ) Images k a 1 a 2 · · · a k

Along with (2), this says that x Images L ( G ′).

Conversely, let y Images L ( G ′). Thus, we have a G ′-derivation

Images

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:

Images

For n = 1, the G ′-derivation in (4) can only be A Images y , where y Images Σ ∪ { images }. But this is also a G -derivation, since A images Images . 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:

Images

where for i = 1 , . . . , m , we have

Images

and Images n i \= n – 1. Thus

Images

Every X i in (6) that is in Images obeys the I.H. due to (7), thus

Images

On the other hand, any X j in (5) that is not in Images 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 Images the so modified X 1 . . . X m . By the construction of G ′, A → Images is a rule of G. This observation and (8) transform (5) into a G -derivation

Images

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 = Images Images , Σ, S , Images Images we can construct a PDA M = Images Q , Σ, Γ, q 0 , δ , F Images 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 Images Σ ∪ { images }. We have drawn below the PDA that we claim will work. We note that in the diagram below b Images Σ.

Images

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 Images Σ*, γ Images Γ*, A Images Images , and x Images Σ*, we have

Images

For the (→) in (1) we do induction on n of

Images

For the basis, let n = 1. The only way for (2) to happen in one move is if the move involved is images , A → x where x = images . But then A Images 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 “ images , A → a ”, where x = a Images Σ. Then the corresponding IDs are connected as follows: ( p , az , A γ) Images ( p , az , a γ) Images ( p , z , γ). The first move requires A → a to be a rule of G , hence we have A Images a , as needed.

  • n > 2: We elaborate on (2) above: ( p , xz , A γ) Images ( p , xz , X 1 X 2 · · · X m γ) Images n −1 ( p , z , γ). We further elaborate on “ Images 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 γ) Images ( p , u 1 . . . u m z , X 1 X 2 · · · X m γ) Images k 1          
    ( p , u 2 . . . u m z , X 2 · · · X m γ) Images k 2 ( p , u 3 . . . u m z , X 3 · · · X m γ) Images k 3
    ( p , u 4 . . . u m z , X 4 · · · X m γ) Images k 4 . . . k m −1                                      
    ( p , u m z , X m γ) Images 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

    Images

    By the I.H., we must conclude that

    Images

    Coupling (3′) with the fact the first Images in the second bullet above requires that A → X 1 · · · X m , is a G -rule, we obtain A Images X 1 · · · X m Images * u 1 . . . u m \= x , as needed.

For the (←) in (1) we do induction on n of

Images

For n = 1 we have two cases:

  • x = images . Then we have A → images is a rule, thus we have ( p , xz , A γ) Images ( 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 γ) Images ( p , az , a γ) Images ( 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):

Images

where each X j is responsible for the substring u j of x . More precisely,

Images

where Images < n means Images k , for some k < n. The I.H. is applicable to (6), thus, from (5) we get

images

With (1) proved, let S images * x . Then

images

Note that ( p , Images , Images ) is terminal as all moves from p require a non-empty stack. Next, let ( q , x , Images ) Images * ( p , Images , Images ). The only move for q is ( q , x , Images ) Images ( p , x , S ), thus our assumption implies ( p , x , S ) Images * ( p , Images , Images ). By (1), S images * x holds. images

4.3.0.6 Theorem. For any PDA M = Images Q , ∑, Γ, q 0 , δ, F Images that accepts by ES + AS we can construct a CFG G = Images images , ∑, S , images Images 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 : Images q , p Images for all q , p in Q (think of them as BNF nonterminals!). We aim to design G so that we can prove

images

To achieve this we include the rules

(i) Images q , q Images → Images , for all q images Q

(ii) Images q , p Images → Images q , r Images Images r , p Images , for all q , p , and r in Q

(iii) Images q , p Images → a Images r , s Images b , for all q , p , r , and s in Q and a , b in ∑ ∪ { Images }, for all pairs of moves such as the ones below. The first pushes some X images Γ to the stack on input a and the second, later , pops the same X , on input b .

images

(iv) The start symbol, S , is Images q 0 , q acc Images .

Let us prove (1), splitting it into two directions:

(→): Assume Images q , p Images images 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 = Images . That means Images q , p Images Images Images , Images q , p Images → Images being the responsible rule. Therefore p = q . But ( q , Images z , γ) Images * ( q , z , γ) holds since Images * 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 Images q , p Images images n x can be elaborated under two cases:

  • For some r images Q , we have

    images

    With reference to (2), the I.H. gives us the subcomputation

    ( q , uvz , γ) Images * ( r , vz , γ) Images * ( p , z , γ)

    where (still under the I.H.) none of the two “ Images *” pop from γ, hence nor does the entire subcomputation ( q , uvz , γ) Images * ( p , z , γ).

  • x = aub —{ a , b } ⊆ ∑ ∪ { Images }—and, for some r and s in Q , we have

    images

    The first images entails that Images q , p Images → a Images r , s Images b is a rule in G , and this in turn means that, for some X images Γ, 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 γ) Images * ( s , bz , X γ)

    where the stack X γ was never popped. Combining with the moves involving X , we obtain

    ( q , xz , γ) = ( q , aubz , γ) Images ( r , ubz , X γ) Images * ( s , bz , X γ) Images ( 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 , γ) Images n ( p , z , γ)— where we never popped γ   (4)

and prove Images q , p Images images * x .

For n = 0 the assumption is ( q , xz , γ) Images 0 ( p , z , γ). But Images 0 is the identity, therefore q = p and x = Images . Since Images q , q Images → images is a rule, we have Images q , q Images images Images ( = 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 , γ) Images < n ( r , vz , γ) Images < n ( p , z , γ)

    By the I.H. we have Images q , r Images images * u and Images r , p Images images * v . Since Images q , p Images → Images q , r Images Images r , p Images is a rule and thus Images q , p Images images Images q , r Images Images r , p Images , we conclude Images q , p Images images * 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 .

    Images   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— Images → Images —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 ! Images

    Thus, in (4), x = aub for some a and b in ∑ ∪ { Images } and we refine (4) as

    ( q , aubz , γ) Images ( r , ubz , X γ) Images < n ( s , bz , X γ) Images ( p , z , γ)

    By I.H. we have Images r , s Images images * u . Since the first and last move put the rule Images q , p Images → a Images r , s Images b in G , we have Images q , p Images images * x and we are done with the proof of (1).

With (1) settled, we have in particular

( q 0 , x , Images ) Images * ( q acc , Images , Images ) iff Images q 0 , q acc Images images * x

images 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 → Images | SZ

to G to finalize the construction of G ′.

The reader can easily show that Z images * S n , for all n ≥ 0, from which the result follows. The details are left to the reader. images

images 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*))

images 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. images