fall 2004comp 3351 regular expressions. fall 2004comp 3352 regular expressions regular expressions...

34
Fall 2004 COMP 335 1 Regular Expressions

Post on 22-Dec-2015

241 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 1

Regular Expressions

Page 2: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 2

Regular ExpressionsRegular expressions describe regular languages

Example:

describes the language

*)( cba

,...,,,,,, * bcaabcaabcabca

Page 3: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 3

Recursive Definition

,,

1

1

21

21

*

r

r

rr

rr

Are regular expressions

Primitive regular expressions:

2r1rGiven regular expressions and

Page 4: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 4

Examples

)(* ccbaA regular expression:

baNot a regular expression:

Page 5: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 5

Languages of Regular Expressions : language of regular expression

Example:

rL r

,...,,,,,)( * bcaabcaabcacbaL

Page 6: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 6

Definition

For primitive regular expressions :

aaL

L

L

Page 7: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 7

Definition (continued)

For regular expressions and

1r 2r

2121 rLrLrrL

2121 rLrLrrL

*1*

1 rLrL

11 rLrL

Page 8: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 8

ExampleRegular expression: *aba

*abaL *aLbaL *aLbaL

*aLbLaL *aba

,...,,,, aaaaaaba

,...,,,...,,, baababaaaaaa

Page 9: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 9

Example

Regular expression bbabar *

,...,,,,, bbbbaabbaabbarL

Page 10: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 10

Example

Regular expression bbbaar **

}0,:{ 22 mnbbarL mn

Page 11: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 11

Example

Regular expression** )10(00)10( r

)(rL = {all strings with at least two consecutive 0}

Page 12: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 12

Example

Regular expression )0()011( * r

)(rL = { all strings without two consecutive 0 }

Page 13: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 13

Equivalent Regular Expressions

Definition:

Regular expressions and

are equivalent if

1r 2r

)()( 21 rLrL

Page 14: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 14

Example L= { all strings without

two consecutive 0 }

)0()011( *1 r

)0(1)0()0111( ****2 r

LrLrL )()( 211r 2rand

are equivalentReg. expressions

Page 15: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 15

Regular Expressionsand

Regular Languages

Page 16: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 16

Theorem

LanguagesGenerated byRegular Expressions

RegularLanguages

Page 17: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 17

Theorem - Part 1

r)(rL

1. For any regular expression

the language is regular

LanguagesGenerated byRegular Expressions

RegularLanguages

Page 18: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 18

Theorem - Part 2

LanguagesGenerated byRegular Expressions

RegularLanguages

Lr LrL )(

2. For any regular language , there is

a regular expression with

Page 19: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 19

Proof - Part 1

r)(rL

1. For any regular expression

the language is regular

Proof by induction on the size of r

Page 20: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 20

Induction BasisPrimitive Regular Expressions: ,,

NFAs

)()( 1 LML

)(}{)( 2 LML

)(}{)( 3 aLaML

regularlanguages

a

Page 21: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 21

Inductive Hypothesis

Assume for regular expressions andthat and are regular

languages

1r 2r)( 1rL )( 2rL

Page 22: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 22

Inductive StepWe will prove:

1

*1

21

21

rL

rL

rrL

rrL

are regular Languages.

Page 23: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 23

By definition of regular expressions:

11

*1

*1

2121

2121

rLrL

rLrL

rLrLrrL

rLrLrrL

Page 24: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 24

)( 1rL )( 2rLBy inductive hypothesis we know: and are regular languages

Regular languages are closed under:

*1

21

21

rL

rLrL

rLrL Union

Concatenation

Star

We also know:

Page 25: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 25

Therefore:

** 11

2121

2121

rLrL

rLrLrrL

rLrLrrL

Are regularlanguages

Page 26: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 26

And trivially:

))(( 1rL is a regular language

Page 27: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 27

Proof – Part 2

Lr LrL )(

2. For any regular language there is

a regular expression with

Proof by construction of regular expression

Page 28: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 28

Since is regular, take an NFA that accepts it

LM

LML )(

Single final state

Page 29: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 29

From , construct an equivalentGeneralized Transition Graph in which

transition labels are regular expressions

M

Example:

a

ba,

cM

a

ba

c

Page 30: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 30

Another Example:

ba a

b

b

0q 1q 2q

ba,a

b

b

0q 1q 2q

b

b

Page 31: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 31

Reducing the states:

ba a

b

b

0q 1q 2q

b

0q 2q

babb*

)(* babb

Page 32: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 32

Resulting Regular Expression:

0q 2q

babb*

)(* babb

*)(**)*( bbabbabbr

LMLrL )()(

Page 33: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 33

In GeneralRemoving states:

iq q jqa b

cde

iq jq

dae* bce*dce*

bae*

Page 34: Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language

Fall 2004 COMP 335 34

The final transition graph:

0q fq

1r

2r

3r4r

*2

*1342

*1 )( rrrrrrr

LMLrL )()(

The resulting regular expression: