Regular Expressions (REs) Regular Expressions (REs) p.1/37 - - PowerPoint PPT Presentation

regular expressions res
SMART_READER_LITE
LIVE PREVIEW

Regular Expressions (REs) Regular Expressions (REs) p.1/37 - - PowerPoint PPT Presentation

Regular Expressions (REs) Regular Expressions (REs) p.1/37 Expressions In arithmetic: Regular Expressions (REs) p.2/37 Expressions In arithmetic: 1. expressions are constructed from numbers and variables using arithmetic


slide-1
SLIDE 1

Regular Expressions (REs)

Regular Expressions (REs) – p.1/37

slide-2
SLIDE 2

Expressions

  • In arithmetic:

Regular Expressions (REs) – p.2/37

slide-3
SLIDE 3

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

Regular Expressions (REs) – p.2/37

slide-4
SLIDE 4

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

  • 2. expressions represent computations with numbers

Regular Expressions (REs) – p.2/37

slide-5
SLIDE 5

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

  • 2. expressions represent computations with numbers
  • 3. results of expressions evaluation are numbers

Regular Expressions (REs) – p.2/37

slide-6
SLIDE 6

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

  • 2. expressions represent computations with numbers
  • 3. results of expressions evaluation are numbers
  • In automata theory:

Regular Expressions (REs) – p.2/37

slide-7
SLIDE 7

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

  • 2. expressions represent computations with numbers
  • 3. results of expressions evaluation are numbers
  • In automata theory:
  • 1. regular expressions are constructed from regular languages

using regular operations and parentheses

Regular Expressions (REs) – p.2/37

slide-8
SLIDE 8

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

  • 2. expressions represent computations with numbers
  • 3. results of expressions evaluation are numbers
  • In automata theory:
  • 1. regular expressions are constructed from regular languages

using regular operations and parentheses

  • 2. regular expressions represent computations with regular

languages

Regular Expressions (REs) – p.2/37

slide-9
SLIDE 9

Expressions

  • In arithmetic:
  • 1. expressions are constructed from numbers and variables

using arithmetic operations and parantheses

  • 2. expressions represent computations with numbers
  • 3. results of expressions evaluation are numbers
  • In automata theory:
  • 1. regular expressions are constructed from regular languages

using regular operations and parentheses

  • 2. regular expressions represent computations with regular

languages

  • 3. result of regular expressions evaluation are regular languages

Regular Expressions (REs) – p.2/37

slide-10
SLIDE 10

Example

✂ ✄ ☎ ✆ ✝

is an arithmetic expression constructed with

  • perations

and

; its value is the number 32

Regular Expressions (REs) – p.3/37

slide-11
SLIDE 11

Example

✂ ✄ ☎ ✆ ✝

is an arithmetic expression constructed with

  • perations

and

; its value is the number 32

✂ ☎

is a regular expression constructed from the languages

  • and

using regular operations;

Regular Expressions (REs) – p.3/37

slide-12
SLIDE 12

Example

✂ ✄ ☎ ✆ ✝

is an arithmetic expression constructed with

  • perations

and

; its value is the number 32

✂ ☎

is a regular expression constructed from the languages

  • and

using regular operations;

  • it evaluates to the language that contains all strings of

zero-s and 1 followed by zero-s

Regular Expressions (REs) – p.3/37

slide-13
SLIDE 13

Regular expression evaluation

Similar to the evaluation of arithmetic expression

Regular Expressions (REs) – p.4/37

slide-14
SLIDE 14

Regular expression evaluation

Similar to the evaluation of arithmetic expression

  • Identify first the constants (languages) involved.

Regular Expressions (REs) – p.4/37

slide-15
SLIDE 15

Regular expression evaluation

Similar to the evaluation of arithmetic expression

  • Identify first the constants (languages) involved.
  • Example:
  • represents the language

and

represents the language

✁ ✄ ✂

Regular Expressions (REs) – p.4/37

slide-16
SLIDE 16

Regular expression evaluation

Similar to the evaluation of arithmetic expression

  • Identify first the constants (languages) involved.
  • Example:
  • represents the language

and

represents the language

✁ ✄ ✂
  • Apply the regular operations on the languages thus identified

following their set-theory definitions.

Regular Expressions (REs) – p.4/37

slide-17
SLIDE 17

