1 introduction to computability theory lecture4: regular expressions

Post on 05-Jan-2016

221 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Introduction to Computability Theory

Lecture4: Regular Expressions

Regular languages are defined and described by use of finite automata.

In this lecture, we introduce Regular Expressions as an equivalent way, yet more elegant, to describe regular languages.

Introduction

2

If one wants to describe a regular language, La, he can use the a DFA, D or an NFA N, such that that .

Consider for example the regular expression describing the language of binary strings containing a single 1.

Motivation

3

LaDL

**100

Basic Regular Expressions

A Regular Expression (RE in short) is a string of symbols that describes a regular Language.

• Let be an alphabet. For each , the symbol is an RE representing the set .

• The symbol is an RE representing the set . (The set containing the empty string).

• The symbol is an RE representing the empty set.

4

Inductive Construction

Let and be two regular expressions representing languages and , resp.

• The string is a regular expression representing the set .

• The string is a regular expression representing the set .

• The string is a regular expression representing the set .

1R 2R1L 2L

21 RR 21 LL

21RR

21 LL *1R

*1L

5

Inductive Construction - Remarks

1. Note that in the inductive part of the definition larger RE-s are defined by smaller ones.

2. This inductive definition also dictates the way we will prove theorems: For any theorem T.

Stage 1: Prove T correct for all base cases. Stage 2: Assume T is correct for and .

Prove correctness for , , and .

6

21 RR 21RR *1R

Some Useful Notation

Let be a regular expression:• The string represents , and it also

holds that .• The string represents .

• The string represents . • The Language represented by R is denoted by

.

R

RL

7

R *RR *RR

kR times

...k

RRR

k ...,, 11

Precedence Rules

• The star (*) operation has the highest precedence.

• The concatenation ( ) operation is second on the preference order.

• The union ( ) operation is the least preferred.

• Parentheses can be omitted using these rules.

8

Examples

• – .• – .• – .

• – .

• – .

**100**1

** str

** 011

1 singlea contains | ww

1 singlea least at has | ww

substringa as contains | strww

1 singlea least at by

followed is in 0every | ww

* length even of is | ww

9

Examples

• - all words starting and ending with the same letter.

• - all strings of forms 1,1,…,1 and 0,1,1,…1 .

• - A set concatenated with the empty set yields the empty set .

• - .

101100 **

*10

R

*

10

*

** 101

Regular expressions and finite automata are equivalent in their descriptive power. This fact is expressed in the following Theorem:

TheoremA set is regular if and only if it can be described by a regular expression.

The proof is by two Lemmata (Lemmas):

Equivalence With Finite Automata

11

,

If a language L can be described by regular expression then L is regular.

Lemma ->

12

,

,

Proofs Using Inductive Definition

The proof follows the inductive definition of RE-s as follows:

Stage 1: Prove correctness for all base cases.Stage 2: Assume correctness for and , and

show its correctness for , and .

1R 2R 21 RR 21RR

*1R

13

Induction Basis

1. For any , the expression describes

the set , recognized by:

2. The set represented by

the expression is recognized by:

3. The set represented by

the expression is recognized by:

14

4q0q

4q

4q

Now, we assume that and represent two regular sets and claim that , and represent the corresponding regular sets.

The proof for this claim is straight forward using the constructions given in the proof for the closure of the three regular operations.

The Induction Step

15

1R 2R

21 RR 21 RR *

1R

Show that the following regular expressions represent regular languages:

1. .

2. .

To be demonstrated on the Blackboard.

Examples

16

*aab

ababa *

If a language L is regular then L can be described by some regular expression.

Lemma <-

17

The proof follows the following stages:1. Define Generalized Nondeterministic Finite

Automaton (GNFA in short).2. Show how to convert any DFA to an

equivalent GNFA.3. Show an algorithm to convert any GNFA to an

equivalent GNFA with 2 states.4. Convert a 2-state GNFA to an equivalent RE.

Proof Stages

18

,

1. A GNFA is a finite automaton in which each transition is labeled with a regular expression over the alphabet .

2. A single initial state with all possible outgoing transitions and no incoming trans.

3. A single final state without outgoing trans.4. A single transition between every two states,

including self loops.

Properties of a Generalized NFA

19

,

Example of a Generalized NFA

20

startq

acceptq

*ab

baab

b

aa

ab

*a

*aa

*b

A computation of a GNFA is similar to a computation of an NFA. except:In each step, a GNFA consumes a block of symbols that matches the RE on the transition used by the NFA.

A Computation of a GNFA

21

Consider abba or bb or abbbaaaaabbbbb

Example of a GNFA Computation

22

startq

acceptq

*ab

baab

b

aa

ab

*a

*aa

*b

Conversion is done by a very simple process:1. Add a new start state with an - transition

from the new start state to the old start state.

2. Add a new accepting state with - transition from every old accepting state to the new accepting state.

Converting a DFA (or NFA) to a GNFA

