reverse of a regular language

75
1 Reverse of a Regular Language

Upload: hollie

Post on 21-Jan-2016

51 views

Category:

Documents


0 download

DESCRIPTION

Reverse of a Regular Language. Proof idea:. Construct NFA that accepts :. invert the transitions of the NFA that accepts. Theorem:. The reverse of a regular language is a regular language. Proof. Since is regular, there is NFA that accepts. Example:. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Reverse of a Regular Language

1

Reverse of a Regular Language

Page 2: Reverse of a Regular Language

2

Theorem:

The reverse of a regular languageis a regular language

RL L

Proof idea:

Construct NFA that accepts :RL

invert the transitions of the NFAthat accepts L

Page 3: Reverse of a Regular Language

3

Proof

Since is regular, there is NFA that accepts

L

Example:

baabL *

a

b

ba

L

Page 4: Reverse of a Regular Language

4

Invert Transitions

a

b

ba

Page 5: Reverse of a Regular Language

5

Make old initial state a final state

a

b

ba

Page 6: Reverse of a Regular Language

6

Add a new initial state

a

b

ba

Page 7: Reverse of a Regular Language

7

a

b

ba

Resulting machine accepts RL

baabL *

ababLR *

RL is regular

Page 8: Reverse of a Regular Language

8

Grammars

Page 9: Reverse of a Regular Language

9

GrammarsGrammars express languages

Example: the English language

verbpredicate

nounarticlephrasenoun

predicatephrasenounsentence

_

_

Page 10: Reverse of a Regular Language

10

walksverb

runsverb

dognoun

boynoun

thearticle

aarticle

Page 11: Reverse of a Regular Language

11

A derivation of “the boy walks”:

walksboythe

verbboythe

verbnounthe

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

_

_

Page 12: Reverse of a Regular Language

12

A derivation of “a dog runs”:

runsdoga

verbdoga

verbnouna

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

_

_

Page 13: Reverse of a Regular Language

13

Language of the grammar:

L = { “a boy runs”, “a boy walks”, “the boy runs”, “the boy walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }

Page 14: Reverse of a Regular Language

14

Notation

dognoun

boynoun

Variable orNon-terminal

TerminalProductionrule

Page 15: Reverse of a Regular Language

15

Another ExampleGrammar:

Derivation of sentence :

S

aSbS

abaSbS

ab

aSbS S

Page 16: Reverse of a Regular Language

16

aabbaaSbbaSbS

aSbS S

aabb

S

aSbSGrammar:

Derivation of sentence :

Page 17: Reverse of a Regular Language

17

Other derivations:

aaabbbaaaSbbbaaSbbaSbS

aaaabbbbaaaaSbbbb

aaaSbbbaaSbbaSbS

Page 18: Reverse of a Regular Language

18

Language of the grammar

S