Regular expression evaluation

Similar to the evaluation of arithmetic expression

  • Identify first the constants (languages) involved.
  • Example:
  • represents the language

and

represents the language

✁ ✄ ✂
  • Apply the regular operations on the languages thus identified

following their set-theory definitions.

  • Example:
✄ ✂

represents the language

✁ ☎✄ ✄ ✂

,

represents the language

✁✞✝ ✄
✟ ✟ ✟ ✂

and

✄ ✂

represents the language

✄ ✄
✄ ☎✄
  • ☎✄
  • ☎✄
✟ ✟ ✟ ✂

Regular Expressions (REs) – p.4/37

slide-18
SLIDE 18

Regular expression evaluation

Similar to the evaluation of arithmetic expression

  • Identify first the constants (languages) involved.
  • Example:
  • represents the language

and

represents the language

✁ ✄ ✂
  • Apply the regular operations on the languages thus identified

following their set-theory definitions.

  • Example:
✄ ✂

represents the language

✁ ☎✄ ✄ ✂

,

represents the language

✁✞✝ ✄
✟ ✟ ✟ ✂

and

✄ ✂

represents the language

✄ ✄
✄ ☎✄
  • ☎✄
  • ☎✄
✟ ✟ ✟ ✂

Note: by convention, the symbol

  • f concatenation op.

is not written in regular expressions

Regular Expressions (REs) – p.4/37

slide-19
SLIDE 19

Applications

  • 1. Regular expressions provide a powerful method to

describe patterns searched in various texts

Regular Expressions (REs) – p.5/37

slide-20
SLIDE 20

Applications

  • 1. Regular expressions provide a powerful method to

describe patterns searched in various texts

  • 2. Utilities such as AWK, GREP in Unix, modern

programming languages, such as PERL, text editors, all use regular expressions for their pattern description

Regular Expressions (REs) – p.5/37

slide-21
SLIDE 21

Applications

  • 1. Regular expressions provide a powerful method to

describe patterns searched in various texts

  • 2. Utilities such as AWK, GREP in Unix, modern

programming languages, such as PERL, text editors, all use regular expressions for their pattern description

  • 3. The lexicon of programming languages is specifi ed by

regular expressions. Hence, lexical analysis is done using regular expressions.

Regular Expressions (REs) – p.5/37

slide-22
SLIDE 22

Applications

  • 1. Regular expressions provide a powerful method to

describe patterns searched in various texts

  • 2. Utilities such as AWK, GREP in Unix, modern

programming languages, such as PERL, text editors, all use regular expressions for their pattern description

  • 3. The lexicon of programming languages is specifi ed by

regular expressions. Hence, lexical analysis is done using regular expressions.

  • 4. Other examples?

Regular Expressions (REs) – p.5/37

slide-23
SLIDE 23

For a given alphabet

  • The regular expression
  • describes the language

consisting of all strings of length 1 over

  • Regular Expressions (REs) – p.6/37
slide-24
SLIDE 24

For a given alphabet

  • The regular expression
  • describes the language

consisting of all strings of length 1 over

describes the language of all strings over the alphabet

  • Regular Expressions (REs) – p.6/37
slide-25
SLIDE 25

For a given alphabet

  • The regular expression
  • describes the language

consisting of all strings of length 1 over

describes the language of all strings over the alphabet

is the language of all strings over

  • that ends in

Regular Expressions (REs) – p.6/37

slide-26
SLIDE 26

For a given alphabet

  • The regular expression
  • describes the language

consisting of all strings of length 1 over

describes the language of all strings over the alphabet

is the language of all strings over

  • that ends in
  • Language
☎ ✁
✂ ☎

describes the language that consists of all strings that either start with 0 or end in 1

Regular Expressions (REs) – p.6/37

slide-27
SLIDE 27

Precedence relation

Denoting

  • the rule describing the order of ops in the

evaluation of arithmetic and regular expressions we have:

Regular Expressions (REs) – p.7/37

slide-28
SLIDE 28

Precedence relation

Denoting

  • the rule describing the order of ops in the

evaluation of arithmetic and regular expressions we have:

  • Arithmetic expressions:
  • ✁✂✁
✄ ✄ ✂
☎ ✄ ✆ ✂

Regular Expressions (REs) – p.7/37

slide-29
SLIDE 29

Precedence relation

Denoting

  • the rule describing the order of ops in the

evaluation of arithmetic and regular expressions we have:

  • Arithmetic expressions:
  • ✁✂✁
✄ ✄ ✂
☎ ✄ ✆ ✂
  • Regular expressions:
  • ✁✁
✁ ✄ ✂ ✂

Regular Expressions (REs) – p.7/37

slide-30
SLIDE 30

Precedence relation

Denoting

  • the rule describing the order of ops in the

evaluation of arithmetic and regular expressions we have:

  • Arithmetic expressions:
  • ✁✂✁
✄ ✄ ✂
☎ ✄ ✆ ✂
  • Regular expressions:
  • ✁✁
✁ ✄ ✂ ✂

Note:

denotes expressions enclosed in parentheses

Regular Expressions (REs) – p.7/37

slide-31
SLIDE 31

Formal definitions

Consider

  • an alphabet. A regular expression (RE)
  • ver
  • is defi ned recursivelyby the rules:

Regular Expressions (REs) – p.8/37

slide-32
SLIDE 32

Formal definitions

Consider

  • an alphabet. A regular expression (RE)
  • ver
  • is defi ned recursivelyby the rules:

Recursion basis:

Regular Expressions (REs) – p.8/37

slide-33
SLIDE 33

Formal definitions

Consider

  • an alphabet. A regular expression (RE)
  • ver
  • is defi ned recursivelyby the rules:

Recursion basis:

  • 1. For any

,

  • is a RE describing the language
☎ ✁

Regular Expressions (REs) – p.8/37

slide-34
SLIDE 34

Formal definitions

Consider

  • an alphabet. A regular expression (RE)
  • ver
  • is defi ned recursivelyby the rules:

Recursion basis:

  • 1. For any

,

  • is a RE describing the language
☎ ✁

2.

is a RE describing the language

✂ ☎ ✁ ✝ ✂

Regular Expressions (REs) – p.8/37

slide-35
SLIDE 35

Formal definitions

Consider

  • an alphabet. A regular expression (RE)
  • ver
  • is defi ned recursivelyby the rules:

Recursion basis:

  • 1. For any

,

  • is a RE describing the language
☎ ✁

2.

is a RE describing the language

✂ ☎ ✁ ✝ ✂

3.

  • is a RE describing the empty language
  • .

Regular Expressions (REs) – p.8/37

slide-36
SLIDE 36

Formal definitions, continutation

Recursion body: If

and

are REs evaluating to the languages

and

then:

Regular Expressions (REs) – p.9/37

slide-37
SLIDE 37

Formal definitions, continutation

Recursion body: If

and

are REs evaluating to the languages

and

then:

1.

is a RE evaluating to

✂ ✁ ✄

Regular Expressions (REs) – p.9/37

slide-38
SLIDE 38

Formal definitions, continutation

Recursion body: If

and

are REs evaluating to the languages

and

then:

1.

is a RE evaluating to

✂ ✁ ✄

2.

is a RE evaluating to

✂ ✂ ✄

Regular Expressions (REs) – p.9/37

slide-39
SLIDE 39

Formal definitions, continutation

Recursion body: If

and

are REs evaluating to the languages

and

then:

1.

is a RE evaluating to

✂ ✁ ✄

2.

is a RE evaluating to

✂ ✂ ✄

3.

is a RE evaluating to

✁✁ ✂ ✄ ✄
  • where
✁ ✂ ✄

and

✂ ✄ ☎ ✝

.

Regular Expressions (REs) – p.9/37

slide-40
SLIDE 40

Note

A defi nition of the form expressed by rule (4) above is called an inductive defi nition

Regular Expressions (REs) – p.10/37

slide-41
SLIDE 41

Potential confusions

  • Do not confuse the REs
  • and

;

Regular Expressions (REs) – p.11/37

slide-42
SLIDE 42

Potential confusions

  • Do not confuse the REs
  • and

;

1.

represents the language containing just the empty string

Regular Expressions (REs) – p.11/37

slide-43
SLIDE 43

Potential confusions

  • Do not confuse the REs
  • and

;

1.

represents the language containing just the empty string 2.

  • is the RE that represents the the empty language

Regular Expressions (REs) – p.11/37

slide-44
SLIDE 44

Potential confusions

  • Do not confuse the REs
  • and

;

1.

represents the language containing just the empty string 2.

  • is the RE that represents the the empty language
  • Note the distinction between
  • and

.

  • is an

expression and

is the set of strings specifi ed by

  • Regular Expressions (REs) – p.11/37
slide-45
SLIDE 45

Example REs

1.

,

Regular Expressions (REs) – p.12/37

slide-46
SLIDE 46

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

Regular Expressions (REs) – p.12/37

slide-47
SLIDE 47

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

Regular Expressions (REs) – p.12/37

slide-48
SLIDE 48

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

Regular Expressions (REs) – p.12/37

slide-49
SLIDE 49

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

Regular Expressions (REs) – p.12/37

slide-50
SLIDE 50

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

Regular Expressions (REs) – p.12/37

slide-51
SLIDE 51

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

Regular Expressions (REs) – p.12/37

slide-52
SLIDE 52

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

Regular Expressions (REs) – p.12/37

slide-53
SLIDE 53

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

Regular Expressions (REs) – p.12/37

slide-54
SLIDE 54

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

Regular Expressions (REs) – p.12/37

slide-55
SLIDE 55

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

Regular Expressions (REs) – p.12/37

slide-56
SLIDE 56

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

Regular Expressions (REs) – p.12/37

slide-57
SLIDE 57

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

7.

✄ ✂ ✆ ✄ ✁

,

Regular Expressions (REs) – p.12/37

slide-58
SLIDE 58

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

7.

✄ ✂ ✆ ✄ ✁

,

✄ ✂ ✆ ✄ ✁
✄ ✂ ☎ ✁
  • starts and

ends with the same symbol

Regular Expressions (REs) – p.12/37

slide-59
SLIDE 59

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

7.

✄ ✂ ✆ ✄ ✁

,

✄ ✂ ✆ ✄ ✁
✄ ✂ ☎ ✁
  • starts and

ends with the same symbol

8.

✝ ✂ ✄ ✆

,

Regular Expressions (REs) – p.12/37

slide-60
SLIDE 60

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

7.

✄ ✂ ✆ ✄ ✁

,

✄ ✂ ✆ ✄ ✁
✄ ✂ ☎ ✁
  • starts and

ends with the same symbol

8.

✝ ✂ ✄ ✆

,

✝ ✂ ✄ ✆ ✂ ☎ ✁
✆ ✁ ✄ ✆ ✂

Regular Expressions (REs) – p.12/37

slide-61
SLIDE 61

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

7.

✄ ✂ ✆ ✄ ✁

,

✄ ✂ ✆ ✄ ✁
✄ ✂ ☎ ✁
  • starts and

ends with the same symbol

8.

✝ ✂ ✄ ✆

,

✝ ✂ ✄ ✆ ✂ ☎ ✁
✆ ✁ ✄ ✆ ✂

9.

✝ ✂
✁ ✝ ✂

,

Regular Expressions (REs) – p.12/37

slide-62
SLIDE 62

Example REs

1.

,

✂ ☎ ✁
  • contains exactly a single 1

2.

✂ ✆ ✄ ✂ ✆

,

✆ ✄ ✂ ✆ ✂ ☎ ✁
  • contains at least one 1

3.

✂ ✆
✂ ✆

,

✂ ✆ ✂ ☎ ✁
  • contains

as a substring

4.

✂ ✂ ✆

,

✂ ✂ ✆ ☎ ✁
  • is a string of even length

5.

✂ ✂ ✂ ✆

,

✂ ✂ ✂ ✆ ☎ ✁

the length of

  • is a multiple of three

6.

,

✂ ☎ ✁

7.

✄ ✂ ✆ ✄ ✁

,

✄ ✂ ✆ ✄ ✁
✄ ✂ ☎ ✁
  • starts and

ends with the same symbol

8.

✝ ✂ ✄ ✆

,

✝ ✂ ✄ ✆ ✂ ☎ ✁
✆ ✁ ✄ ✆ ✂

9.

✝ ✂
✁ ✝ ✂

,

✝ ✂
✁ ✝ ✂ ✂ ☎ ✁ ✝ ✄ ☎✄ ✄ ✄

Regular Expressions (REs) – p.12/37

slide-63
SLIDE 63

More example

10.

