class12 by chenshu

VIEWS: 3 PAGES: 74

									    Formal Languages
   Deterministic PDAs
        Hinrich Schütze
IMS, Uni Stuttgart, WS 2006/07
      Slides based on RPI CSCI 2400

         Thanks to Costas Busch
        Deterministic PDA: DPDA
Allowed transitions:

                    a, b  w
               q1              q2




                    , b  w
               q1              q2

            (deterministic choices)
                                      2
Allowed transitions:



  a, b  w1     q2           , b  w1   q2

  q1                        q1

   a , c  w2   q3           , c  w2   q3

            (deterministic choices)
                                              3
Not allowed:


 a, b  w1     q2            , b  w1   q2

 q1                         q1

  a , b  w2   q3           a , b  w2   q3


          (non deterministic choices)
                                              4
          DPDA example


        L( M )  {a b : n  0}
                   n n



        a,   a      b, a  


q0 a,   a q b, a   q , $  $ q
             1          2          3

                                   5
Definition:
A language L is deterministic context-free
if there exists some DPDA that accepts it


Example:
    The language   L( M )  {a b : n  0}
                             n n


     is deterministic context-free

                                             6
   Example of Non-DPDA (PDA)

        L( M )  {vv : v {a, b} }
                   R             *




a,   a           a, a  
b,   b           b, b  


   q0   ,          q1   , $  $   q2

                                            7
            Not allowed in DPDAs



a,   a           a, a  
b,   b           b, b  


   q0   ,        q1   , $  $   q2

                                          8
       PDAs

Have More Power than

      DPDAs



                       9
It holds that:

Deterministic
Context-Free
 Languages
                       Context-Free
                         Languages
                            PDAs
   (DPDA)


   Since every DPDA is also a PDA




                                       10
We will actually show:

Deterministic

                  
                         Context-Free
Context-Free
                          Languages
 Languages
                            (PDA)
   (DPDA)
     L                      L

We will show that there exists
a context-free language L which is not
accepted by any DPDA
                                         11
The language is:


   L  {a b }  {a b }
         n n       n 2n
                                n0


We will show:

     • L is context-free

     • L is not deterministic context-free


                                             12
Proof?




         13
           L  {a b }  {a b }
                    n n      n 2n


Language   L is context-free

Context-free grammar for    L:
      S  S1 | S2          {a nbn }  {a nb2n }

     S1  aS1b | 
                             n n
                           {a b }


     S2  aS2bb |           n 2n
                           {a b }
                                                  14
Theorem:

    The language   L  {a b }  {a b }
                        n n          n 2n

    is not deterministic context-free



    (there is no DPDA that accepts    L)



                                            15
Proof: Assume for contradiction that

             L  {a b }  {a b }
                    n n     n 2n

        is deterministic context free



Therefore:

  there is a DPDA   M that accepts L

                                        16
DPDA   M with L( M )  {a b }  {a b }
                                n n   n 2n


                     n n
          accepts   a b

            n n                 n
           a b              b



                           n 2n
              accepts     a b
                                             17
DPDA   M with L( M )  {a b }  {a b }
                           n n      n 2n



  Such a path exists due to determinism


       M
            n n               n
           a b            b



                                           18
Fact 1:    The language {a b
                              n n n
                                  c }
           is not context-free


             Context-free languages
                     a nb n

               Regular languages
                     a *b *


 (we will prove this at a later class using
 pumping lemma for context-free languages)
                                              19
Fact 2:   The language L  {a
                                   n n n
                                   b c }
          is not context-free



          ( L  {a b }  {a b })
                n n       n 2n




     (we can prove this using pumping lemma
     for context-free languages)
                                              20
We will construct a PDA that accepts:

             L  {a b c }
                       n n n


          ( L  {a b }  {a b })
                 n n       n 2n




         which is a contradiction!


                                        21
DPDA     M             L( M )  {a b }  {a b }
                                     n n       n 2n


              a nb n                 bn



Modify   M         Replace       b
                   with c

DPDA     M            L( M )  {a nc n }  {a nc 2n }
              a nc n                 cn


                                                          22
