# Transparency No. 4-1 Formal Language and Automata Theory Chapter 4 Patterns, Regular Expressions and Finite Automata (include lecture 7,8,9) Transparency.

## Presentation on theme: "Transparency No. 4-1 Formal Language and Automata Theory Chapter 4 Patterns, Regular Expressions and Finite Automata (include lecture 7,8,9) Transparency."— Presentation transcript:

Transparency No. 4-1 Formal Language and Automata Theory Chapter 4 Patterns, Regular Expressions and Finite Automata (include lecture 7,8,9) Transparency No. 4-1

Patterns, regular expression & FAs Transparency No. 4-2 Patterns and their defined languages  : a finite alphabet A pattern is a string of symbols representing a set of strings in  *. The set of all patterns is defined inductively as follows: 1. atomic patterns: a  , , , #, @. 2. compound patterns: if  and  are patterns, then so are:  + ,  ,  *,  +, ~  and . For each pattern , L(  ) is the language represented by  and is defined inductively as follows: 1. L(a) = {a}, L(  ) = {  }, L(  )= {}, L(#) = , L(@) =  *. 2. If L(  ) and L(  ) have been defined, then L(  +  ) = L(  ) U L(  ), L(   ) = L(  )  L(  ). L(  + ) = L(  ) +, L(  *) = L(  )*, L(~  ) =  * - L(  ), L(  ) = L(  )  L(  ).

Patterns, regular expression & FAs Transparency No. 4-3 More on patterns We say that a string x matches a pattern  iff x  L(  ). Some examples:  * = L(@) = L(#*) 2. L(x) = {x} for any x   * 3. for any x 1,…,x n in  *, L(x 1 +x 2 +…+x n ) = {x 1,x 2,…,x n }. 4. {x | x contains at least 3 a’s} = L(@a@a@a@}  - {a} = #  ~a 6. {x | x does not contain a} = (#  ~a)* 7. {x | every ‘a’ in x is followed sometime later by a ‘b’ } = = {x | either no ‘a’ in x or  ‘b’ in x followed no ‘a’ } = (#  ~a)* + @b(#  ~a)*

Patterns, regular expression & FAs Transparency No. 4-4 More on pattern matching Some interesting and important questions: 1. How hard is it to determine if a given input string x matches a given pattern a ? ==> efficient algorithm exists 2. Can every set be represented by a pattern ? ==> no! the set {a n b n | n > 0 } cannot be represented by any pattern. 3. How to determine if two given patterns  and  are equivalent ? (I.e., L(  ) = L(  )) --- an exercise ! 4. Which operations are redundant ?  = ~(#  @) =  * ;  + =    * # = a 1 + a 2 +…+ a n if  = {a 1,.., a n }  +  = ~(    ) ;    = ~ (  +  ) It can be shown that ~ is redundant.

Patterns, regular expression & FAs Transparency No. 4-5 Equivalence of patterns, regular expr. & FAs Recall that regular expressions are those patterns that can be built from: a , , , +,  and *. Notational conventions:  +  means  + (  )  +  * means  + (  *) Theorem 8: Let A   *. Then the following are equivalent: 1. A is regular (I.e., A = L(M) for some FA M ), 2. A = L(  ) for some pattern , 3. A = L(  ) for some regular expression . pf: Trivial parts: (3) => (2). (2) => (1) to be proved now! (1)=> (3) later.

Patterns, regular expression & FAs Transparency No. 4-6 (2) => (1) : Every set represented by a pattern is regular Pf: By induction on the structure of pattern . Basis:  is atomic: (by construction!)  = a :  =  :  =  or 4.  = #:  = @ = #* : Inductive cases: Let M 1 and M 2 be any FAs accepting L(  ) and L(  ), respectively.  =   : => L(  ) = L(M 1  M 2 )  =  * : => L(  ) = L(M 1 *)  =  +  = ~  or  =    : By ind. hyp.  and  are regular. Hence by closure properties of regular languages,  is regular, too.  =  + =   * : Similar to case 8.

Patterns, regular expression & FAs Transparency No. 4-7 Some examples patterns & their equivalent FAs 1. (aaa)* + (aaaaa)*

Patterns, regular expression & FAs Transparency No. 4-8 (1)=>(3): Regular languages can be represented by reg. expr. M = (Q, , , S, F) : a NFA; X  Q: a set of states;   Q : two states  X (  ) = def {y   * |  a path from  to labeled y and all intermediate states  X }. Note: L(M) = ?  X (  ) can be shown to be representable by a regular expr. by induction as follows: Let { a |  ,a) } = {a 1,…,a k } ( k  0), then 1. if   :   (  ) = {a 1, a 2,…,a k }= L(a 1 + a 2 +…+ a k ) if k > 0, = {} = L(  ) if k = 0. 2. if  = :   (  ) = {a 1, a 2,… a k,  }=L(a 1 + a 2 +…+ a k +  ) if k > 0, = {  } = L(  ) if k = 0. 3. For nonempty X, let q be any state in X, then,  X (  ) =  X-{q} (  ) U  X-{q} (  q) (  X-{q} (q  q))*  X-{q} (q  ) = L(  +  ), where by ind. hyp., assume L( (  )) = (  X-{q} (  ),  X-{q} (  q), (  X-{q} (q  q)),  X-{q} (q  ) )

Patterns, regular expression & FAs Transparency No. 4-9 Finally, L(M) = {x | s --x--> f, s  S, f  F } =  s  S, f  F  Q (s,f), is representable by a regular expression. Example (9.3): M : Some examples L(M) =  {p,q,r} (p,p) =  {p,r} (p,p) +  {p,r} (p,q) (  {p,r} (q,q))*  {p,q,r} (q,p)  {p,r} (p,p) = ?  {p,r} (p,q) = ?  {p,r} (q,q) = ?  {p,r} (q,p) = ? Hence L(M) = ? 

Similar presentations