✂ ✄ ✁

,

✄ ✁ ☎

; Note: concatenating

to any set yields

Regular Expressions (REs) – p.13/37

slide-64
SLIDE 64

More example

10.

✂ ✄ ✁

,

✄ ✁ ☎

; Note: concatenating

to any set yields

11.

✁ ✄

,

✄ ☎

; by defi nition,

  • is in the star operation applied on any language;

if the language is empty

  • becomes the one element

Regular Expressions (REs) – p.13/37

slide-65
SLIDE 65

Identities

If

  • is a RE then the following identities take place:

Regular Expressions (REs) – p.14/37

slide-66
SLIDE 66

Identities

If

  • is a RE then the following identities take place:
✁ ☎

; adding the empty language to any

  • ther language will not change that language

Regular Expressions (REs) – p.14/37

slide-67
SLIDE 67

Identities

If

  • is a RE then the following identities take place:
✁ ☎

; adding the empty language to any

  • ther language will not change that language

; concatenating any string with the empty string does not change that string

Regular Expressions (REs) – p.14/37

slide-68
SLIDE 68

Note

. Example: if

  • then
  • ;

Regular Expressions (REs) – p.15/37

slide-69
SLIDE 69

Note

. Example: if

  • then
  • ;

. Example: if

  • then

but

Regular Expressions (REs) – p.15/37

slide-70
SLIDE 70

Application

  • REs are useful tools for the design of compilers

Regular Expressions (REs) – p.16/37

slide-71
SLIDE 71

Application

  • REs are useful tools for the design of compilers
  • Language lexicon is described by REs.

Regular Expressions (REs) – p.16/37

slide-72
SLIDE 72

Application

  • REs are useful tools for the design of compilers
  • Language lexicon is described by REs.

Example: numerical constants can be described by:

✁ ☎ ✄ ✆ ✄ ✝ ✂

where

✄ ✄ ✁☎✄ ✂☎✄ ✄ ✄ ☎☎✄ ✆ ✄ ✝ ✄ ✞☎✄ ✟ ✂

Regular Expressions (REs) – p.16/37

slide-73
SLIDE 73

Application

  • REs are useful tools for the design of compilers
  • Language lexicon is described by REs.

Example: numerical constants can be described by:

✁ ☎ ✄ ✆ ✄ ✝ ✂

where

✄ ✄ ✁☎✄ ✂☎✄ ✄ ✄ ☎☎✄ ✆ ✄ ✝ ✄ ✞☎✄ ✟ ✂
  • From the lexicon description by REs one can generate

automatically lexical analyzers

Regular Expressions (REs) – p.16/37

slide-74
SLIDE 74

New terminology

A RE

  • that evaluates to the language

is further said that specifi es the language

Regular Expressions (REs) – p.17/37

slide-75
SLIDE 75

Equivalence with FA

  • REs and fi nite automata are equivalentin their

descriptive power

Regular Expressions (REs) – p.18/37

slide-76
SLIDE 76

Equivalence with FA

  • REs and fi nite automata are equivalentin their

descriptive power

  • Any RE
  • can be converted into a fi nite automaton,
✁✄✂

, that recognizes the language specifi ed by

  • Regular Expressions (REs) – p.18/37
slide-77
SLIDE 77

Equivalence with FA

  • REs and fi nite automata are equivalentin their

descriptive power

  • Any RE
  • can be converted into a fi nite automaton,
✁✄✂

, that recognizes the language specifi ed by

  • Vice-versa, any fi nite automaton recognizing a

language

can be converted into a RE

that specifi es the language

Regular Expressions (REs) – p.18/37

slide-78
SLIDE 78

Theorem 1.54

Language is regular iff some RE specifi es it

Regular Expressions (REs) – p.19/37

slide-79
SLIDE 79

Theorem 1.54

Language is regular iff some RE specifi es it Proof idea: this proof has two parts:

Regular Expressions (REs) – p.19/37

slide-80
SLIDE 80

Theorem 1.54

Language is regular iff some RE specifi es it Proof idea: this proof has two parts:

  • First part: we show that a language specified by a RE is regular,

i.e., there is a finite automaton that recognizes it.

Regular Expressions (REs) – p.19/37

slide-81
SLIDE 81

Theorem 1.54

