0% found this document useful (0 votes)
50 views19 pages

Patterns, Regular Expressions and Finite Automata: (Include Lecture 7,8,9)

The document discusses converting between patterns, regular expressions, and finite automata (FAs). It shows that any language represented by a pattern is regular and can be recognized by an FA. It also demonstrates converting an FA to an equivalent regular expression in three steps: 1) merging edges with the same label, 2) removing intermediate states and replacing them with edge labels, and 3) reducing to a single start/end state FA and reading off the regular expression. An example converts an FA to a regular expression using this process.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views19 pages

Patterns, Regular Expressions and Finite Automata: (Include Lecture 7,8,9)

The document discusses converting between patterns, regular expressions, and finite automata (FAs). It shows that any language represented by a pattern is regular and can be recognized by an FA. It also demonstrates converting an FA to an equivalent regular expression in three steps: 1) merging edges with the same label, 2) removing intermediate states and replacing them with edge labels, and 3) reducing to a single start/end state FA and reading off the regular expression. An example converts an FA to a regular expression using this process.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Formal Language and Automata Theory

Chapter 4
Patterns, Regular Expressions and Finite Automata
(include lecture 7,8,9)

Transparency No. 4-1 Transparency No. 4-1

Patterns and their defined languages

Patterns, regular expression & FAs

: 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( ).
Transparency No. 4-2

More on patterns

Patterns, regular expression & FAs

We say that a string x matches a pattern iff x L(). Some examples: * 1. = L(@) = L(#*) 2. L(x) = {x} for any x * 3. for any x1,,xn in *, L(x1+x2++xn) = {x1,x2,,xn}. 4. {x | x contains at least 3 as} = L(@a@a@a@} 5. - {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)*

Transparency No. 4-3

More on pattern matching

Patterns, regular expression & FAs

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 {anbn | 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 ? = ~(#+ @) = * ; + = * # = a1 + a2 ++ an if = {a1,.., an} + = ~(~ ~) ; = ~ (~ + ~ ) It can be shown that ~ is redundant.
Transparency No. 4-4

Equivalence of patterns, regular expr. & FAs

Patterns, regular expression & FAs

Recall that regular expressions are those patterns that can be built from: a , , , +, and *. Notational conventions: + means + ( ) + * means + (*) * means (*) Theorem 8: Let A *. Then the followings 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 part: (3) => (2). (2) => (1) to be proved now! (1)=> (3) later.
Transparency No. 4-5

(2) => (1) : Every set represented by a pattern is regular

Patterns, regular expression & FAs

Pf: By induction on the structure of pattern . Basis: is atomic: (by construction!) a 1. = a : 2. = : 3. = : 4. = #: a,b,c, 5. = @ = #* : a,b,c,

Transparency No. 4-6

Patterns, regular expression & FAs

Inductive cases: Let M1 and M2 be any FAs accepting L() and L(), respectively. 6. = : => L() = L(M1 M2) 7. = * : => L() = L(M1*) 8. = + , = ~ or = : By ind. hyp. and are regular. Hence by closure properties of regular languages, is regular, too. 9. = + = * : Similar to case 8.

Transparency No. 4-7

Some examples patterns & their equivalent FAs

Patterns, regular expression & FAs

1. (aaa)* + (aaaaa)*

Transparency No. 4-8

(1)=>(3): Regular languages can be represented by reg.

Patterns, regular expression & FAs 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 D(,) = { a | ( a ) } = {a1,,ak} ( k 0) = the set of symbols by which we can reach from to , then Basic case: X = : 1.1 if : (, = {a1, a2,,ak } = L(a1 + a2++ ak) if k > 0, ,) , = {} = L() if k = 0. 1.2 if =: (, = {a1, a2, ak, }=L(a1 + a2++ ak +) if k > 0, ,) , = {} = L() if k = 0.
Transparency No. 4-9

Patterns, regular expression & FAs

3. For nonempty X, let q be any state in X, then : ,) ,) ,q) (X-{q}(q,q))* X-{q}(q, ,). X(, = X-{q} (, U X-{q}(, , , , , , By Ind.hyp.(why?), there are regular expressions , , , with L( [, , , ] ) = [X-{q} (, X-{q}(, ,), ,q), (X-{q}(q,q)), X-{q}(q, ] ,) , , , , , Hence X(, = L( ) ,) U L() L() , = L( + ) and can be represented as a reg. expr. Finally, L(M) = {x | s --x--> f, s S, f F } = sS, fF Q(s,f), is representable by a regular expression. * L( ),

Transparency No. 4-10

Some examples

Patterns, regular expression & FAs

Example (9.3): M : L(M) = p{p,q,r}(p,p) = p{p,r}(p,p) + p{p,r}(p,q) (p{p,r}(q,q))* p{p,r}(q,p) p{p,r}(p,p) = ? p{p,r}(p,q) = ? p{p,r}(q,q) = ? p{p,r}(q,p) = ?

0 >pF q r {p} {r} {p}

1 {q} {} {q}

Hence L(M) = ?

Transparency No. 4-11

Another approach

Patterns, regular expression & FAs

The previous method easy to prove, easy for computer implementation, but hard for human computation. The strategy of the new method: reduce the number of states in the target FA and encodes path information by regular expressions on the edges. until there is one or two states : one is the start state and one is the final state.

Transparency No. 4-12

Steps

Patterns, regular expression & FAs

0. Assume the machine M has only one start state and one final state. Both may probably be identical. 1. While the exists a third state p that is neither start nor final: 1.1 (Merge edges) For each pair of states (q,r) that has more than 1 edges with labels t1,t2,tn, respectively, than merge these edges by a new one with regular expression t = t1 + t2 + tn. 1.2 (Replace state p by edges; remove state) Let (p1, 1, p), (pn, n, p) where pj != p be the collection of all edges in M with p as the destination state, and (p,1, q1),,(p, m, qm) where qj != p be the collection of , all edges with p as the start state. Now the sate p together with all its connecting edges can be removed and replaced by a set of m x n new edges : { (pi, i t* j, qj) | i in [1,n] and j in [1,m] }. The new machine is equivalent to the old one.
Transparency No. 4-13

Patterns, regular expression & FAs

Merge Edges :

Replace state by Edges 1 1 p1 2 p2 p 2 3 p3

q1

q2

p1 p2

1 1

q1

++
p3

2 1 31 2 2 1 2
q2

Note: {p1,p2,p3} may intersect with {q1,q2}.

3 2
Transparency No. 4-14

Patterns, regular expression & FAs

2. perform 1.1 once again (merge edges) // There are one or two states now 3 Two cases to consider: 3.1 The final machine has only one state, that is both start and final. Then if there is an edge labeled t on the sate, then t* is the result, other the result is . 3.2 The machine has one start state s and one final state f. Let (s, s s, s), (f, f f, f), (s,s f, f) and (f, f f, f) be the collection of all edges in the machine, where (s f) means the regular expression or label on the edge from s to f. The result then is [ (s s) + (s f ) (f f)* (f s) ] * (s f) (f f)*
Transparency No. 4-15

Example

0 >p q rF {p,r} {r} {p,q}

1 {q,r} {p,q,r} {q,r}


p q 1 1

Patterns, regular expression & FAs

1. another representation

r 0,1 0,1

p q r 1 0

0,1 1
Transparency No. 4-16

Merge edges

Patterns, regular expression & FAs

p p q r p p q r 1 0 0 q 1 1 r 0+1 0+1 1 0 0

q 1 1

r 0,1 0,1

0,1 1

0+1 1
Transparency No. 4-17

remove q

Patterns, regular expression & FAs

p p p p q r
0, 11*1 1

q 1 1

r 0+1 0+1

0 1 0
1

q
1 1,

r
0+1, 11* (0+1) 0+1

q r

0+1 1
1

p r

0+1 1, 0, (0+1)1*(0+1) (0+1) 1*1

r 0+1

q
1

0+1

Transparency No. 4-18

Form the final result

Patterns, regular expression & FAs

p >p rF
0+11*1 0+ (0+1) 1*1

r
0+1+11* (0+1) 1+ (0+1)1*(0+1)

Final result : = [ p p + (p r) (r r)* (r p) ]* (p r) (r r) * [ (0+11*1) +(0+1+11*(0+1)) (1+(0+1)1*(0+1))* (0+(0+1)1*1) ]* (0+1+11(0+1)) (1+(0+1)1(0+1))

Transparency No. 4-19

You might also like