23

4. Replace any transition with multiple labels by a single transition labeled with the union of all labels.

5. Add any missing transition, including self transitions; label the added transition by .

Converting a DFA to a GNFA (Cont)

24

Stage 1: Convert D to a GNFA

1.0 Start with D

25

0

aq

bq

1

cq

1

0

1,0

Stage 1: Convert D to a GNFA

1.1 Add 2 new states

26

0

aq

bq

1

cq

1

0

1,0

acceptqstartq

Stage 1: Convert D to a GNFA

1.2 Make the initial state and the finalstate.

27

0

aq

bq

1

cq

1

0

1,0

acceptqstartq

startq acceptq

Stage 1: Convert D to a GNFA

1.3 Replace multi label transitions by their union.

28

0

aq

bq

1

cq

1

0

10

acceptqstartq

Stage 1: Convert D to a GNFA

1.4 Add all missing transitions and label them .

29

0

aq

bq

1

cq

1

0

startq

acceptq

10

The final element needed for the proof is a procedure in which for any GFN G, any state of G, not including and , can be ripped off G, while preserving .This is demonstrated in the next slide by considering a general state, denoted by , and an arbitrary pair of states, and , as demonstrated in the next slide:

Ripping a state from a GNFA

30

GLstartq acceptq

ripq

iq jq

Removing a state from a GNFA

31

1R

iqjq

ripq

4R

3R

2R

43*

21 RRRR iq jq

Before Ripping After Ripping

Note: This should be done for every pair of outgoing and incoming outgoing .ripq

Consider the RE ,representing all strings that enable transition from via to .

What we want to do is to augment the Regular expression of transition , namely , so These strings can pass through . This is done by setting it to .

Ellaboration

32

1R

iqjq

ripq

4R

3R

2Rripq

43*

214 RRRR

4R ji qq ,

iqjq

3*

21 RRR

ji qq ,

Note: In order to achieve an equivalent GNFA in which is disconnected,this procedure should becarried out separately, for everypair of transitions of the form and . Then can be removed, as demonstrated on the next slide:

Ellaboration

33

1R

iqjq

ripq

4R

3R

2R

ripq

ripi qq , jrip qq ,

ripq

Elaboration

Assume the following situation:In order to rip , all pairsof incoming and outgoingtransitions should be considered in the way showed on the previous slide namely consider one after the other. After that can be ripped while preserving .

34

1t 2t 3t

5t4t 53

4352425141

,

,,,,,,,,,,

tt

tttttttttt

ripq

ripq

ripq

GL

In Particular

Replace with .

35

1R 3R

2R

4R

aq

ripq

4R 3*

214 RRRR

A (half?) Formal Proof of Lemma<-

The first step is to formally define a GNFA.Each transition should be labeled with an RE.Define the transition function as follows:

where denotes all regular expressions over .

Note: The def. of is different then for NFA.

36

REqQqQ startaccept:

RE

Changes in Definition

Note: The definition of as:

is different than the original definitions (For DFA and NFA).

In this definition we rely on the fact that every 2 states (except and ) are connected in both directions.

37

REqQqQ startaccept:

startq

acceptq

A Generalized Finite Automaton is a 5-tupple where:

1. is a finite set called the states.2. is a finite set called the alphabet.3. * is the transition

function.4. is the start state, and5. is the accept state.

REqQqQ startaccept:

GNFA – A Formal Definition

38

acceptstart qqQ ,,,, Q

Qqstart

Qqaccept

A GNFA accepts a string if and there exists a sequence of states , satisfying:

For each , , , where , or in other words, is the expression on the arrow from to .

GNFA – Defining a Computation

39

*w kwwww 21

acceptstart qqqq 21

i ki 1 ii RLw iii qqR ,1 iR

iq 1iq

Procedure CONVERT takes as input a GNFA G with k states.

If then these 2 states must be and , and the algorithm returns

.If , the algorithm converts G to an

equivalent G’ with states by use of the ripping procedure described before.

Procedure CONVERT

40

acceptstart qq ,

2k startqacceptq

2k1k

Convert 1. ;2. If return ;3. ;4. ;5. For any and any

for return ;

Procedure CONVERT

41

2kstartq Grip QtateGetRandomSq

G|| GQk

acceptstart qq ,

ripG qQQ 'acceptj qQq ' starti qQq '

43*

21,' RRRRqq ji ripi qqR ,1 irip qrR ,3 riprip qqR ,2 ji qqR ,4

acceptstart qqQG ,,',,''

In this lecture we:1. Motivated and defined regular expressions as

a more concise and elegant method to represent regular Languages.

2. Proved that FA-s (Deterministic as well as Nondeterministic) and RE-s is identical by:2.1 Defined GNFA – s.2.2 Showed how to convert a DFA to a GNFA.2.3 Showed an algorithm to converted a GNFA with states to an equivalent GNFA with states.

Recap

42

K1K

top related