A PDA that accepts   L  {a b c }
                          n n n

      Connect the final states of M
      with the final states of M 
      M
              n n             n
            a b           b



                                 
      M
            a nc n        cn


                                      23
Since     L  {a b c } is accepted by a PDA
                n n n


     it is context-free


        Contradiction!



 (since    L  {a b c } is not context-free)
                 n n n




                                               24
Therefore:

             L  {a b }  {a b }
                  n n       n 2n

      Is not deterministic context free

      There is no DPDA that accepts it




                 End of Proof
                                          25
  Positive Properties
           of
Context-Free languages




                         26
                       Union

             Context-free languages
             are closed under:   Union


L1   is context free
                                 L1  L2
L2   is context free           is context-free


                                            27
Proof?




         28
                 Example
  Language                   Grammar

 L1  {a b }
       n n
                           S1  aS1b | 

 L2  {ww }  R
                      S2  aS2a | bS2b | 

                  Union

L  {a b }  {ww }
     n n          R         S  S1 | S2
                                             29
In general:
   For context-free languages    L1,   L2
   with context-free grammars    G1,   G2
   and start variables           S1,   S2


  The grammar of the union    L1  L2
  has new start variable      S
  and additional production   S  S1 | S2
                                            30
                Concatenation

      Context-free languages
      are closed under:      Concatenation



L1   is context free
                                    L1L2
L2   is context free            is context-free


                                             31
Proof?




         32
                  Example
 Language                       Grammar

L1  {a b }
      n n
                            S1  aS1b | 

L2  {ww }  R
                        S2  aS2a | bS2b | 

                Concatenation

L  {a b }{ww }
     n n           R            S  S1S2
                                               33
In general:

   For context-free languages   L1,   L2
   with context-free grammars   G1,   G2
   and start variables          S1,   S2


   The grammar of the concatenation L1L2
   has new start variable    S
   and additional production S  S1S2

                                           34
             Star Operation



    Context-free languages
    are closed under:      Star-operation




L is context free         *
                         L is context-free

                                            35
Proof?




         36
               Example

 Language                 Grammar


L  {a b }
      n n
                         S  aSb | 


         Star Operation


L  {a b } *
      n n
                         S1  SS1 | 
                                        37
In general:

   For context-free language   L
   with context-free grammar   G
   and start variable
                               S


   The grammar of the star operation L *
   has new start variable    S1
   and additional production S1  SS1 | 

                                            38
  Negative Properties
          of
Context-Free Languages




                         39
                Intersection


       Context-free languages
       are not closed under:  intersection

L1   is context free
                                 L1  L2
L2   is context free           not necessarily
                               context-free

                                             40
                 Example

L1  {a b c }
       n n m
                           L2  {a b c }
                                   n m m

 Context-free:               Context-free:
 S  AC                      S  AB
 A  aAb |                  A  aA | 
 C  cC |                   B  bBc | 
              Intersection

L1  L2  {a b c } NOT context-free
           n n n
                                             41
              Complement


     Context-free languages
     are not closed under:  complement



L is context free      L   not necessarily
                           context-free


                                         42
                Example

L1  {a b c }
      n n m
                          L2  {a b c }
                                 n m m

Context-free:              Context-free:
S  AC                     S  AB
A  aAb |                 A  aA | 
C  cC |                  B  bBc | 
              Complement
    L1  L2  L1  L2  {a b c }
                             n n n

         NOT context-free
                                           43
     Intersection
          of
Context-free languages
         and
  Regular Languages



                         44
 The intersection of
           a context-free language and
           a regular language
 is a context-free language



L1   context free
                             L1  L2
L2   regular               context-free

                                          45
Machine   M1            Machine     M2
NPDA for   L1          DFA for      L2
 context-free             regular


    Construct a new NPDA machine   M
    that accepts L1  L2


M simulates in parallel M1 and M 2
                                         46
     NPDA    M1                  DFA       M2
      a, b  c                         a
q1                q2        p1                  p2
     transition                  transition



                   NPDA    M

             q1, p1 a, b  c q2 , p2
                   transition
                                                     47
     NPDA    M1                   DFA   M2
      , b  c
