Skip to content

10. PDAs and CFGs

Feb 8-10, 2022

PDA CFG

Prove in two directions:

  1. PDA CFG

    Convert any context-free grammar G to a PDA M with L(G)=L(M)

    Take any arbitrary CFG G, conversion procedure:

    • for each production Aw in G, add transition: ϵ,Aw
    • for each terminal a in G, add transition: a,aϵ



    Example:

    GRAMMAR                      ε,S → aSTb
    S → aSTb                     ε,S → b
    S → b                        ε,T → Ta
    T → Ta                       ε,T → ε
    T → ε                        a,a → ε
                                 b,b → ε
                   ε,ε → S          ⮏        ε,$ → $
     -→ ( q0 ) -----------------> ( q1 ) ---------------------> 《 q2 》

    In general we can show that grammar G generates string w Sw iff and only if PDA M accepts w (q0,w,$)(q2,ϵ,$).


  2. PDA CFG

    Convert any PDA M to a context-free grammar G with L(G)=L(M)

    Take any arbitrary PDA M, modify PDA M so that:

    • the PDA has a single accept state
    • use new initial stack symbol # (not used in any transitions)
    • On acceptance the stack contains only stack symbol #
    • Each transition either pushes a symbol or pops a symbol but not both together

    Using this modified PDA, construct the grammar

    Denoting variables: Aqi,qj where qi,qj are states of PDA

    for each state q, grammar Aqqϵ
    for every 3 states p,q,r ApqAprArq
    for every pair of transitions   pa,ϵtt     sb,tϵq ApqaArsb
    Initial state q0 and accept state qf Start variable Aq0qf

Proving L(G) = L(M)

  1. L(G)L(M)

    We need to show that if G has derivation Aq0qfw there is an accepting computation in M (q0,w,#)(qf,ϵ,#) with input string w. Will actually show that if G has derivation Apqw, then there is a computation in M (p,w,ϵ)(q,ϵ,ϵ).

    Since there is no transition with # symbol:

    Aq0qfw     =>     (q0,w,ϵ)(qf,ϵ,ϵ)     =>     (q0,w,#)(qf,ϵ,#)

    If Apqw (string of terminals) then there is a computation from state p to state q on string w which leaves the stack empty: (q0,w,ϵ)(qf,ϵ,ϵ)

    Can formally prove this claim by induction on the number of steps in derivation: Apqw where "" represents the number of steps.


    Base case: one derivation step Apqw

    means Aqqϵ kind production must have been used, therefore p=q and w=ϵ. This computation of PDA trivially exists: (p,ϵ,ϵ)(p,ϵ,ϵ).


    Inductive step: k derivation steps Apqkw

    suppose for k steps (p,w,ϵ)(q,ϵ,ϵ) holds. For k+1 steps, show that (p,w,ϵ)(q,ϵ,ϵ) holds.


    case 1:   ApqAprArqw, we can write w=yz then

        Apqy - at most k steps, and from IH (p,y,ϵ)(r,ϵ,ϵ)

        Arqz - at most k steps, and from IH (r,z,ϵ)(q,ϵ,ϵ)

        Then (p,yz,ϵ)(r,z,ϵ)(q,ϵ,ϵ), and since w=yz, (p,w,ϵ)(q,ϵ,ϵ).


    case 2:   ApqaArsbw, we can write w=ayb

    Apqy at most k steps. From IH, PDA has computation (r,y,ϵ)(s,ϵ,ϵ). Grammar contains production ApqaArsb and PDA contains transitions:

    • pa,ϵtt     =>   (p,ayb,ϵ)(r,yb,t)
    • sb,tϵq     =>   (s,b,t)(q,ϵ,ϵ)

    We know:   (r,y,ϵ)(s,ϵ,ϵ)     =>     (r,yb,t)(s,b,t)

    We also know:   (p,ayb,ϵ)(r,yb,t)   and   (s,b,t)(q,ϵ,ϵ)

    Therefore:   (p,ayb,ϵ)(r,yb,t)(s,b,t)(q,ϵ,ϵ)

    Since w=ayb,   (p,w,ϵ)(q,ϵ,ϵ).


  2. L(G)L(M)

    The proof for this direction works the same way as the previous inductive proof.

Therefore L(G)=L(M).