aSbS

}0:{ nbaL nn

Page 19: Reverse of a Regular Language

19

More Notation

Grammar PSTVG ,,,

:V

:T

:S

:P

Set of variables

Set of terminal symbols

Start variable

Set of Production rules

Page 20: Reverse of a Regular Language

20

Example

Grammar :

S

aSbSG

PSTVG ,,,

}{SV },{ baT

},{ SaSbSP

Page 21: Reverse of a Regular Language

21

More NotationSentential Form: A sentence that contains variables and terminals

Example:

aaabbbaaaSbbbaaSbbaSbS

Sentential Forms sentence

Page 22: Reverse of a Regular Language

22

We write:

Instead of:

aaabbbS*

aaabbbaaaSbbbaaSbbaSbS

Page 23: Reverse of a Regular Language

23

In general we write:

If:

nww*

1

nwwww 321

Page 24: Reverse of a Regular Language

24

By default: ww*

Page 25: Reverse of a Regular Language

25

Example

S

aSbS

aaabbbS

aabbS

abS

S

*

*

*

*

Grammar Derivations

Page 26: Reverse of a Regular Language

26

baaaaaSbbbbaaSbb

aaSbbS

S

aSbS

Grammar

Example

Derivations

Page 27: Reverse of a Regular Language

27

Another Grammar ExampleGrammar :

A

aAbA

AbS

Derivations:

aabbbaaAbbbaAbbS

abbaAbbAbS

bAbS

G

Page 28: Reverse of a Regular Language

28

More Derivations

aaaabbbbbaaaaAbbbbb

aaaAbbbbaaAbbbaAbbAbS

bbaS

bbbaaaaaabbbbS

aaaabbbbbS

nn

Page 29: Reverse of a Regular Language

29

Language of a Grammar

For a grammar with start variable :

GS

}:{)( wSwGL

String of terminals

Page 30: Reverse of a Regular Language

30

ExampleFor grammar :

A

aAbA

AbS

}0:{)( nbbaGL nn

Since: bbaS nn

G

Page 31: Reverse of a Regular Language

31

A Convenient Notation

A

aAbA|aAbA

thearticle

aarticle

theaarticle |

Page 32: Reverse of a Regular Language

32

Linear Grammars

Page 33: Reverse of a Regular Language

33

Linear GrammarsGrammars with at most one variable at the right sideof a production

Examples:

A

aAbA

AbS

S

aSbS

Page 34: Reverse of a Regular Language

34

A Non-Linear Grammar

bSaS

aSbS

S

SSS

Grammar :G

)}()(:{)( wnwnwGL ba

Page 35: Reverse of a Regular Language

35

Another Linear Grammar

Grammar :

AbB

aBA

AS

|

}0:{)( nbaGL nn

G

Page 36: Reverse of a Regular Language

36

Right-Linear GrammarsAll productions have form:

Example:

xBA

xAor

aS

abSS

Page 37: Reverse of a Regular Language

37

Left-Linear GrammarsAll productions have form:

Example:

BxA

aB

BAabA

AabS

|

xAor

Page 38: Reverse of a Regular Language

38

Regular Grammars

Page 39: Reverse of a Regular Language

39

Regular GrammarsA regular grammar is any right-linear or left-linear grammar

Examples:

aS

abSS

aB

BAabA

AabS

|

1G 2G

Page 40: Reverse of a Regular Language

40

ObservationRegular grammars generate regular

languages

Examples:

aS

abSS

aabGL *)()( 1

aB

BAabA

AabS

|

*)()( 2 abaabGL

1G

2G

Page 41: Reverse of a Regular Language

41

Regular Grammars Generate

Regular Languages

Page 42: Reverse of a Regular Language

42

Theorem

LanguagesGenerated byRegular Grammars

RegularLanguages

Page 43: Reverse of a Regular Language

43

Theorem - Part 1

LanguagesGenerated byRegular Grammars

RegularLanguages

Any regular grammar generatesa regular language

Page 44: Reverse of a Regular Language

44

Theorem - Part 2

LanguagesGenerated byRegular Grammars

RegularLanguages

Any regular language is generated by a regular grammar

Page 45: Reverse of a Regular Language

45

Proof – Part 1

LanguagesGenerated byRegular Grammars

RegularLanguages

The language generated by any regular grammar is regular

)(GLG

Page 46: Reverse of a Regular Language

46

The case of Right-Linear Grammars

Let be a right-linear grammar

We will prove: is regular

Proof idea: We will construct NFA with

G

)(GL

M)()( GLML

Page 47: Reverse of a Regular Language

47

Grammar is right-linearG

Example:

aBbB

BaaA

BaAS

|

|

Page 48: Reverse of a Regular Language

48

Construct NFA such thatevery state is a grammar variable:

M

aBbB

BaaA

BaAS

|

|

S FV

A

B

specialfinal state

Page 49: Reverse of a Regular Language

49

Add edges for each production:

S FV

A

B

a

aAS

Page 50: Reverse of a Regular Language

50

S FV

A

B

a

BaAS |

Page 51: Reverse of a Regular Language

51

S FV

A

B

a

BaaA

BaAS

|

a

a

Page 52: Reverse of a Regular Language

52

S FV

A

B

a

bBB

BaaA

BaAS

|

a

a

b

Page 53: Reverse of a Regular Language

53

S FV

A

B

a

abBB

BaaA

BaAS

|

|

a

a

b

a

Page 54: Reverse of a Regular Language

54

aaabaaaabBaaaBaAS

S FV

A

B

a

a

a

b

a

Page 55: Reverse of a Regular Language

55

SFV

A

B

a

a

a

b

aabBB

BaaA

BaAS

|

|

G

M GrammarNFA

abaaaab

GLML

**

)()(

Page 56: Reverse of a Regular Language

56

In GeneralA right-linear grammar

has variables:

and productions:

G

,,, 210 VVV

jmi VaaaV 21

mi aaaV 21

or

Page 57: Reverse of a Regular Language

57

We construct the NFA such that:

each variable corresponds to a node:

M

iV

0V

FV

1V

2V

3V

4V specialfinal state

Page 58: Reverse of a Regular Language

58

For each production:

we add transitions and intermediate nodes

jmi VaaaV 21

iV jV………

1a 2a ma

Page 59: Reverse of a Regular Language

59

For each production:

we add transitions and intermediate nodes

mi aaaV 21

iV FV………

1a 2a ma

Page 60: Reverse of a Regular Language

60

Resulting NFA looks like this:M

0V

FV

1V

2V

3V

4V

1a

3a

3a

4a

8a

2a 4a

5a

9a5a

9a

)()( MLGL It holds that:

Page 61: Reverse of a Regular Language

61

The case of Left-Linear Grammars

Let be a left-linear grammar

We will prove: is regular

Proof idea: We will construct a right-linear grammar with

G

)(GL

G RGLGL )()(

Page 62: Reverse of a Regular Language

62

Since is left-linear grammar

the productions look like:

G

kaaBaA 21

kaaaA 21

Page 63: Reverse of a Regular Language

63

Construct right-linear grammar G

In :G kaaBaA 21

In :G BaaaA k 12

vBA

BvA R

Page 64: Reverse of a Regular Language

64

Construct right-linear grammar G

In :G kaaaA 21

In :G 12aaaA k

vA

RvA

Page 65: Reverse of a Regular Language

65

It is easy to see that:

Since is right-linear, we have:

RGLGL )()(

)(GL RGL )(

G

)(GL

RegularLanguage

RegularLanguage

RegularLanguage

Page 66: Reverse of a Regular Language

66

Proof - Part 2

LanguagesGenerated byRegular Grammars

RegularLanguages

Any regular language is generated by some regular grammar

LG

Page 67: Reverse of a Regular Language

67

Proof idea:

Let be the NFA with . Construct from a regular grammar such that

Any regular language is generated by some regular grammar

LG

M )(MLL

M G)()( GLML

Page 68: Reverse of a Regular Language

68

Since is regularthere is an NFA such that

LM )(MLL

Example:a

b

a

b*)*(* abbababL

)(MLL

M

1q 2q

3q

0q

Page 69: Reverse of a Regular Language

69

Convert to a right-linear grammarM

a

b

a

b

M0q 1q 2q

3q10 aqq

Page 70: Reverse of a Regular Language

70

a

b

a

b

M0q 1q 2q

3q

21

11

10

aqq

bqq

aqq

Page 71: Reverse of a Regular Language

71

a

b

a

b

M0q 1q 2q

3q

32

21

11

10

bqq

aqq

bqq

aqq

Page 72: Reverse of a Regular Language

72

a

b

a

b

M0q 1q 2q

3q

3

13

32

21

11

10

q

qq

bqq

aqq

bqq

aqq

G

LMLGL )()(

Page 73: Reverse of a Regular Language

73

In General

For any transition:aq p

Add production: apq

variable terminal variable

Page 74: Reverse of a Regular Language

74

For any final state: fq

Add production: fq

Page 75: Reverse of a Regular Language

75

Since is right-linear grammar

is also a regular grammar

with

G

G

LMLGL )()(