q1                q2         p1
     transition



                   NPDA    M

             q1, p1  , b  c q2 , p1
                   transition
                                             48
NPDA      M1                    DFA    M2

     q0                               p0
initial state                   initial state



                NPDA        M

                  q0 , p0
                Initial state
                                                49
NPDA     M1                  DFA   M2

    q1                    p1            p2
final state                  final states



              NPDA    M

         q1, p1        q1, p2
              final states
                                             50
Example:               context-free
  L1  {w1w2 : | w1 || w2 |, w1 {a, b}*, w2 {c, d }*}


                 NPDA      M1
               a,   1       c,1  
               b,   1       d ,1  


    q0  ,    q1  ,    q2  ,    q3


                                                           51
  regular
            *
L2  {a, c}



DFA       M2
   a, c


    p0


                52
Intersection?




                53
                                 context-free
Automaton for: L1  L2  {a nc n : n  0}


                            NPDA      M

                    a,   1         c,1  


  q0 , p0    ,    q1, p0  ,    q2 , p0  ,      q3 , p0




                                                                     54
In General:

M simulates in parallel M1 and M 2
M accepts string w if and only if

         M1   accepts string   w     and

        M2    accepts string   w

        L( M )  L( M1)  L( M 2 )
                                           55
Therefore:
                 M is NPDA




       L ( M1 )  L ( M 2 )   is context-free




             L1  L2   is context-free

                                                56
 Applications
      of
Regular Closure




                  57
 The intersection of
           a context-free language and
           a regular language
 is a context-free language


                          Regular Closure
L1   context free
                             L1  L2
L2   regular               context-free

                                            58
    An Application of Regular Closure



                   n n
Prove that:   L  {a b : n  100, n  0}

               is context-free




                                           59
We know:


    n n
  {a b : n  0}   is context-free




                                    60
We also know:



     L1  {a100 100
                    b   }       is regular




 L1  {(a  b) }  {a
                *       100 100
                            b      }    is regular



                                                     61
    n n
 {a b }               L1  {(a  b) }  {a
                                   *     100 100
                                               b        }
context-free                       regular




(regular closure)   {a b }  L1
                      n n
                                   context-free



    n n               n n
 {a b }  L1  {a b : n  100, n  0}  L
                             is context-free
                                                   62
Another Application of Regular Closure



  Prove that:   L  {w : na  nb  nc }
                   is not context-free




                                          63
Proof?




         64
If   L  {w : na  nb  nc } is context-free

          (regular closure)

 Then    L  {a * b * c*}  {a b c }
                              n n n



context-free       regular     context-free
                               Impossible!!!

 Therefore,    L is not context free
                                               65
 Decidable Properties
          of
Context-Free Languages




                         66
Membership Question:

     for context-free grammar   G
     find if string w L (G )




                                    67
Membership Question:

     for context-free grammar      G
     find if string w L (G )



Membership Algorithms: Parsers

      • Exhaustive search parser

      • CYK parsing algorithm

                                       68
Empty Language Question:

     for context-free grammar   G
     find if L (G )  




                                    69
Empty Language Question:

     for context-free grammar      G
     find if L (G )  



Algorithm:

    1. Remove useless variables

    2. Check if start variable   S is useless
                                                70
Infinite Language Question:
        for context-free grammar     G
        find if L (G ) is infinite




                                         71
Infinite Language Question:
        for context-free grammar     G
        find if L (G ) is infinite

Algorithm:
 1. Remove useless variables
 2. Remove unit and      productions

 3. Create dependency graph for variables

  4. If there is a loop in the dependency graph
     then the language is infinite
                                             72
Example:   S  AB
           A  aCb | a
           B  bB | bb
           C  cBS
Dependency graph         Infinite language


                     A          C
           S
                     B
                                             73
            S  AB
            A  aCb | a
            B  bB | bb
            C  cBS

S  AB  aCbB  acBSbB  acbbSbbb
                  
   S  acbbSbbb(acbb) S (bbb)
                           2     2

    
    (acbb) S (bbb)
              i        i
                                     74

								
To top