Language is regular iff some RE specifi es it Proof idea: this proof has two parts:

  • First part: we show that a language specified by a RE is regular,

i.e., there is a finite automaton that recognizes it.

  • Second part: we show that if a language is regular then there is a

RE that specifies it

Regular Expressions (REs) – p.19/37

slide-82
SLIDE 82

Lemma 1.55

If a language is specifi ed by a RE, then it is regular.

Regular Expressions (REs) – p.20/37

slide-83
SLIDE 83

Lemma 1.55

If a language is specifi ed by a RE, then it is regular. Proof idea: Assume that we have a RE

  • that evaluate to

the language

.

Regular Expressions (REs) – p.20/37

slide-84
SLIDE 84

Lemma 1.55

If a language is specifi ed by a RE, then it is regular. Proof idea: Assume that we have a RE

  • that evaluate to

the language

.

  • 1. We will show how to convert
  • into an NFA that recognizes
  • .

Regular Expressions (REs) – p.20/37

slide-85
SLIDE 85

Lemma 1.55

If a language is specifi ed by a RE, then it is regular. Proof idea: Assume that we have a RE

  • that evaluate to

the language

.

  • 1. We will show how to convert
  • into an NFA that recognizes
  • .
  • 2. Then by corollary 1.40, if an NFA recognizes
  • then
  • is regular

Regular Expressions (REs) – p.20/37

slide-86
SLIDE 86

Proof

Convert

  • into an NFA
  • by the following six-steps proce-

dure:

Regular Expressions (REs) – p.21/37

slide-87
SLIDE 87

Step 1:

If

  • , then

and the NFA

  • recognizing

is in Figure 1

✂ ✄ ✁ ✂

Figure 1: NFA

recognizing

Regular Expressions (REs) – p.22/37

slide-88
SLIDE 88

Step 1:

If

  • , then

and the NFA

  • recognizing

is in Figure 1

✂ ✄ ✁ ✂

Figure 1: NFA

recognizing

Note: this is an NFA but not a DFA because it has states with no exiting arrow for each possible input symbol

Regular Expressions (REs) – p.22/37

slide-89
SLIDE 89

Formal construction

Formally

✁ ✁
✂ ✁
✁ ☎

where:

,

✂ ✁✄ ✁ ☎ ☎

, for

  • and
  • Regular Expressions (REs) – p.23/37
slide-90
SLIDE 90

Step 2:

If

  • then

and the NFA

  • that recognizes

is in Figure 2

Figure 2: The NFA

recognizing

✁ ✝ ✂

Regular Expressions (REs) – p.24/37

slide-91
SLIDE 91

Formal construction

Formally

✂ ✁

where:

✂ ✁✄ ✁ ☎ ☎

for any

and

☎ ✁
  • Regular Expressions (REs) – p.25/37
slide-92
SLIDE 92

Step 3:

If

then

, and the NFA

  • that recognizes

is in Figure 3

Figure 3: he NFA

recognizing

  • Regular Expressions (REs) – p.26/37
slide-93
SLIDE 93

Formal construction

Formally

✁ ✁
✂ ✁
✁ ☎

where:

✂ ✁✄ ✁ ☎ ☎

for any

and

Regular Expressions (REs) – p.27/37

slide-94
SLIDE 94

Step 4:

If

then

☎ ✁ ✄

.

Regular Expressions (REs) – p.28/37

slide-95
SLIDE 95

Step 4:

If

then

☎ ✁ ✄

.

Note: in view with the inductive nature of

  • we may assume that:

Regular Expressions (REs) – p.28/37

slide-96
SLIDE 96

Step 4:

If

then

☎ ✁ ✄

.

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

Regular Expressions (REs) – p.28/37

slide-97
SLIDE 97

Step 4:

If

then

☎ ✁ ✄

.

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

2.

☎ ✂ ☎
✄ ✂ ✄ ✁ ✂ ✄ ✄ ✂ ✄ ✂ ✂ ✂

is an NFA recognizing

Regular Expressions (REs) – p.28/37

slide-98
SLIDE 98

Step 4:

If

then

☎ ✁ ✄

.

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

2.

☎ ✂ ☎
✄ ✂ ✄ ✁ ✂ ✄ ✄ ✂ ✄ ✂ ✂ ✂

is an NFA recognizing

The NFA

  • recognizing

