CSC 473 Automata, Grammars & Languages 10/20/10 CSC 473 Automata, Grammars & Languages 10/20/10 2 C

download CSC 473 Automata, Grammars & Languages 10/20/10 CSC 473 Automata, Grammars & Languages 10/20/10 2 C

of 5

  • date post

    12-Mar-2020
  • Category

    Documents

  • view

    7
  • download

    0

Embed Size (px)

Transcript of CSC 473 Automata, Grammars & Languages 10/20/10 CSC 473 Automata, Grammars & Languages 10/20/10 2 C

  • CSC 473 Automata, Grammars & Languages 10/20/10

    1

    C SC 473 Automata, Grammars & Languages 41

    PDA CFG Lemma 2.27: There is an algorithm for constructing, from any PDA P, a CFG G such that L(G) = L(P).

    Pf: Given a PDA we can convert it into a PDA with the following simplified structure: • it has only one accept state:

    • add ε-transitions from multiple accept states • it empties its stack just before entering the accept state:

    •Loop on a state that just pops:

    • each PDA transition is either a “pure push” or a “pure pop

    - introduce new intermediate states

    { } accept

    F q= ,

    accept f q

    ! ! !"

    "

    ,

    , pop pop

    X

    q q X ! !"

    # $"

    ,a X! "

    ,a X !"

    0 ( , , , , , )P Q q F!= " #

    C SC 473 Automata, Grammars & Languages 42

    PDA CFG (contʼd)  becomes  becomes

    • Idea of proof: construct G with variables for each p and q in the set of states Q. Arrange that if

    generates terminal string x, then PDA P started in state p with an empty stack on input string x has a computation that reaches state q with an empty stack. And conversely, if P started in state p with an empty stack has a computation on input string x that reaches state q with an empty stack, then

    How does P, when started on an empty stack in state p, operate on an input string x, ending with an empty stack in state q ?  First move must be a push  Last move must be a pop

    ,a X Y! , ,a X Y! ! !" "

    ,a ! !" , ,a X X! ! !" "

    pq A

    pq A

    . pq G

    A x!

    ,a X! "

    ,b X !"

    C SC 473 Automata, Grammars & Languages 43

    PDA CFG (contʼd)  Trace computation of P on x starting in state p with

    empty stack, and ending in state q with empty stack: (1) stack never empties

    pq G rs A aA b!

    p q a b

    rs GA y! !"""""#"""""$

    input

    St ac

    k he

    ig ht

    r s

    pq GA x! !"""""#"""""$

    push X! pop X !

    Fig. 1

  • CSC 473 Automata, Grammars & Languages 10/20/10

    2

    C SC 473 Automata, Grammars & Languages 44

    PDA CFG (contʼd)  Trace computation of P on x starting in state p with

    empty stack, and ending in state q with empty stack: (2) stack empties somewhere

    pq G pr rq A A A!

    p q

    r q GA z! !"""""#"""""$

    input

    St ac

    k he

    ig ht

    r

    pr GA y! !"""""#"""""$

    Fig. 2

    C SC 473 Automata, Grammars & Languages 45

    PDA CFG (contʼd) Construction. Given PDA

    construct with the following rules in R:

    ◆ If

    ◆ then

    ( , )( ) pq p q

    p q Q Q A A A! " ! " # r r

    r

    &

    pq rs A aA b!

    0 ( , , , , ,{ })

    accept P Q q q!= " #

    0, ( , , , )

    acceptq q G V R A= !

    ( ) pp

    p Q A !" # $

    ( , , , )( )( , )p q r s Q X a b !

    " # " # $ " # %

    ,a X! " p r

    ,b X !" s q

    C SC 473 Automata, Grammars & Languages 46

    PDA CFG (contʼd) Claim 2.30: If then Pf: by induction on a derivation in G length k. Base: k=1. The only derivations of length 1 are and we have Step: Assume (IH) true for derivations of ≤ k steps. Want Claim true for derivations of k+1 steps. Suppose that . The first derivation step is either of the form or Case . Then with So IH ⇒ By construction, since is a rule of G,

    pq G p q A A A!

    r r

    pq G rs A aA b!

    pq G A x!

    (p,x,!) !

    P

    " (q,!,!)

    k

    pq G A x!

    1

    pp G A !"

    (p,!,!) !

    P

    " (p,!,!)

    1k

    pq G A x

    + !

    x ayb=

    . k

    rs G A y!

    (r,y,!) !

    P

    " (s,!,!).

    pq rs A aA b!

    pq G rs A aA b!

  • CSC 473 Automata, Grammars & Languages 10/20/10

    3

    C SC 473 Automata, Grammars & Languages 47

    PDA CFG (contʼd)

    Case . Then with So IH ⇒

    Putting these together:

    pq G p q A A A!

    r r

    ! (p,x,") !

    P (r,yb,X)!

    P

    # (s,b,X)!

    P (q,",")

    x yz=

    & . k k

    pr G rq G A y A z

    ! ! " "

    (p,y,!) !

    P

    " (r,!,!) & (r,z,!) !

    P

    " (q,!,!)

    (p,yz,!) !

    P

    " (r,z,!) = (r,z,!) !

    P

    " (q,!,!)!

    ( ) ( , ) ( , , )&( , ) ( , , ).X r X p a q s b X! " " !# $ $

    C SC 473 Automata, Grammars & Languages 48

    PDA CFG (contʼd) Claim 2.31: If then Pf: by induction on a computation in P of length k:

    Base: k=0. The only computations of length 0 are where x = ε. By construction

    Step: Assume (IH) true for computations of ≤ k steps. Want Claim true for computations of k+1 steps. Suppose that . Two cases: either the stack does not empty in midst of this computation (Fig. 1) or it Becomes empty during the computation (Fig. 2). Call these Case 1 and Case 2.

    . pq G

    A x !

    " (p,x,!) !

    P

    " (q,!,!)

    1 .

    pp G A !" (p,x,!) !

    P

    0 (p,!,!)

    (p,x,!) !

    P

    k (q,!,!)

    (p,x,!) !

    P

    k +1 (q,!,!).

    C SC 473 Automata, Grammars & Languages 49

    PDA CFG (contʼd) Case 1: See Fig.1. The symbol X pushed in the 1st move Is the same as that popped in the last move. Let the 1st and last moves be governed by the push/pop transitions:

    By construction, there is a rule in G Let x = ayb. Since then we must have By IH Then Using we conclude

    . pq rs

    A aA b!

    . pq G

    A ayb x !

    " =

    . rs G A y

    ! "

    (r,y,X) !

    P

    k !1 (s,",X)

    ( , ) ( , , )&( , ) ( , , ).r X p a q s b X! " " !# #

    (r,y,!) !

    P

    k "1 (s,!,!).

    1

    pq G rs A aA b!

  • CSC 473 Automata, Grammars & Languages 10/20/10

    4

    C SC 473 Automata, Grammars & Languages 50

    PDA CFG (contʼd) Case 2: See Fig.2. Let r be the intermediate state where

    the stack becomes empty. Then

    By the IH, and Since by construction there is a rule in G of the form

    then pq pr rq

    A A A!

    pr G A y

    ! "

    ( , )y z x yz! =

    (p,y,!) !

    P

    "k (r,!,!) & (r,z,!) !

    P

    "k (q,!,!)

    . rq G A z

    ! "

    . pq G pr rq G

    A A A yz x !

    " " = !

    C SC 473 Automata, Grammars & Languages 51

    PDA CFG (contʼd) Ex:

    Rules of G: (1) push-pop pairs (1st kind):

    # # sf qq A A!

    (, A! "

    s

    #, $! " f

    #,$ !"

    ),A !"

    is a well-balanced string of parentheses{ {(, )}( ) }! *

    L P = #w# | w

    q

    s #, $! "

    q

    q

    (, A! "

    f

    q

    #,$ !"

    q

    q ),A !"

    q

    :

    { , , }

    P

    Q s q f= { ,$}

    {#,(,)}

    A! =

    " =

    ( ) qq qq A A!

    C SC 473 Automata, Grammars & Languages 52

    PDA CFG (contʼd) Note: If (p´ unreachable) then

    (abbreviated ). Such variables are useless; all rules involving them on left or right sides can be eliminated as useless productions. For this grammar (2) Rules of the 2nd Kind (with useless rules removed—only 10/27 survive) in the order s,q,f:

    (p,!,!) !

    P

    " ( #p ,!,!)

    ss ss ss

    sq ss sq

    sq sq qq

    sf ss sf

    sf sq qf

    sf sf ff

    A A A A A A

    A A A

    A A A A A A

    A A A

    !

    !

    !

    !

    !

    !

    { | } pp G

    x A x !

    " # = $ ppA ! = "

    , , fq qs fs A A A= ! = ! = !

    qq qq qq

    qf qq qf

    qf qf ff

    ff ff ff

    A A A

    A A A

    A A A

    A A A

    !

    !

    !

    !

  • CSC 473 Automata, Grammars & Languages 10/20/10

    5

    C SC 473 Automata, Grammars & Languages 53

    PDA CFG (contʼd) (2) Rules of the 3rd Kind:

    Combining all rules with same LHS:

    ss

    qq

    ff

    A A

    A

    !

    !

    !

    "

    "

    "

    |

    |

    # # | | |

    ( )| |

    |

    |

    ss ss ss