Docstoc

Languages and Finite Automata

Document Sample
Languages and Finite Automata Powered By Docstoc
					                Closure Properties
                        of
              Context-Free languages




Summer 2004            COMP 335        1
                         Union

               Context-free languages
               are closed under:   Union


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


Summer 2004              COMP 335                2
                        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
Summer 2004                  COMP 335                   3
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
Summer 2004           COMP 335                 4
                      Concatenation

              Context-free languages
              are closed under:      Concatenation



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


Summer 2004                 COMP 335                 5
                          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
Summer 2004                    COMP 335                   6
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

Summer 2004          COMP 335                7
                  Star Operation



         Context-free languages
         are closed under:      Star-operation




L is context free                  *
                                   L is context-free

Summer 2004             COMP 335                  8
                         Example

              Language                 Grammar


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


                     Star Operation


        L  {a b } *
                  n n
                                      S1  SS1 | 
Summer 2004                COMP 335                  9
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 | 

Summer 2004          COMP 335                 10
                Negative Properties
                        of
              Context-Free Languages




Summer 2004            COMP 335        11
                      Intersection


              Context-free languages
              are not closed under:  intersection

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

Summer 2004                COMP 335                 12
                      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
Summer 2004              COMP 335                   13
                      Complement


              Context-free languages
              are not closed under:  complement



L is context free                    L   not necessarily
                                         context-free


Summer 2004               COMP 335                    14
                        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
Summer 2004               COMP 335                   15
                   Intersection
                        of
              Context-free languages
                       and
                Regular Languages



Summer 2004            COMP 335        16
    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

Summer 2004            COMP 335                  17
  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
Summer 2004                COMP 335                    18
              NPDA   M1                       DFA       M2
              a, b  c                              a
     q1                  q2              p1                  p2
          transition                          transition



                          NPDA           M

                     q1, p1 a, b  c q2 , p2
                          transition
Summer 2004                   COMP 335                            19
              NPDA   M1                       DFA   M2
              , b  c
     q1                  q2              p1
          transition



                          NPDA           M

                     q1, p1  , b  c q2 , p1
                          transition
Summer 2004                   COMP 335                   20
              NPDA   M1                    DFA    M2

                q0                               p0
          initial state                    initial state



                          NPDA         M

                            q0 , p0
                          Initial state
Summer 2004                 COMP 335                       21
              NPDA   M1                      DFA   M2

                q1                           p1         p2
          final state                        final states



                          NPDA           M

                     q1, p1              q1, p2
                          final states
Summer 2004                   COMP 335                       22
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


Summer 2004                   COMP 335                         23
                regular
                             *
              L2  {a, c}



              DFA       M2
                 a, c


                  p0


Summer 2004             COMP 335   24
                                    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




Summer 2004                       COMP 335                             25
 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 )
Summer 2004              COMP 335                26
Therefore:
                        M is NPDA




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




                  L1  L2    is context-free

Summer 2004                 COMP 335                     27
               Applications
                    of
              Regular Closure




Summer 2004        COMP 335     28
    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

Summer 2004            COMP 335                     29
        An Application of Regular Closure



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

                  is context-free




Summer 2004           COMP 335               30
 We know:


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




Summer 2004            COMP 335              31
 We also know:



              L1  {a
                    100 100
                            b     }            is regular




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



Summer 2004                     COMP 335                            32
      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
Summer 2004             COMP 335                         33
 Another Application of Regular Closure



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




Summer 2004             COMP 335               34
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
Summer 2004                COMP 335                    35
               Decidable Properties
                        of
              Context-Free Languages




Summer 2004            COMP 335        36
Membership Question:

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



Membership Algorithms: Parsers

              • Exhaustive search parser

              • CYK parsing algorithm

Summer 2004                COMP 335            37
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
Summer 2004                COMP 335                   38
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
Summer 2004            COMP 335               39
Example:      S  AB
              A  aCb | a
              B  bB | bb
              C  cBS
Dependency graph                  Infinite language


                        A                C
              S
                        B
Summer 2004            COMP 335                       40
                  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
Summer 2004                COMP 335           41

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:5/15/2012
language:
pages:41