is given in Figure 4

Regular Expressions (REs) – p.28/37

slide-99
SLIDE 99

NFA recognizing

✂ ✁ ✂ ✄ ✄ ✄ ✂✆☎ ✂ ✄ ✄ ✄ ✂ ✂ ✂ ✂ ✝ ✝ ✄ ✄ ✄ ✄ ✄ ✄

Figure 4: Construction of

to recognize

Regular Expressions (REs) – p.29/37

slide-100
SLIDE 100

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

: That is, the states of

are all states on

☎ ✁

and

☎ ✂

with the addition of a new state

✄ ✄

Regular Expressions (REs) – p.30/37

slide-101
SLIDE 101

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

: That is, the states of

are all states on

☎ ✁

and

☎ ✂

with the addition of a new state

✄ ✄
  • 2. The start state of

is

✄ ✄

Regular Expressions (REs) – p.30/37

slide-102
SLIDE 102

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

: That is, the states of

are all states on

☎ ✁

and

☎ ✂

with the addition of a new state

✄ ✄
  • 2. The start state of

is

✄ ✄
  • 3. The accept states of

are

✂ ☎ ✂ ✁ ✁ ✂ ✂

: That is, the accept states

  • f

are all the accept states of

☎ ✁

and

☎ ✂

Regular Expressions (REs) – p.30/37

slide-103
SLIDE 103

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

: That is, the states of

are all states on

☎ ✁

and

☎ ✂

with the addition of a new state

✄ ✄
  • 2. The start state of

is

✄ ✄
  • 3. The accept states of

are

✂ ☎ ✂ ✁ ✁ ✂ ✂

: That is, the accept states

  • f

are all the accept states of

☎ ✁

and

☎ ✂
  • 4. Define

so that for any

✄ ✁
  • and any
✂✁

:

☎ ✂ ✄☎✄✆✄✝✄☎✄✆✞ ✄☎✄✝✄✆✄☎✄✝✟ ✁ ✁

if

✄ ✁
✁ ✂

if

✄ ✁
✁ ✄ ✁ ✄ ✄ ✂ ✂ ✄

if

✄ ☎ ✄ ✄

and

if

✄ ☎ ✄ ✄

and

☎ ✝

.

Regular Expressions (REs) – p.30/37

slide-104
SLIDE 104

Step 5:

If

then

.

Regular Expressions (REs) – p.31/37

slide-105
SLIDE 105

Step 5:

If

then

.

Note: in view with the inductive nature of

  • we may assume that:

Regular Expressions (REs) – p.31/37

slide-106
SLIDE 106

Step 5:

If

then

.

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

Regular Expressions (REs) – p.31/37

slide-107
SLIDE 107

Step 5:

If

then

.

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

2.

☎ ✂ ☎
✄ ✂ ✄ ✁ ✂ ✄ ✄ ✂ ✄ ✂ ✂ ✂

is an NFA recognizing

.

Regular Expressions (REs) – p.31/37

slide-108
SLIDE 108

NFA recognizing

✂ ✁ ✂ ✄ ✄ ✄ ✂ ☎ ✂ ✄ ✄ ✄ ✄ ✂ ✄ ✄ ✄ ✄ ✄ ✄ ✄ ✁ ✁ ✁ ✁ ✂ ✝ ✂ ✝ ✄ ✄ ✄ ✄ ☎ ✝ ✂

Figure 5: Construction of

to recognize

Regular Expressions (REs) – p.32/37

slide-109
SLIDE 109

Construction procedure

1.

. The states of

are all states of

☎ ✁

and

☎ ✂

Regular Expressions (REs) – p.33/37

slide-110
SLIDE 110

Construction procedure

1.

. The states of

are all states of

☎ ✁

and

☎ ✂
  • 2. The start state is the state
✄ ✁
  • f
☎ ✁

Regular Expressions (REs) – p.33/37

slide-111
SLIDE 111

Construction procedure

1.

. The states of

are all states of

☎ ✁

and

☎ ✂
  • 2. The start state is the state
✄ ✁
  • f
☎ ✁
  • 3. The accept states is the set
✂ ✂
  • f the accept states of
☎ ✂

Regular Expressions (REs) – p.33/37

slide-112
SLIDE 112

Construction procedure

1.

. The states of

are all states of

☎ ✁

and

☎ ✂
  • 2. The start state is the state
✄ ✁
  • f
☎ ✁
  • 3. The accept states is the set
✂ ✂
  • f the accept states of
☎ ✂
  • 4. Define

so that for any

✄ ✁
  • and any
  • :
☎ ✂ ✄✝✄✆✄☎✄✝✄✆✞ ✄☎✄✝✄✆✄☎✄✝✟ ✁ ✁

if

✄ ✁

and

✄ ✠ ✁ ✂ ✁ ✁ ✁

if

✄ ✁ ✂ ✁

and

☎ ✝ ✁ ✁
✁ ✁ ✄ ✂ ✂ ✄

if

✄ ✁ ✂ ✁

and

✝ ✁ ✂

if

✄ ✁

.

Regular Expressions (REs) – p.33/37

slide-113
SLIDE 113

Step 6:

If

  • then
  • ✁✁
✂✄ ✄
  • .

Regular Expressions (REs) – p.34/37

slide-114
SLIDE 114

Step 6:

If

  • then
  • ✁✁
✂✄ ✄
  • .

Note: in view with the inductive nature of

  • we may assume that:

Regular Expressions (REs) – p.34/37

slide-115
SLIDE 115

Step 6:

If

  • then
  • ✁✁
✂✄ ✄
  • .

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

Regular Expressions (REs) – p.34/37

slide-116
SLIDE 116

Step 6:

If

  • then
  • ✁✁
✂✄ ✄
  • .

Note: in view with the inductive nature of

  • we may assume that:

1.

☎ ✁ ☎
✄ ✂ ✄ ✁ ✁ ✄ ✄ ✁ ✄ ✂ ✁ ✂

is an NFA recognizing

The NFA

  • recognizing

is given in Figure 6

Regular Expressions (REs) – p.34/37

slide-117
SLIDE 117

NFA recognizing

✁ ✂ ✄ ✄ ✂ ✂ ✄ ✄ ✂ ✝ ✁ ✝ ✂ ✝

Figure 6: Construction of

  • to recognize

Regular Expressions (REs) – p.35/37

slide-118
SLIDE 118

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

; that is, states of

are the states of

☎ ✁

plus a new state

✄ ✄

Regular Expressions (REs) – p.36/37

slide-119
SLIDE 119

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

; that is, states of

are the states of

☎ ✁

plus a new state

✄ ✄
  • 2. Start state of

is

✄ ✄

Regular Expressions (REs) – p.36/37

slide-120
SLIDE 120

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

; that is, states of

are the states of

☎ ✁

plus a new state

✄ ✄
  • 2. Start state of

is

✄ ✄

3.

✂ ☎ ✁ ✄ ✄ ✂ ✁ ✂ ✁

; that is, the accept states of

are the accept states of

☎ ✁

plus the new start state

Regular Expressions (REs) – p.36/37

slide-121
SLIDE 121

Construction procedure

1.

✁ ✄ ✄ ✂ ✁

; that is, states of

are the states of

☎ ✁

plus a new state

✄ ✄
  • 2. Start state of

is

✄ ✄

3.

✂ ☎ ✁ ✄ ✄ ✂ ✁ ✂ ✁

; that is, the accept states of

are the accept states of

☎ ✁

plus the new start state

  • 4. Define

so that for any

✄ ✁
  • and
  • :
☎ ✂ ✄✆✄☎✄✝✄✆✄☎✄✝✄✝✄✝✞ ✄✝✄✝✄✝✄✝✄✝✄✝✄✝✄✝✟ ✁ ✁

if

✄ ✁

and

✄ ✠ ✁ ✂ ✁ ✁ ✁

if

✄ ✁ ✂ ✁

and

☎ ✝ ✁ ✁
✁ ✁ ✄ ✁ ✂ ✄

if

✄ ✁ ✂ ✁

and

✝ ✁ ✄ ✁ ✂ ✄

if

✄ ☎ ✄ ✄

and

if

✄ ☎ ✄ ✄

and

☎ ✝

.

Regular Expressions (REs) – p.36/37

slide-122
SLIDE 122

Examples conversion

Convert the following REs into NFA following the procedure presented above 1.

to an NFA 2.

☎ ☎ ✄
  • Regular Expressions (REs) – p.37/37