Regular Grammars Generate Regular Languages 1 Theorem Regular

  • Slides: 44
Download presentation
Regular Grammars Generate Regular Languages 1

Regular Grammars Generate Regular Languages 1

Theorem Regular grammars generate exactly the class of regular languages: If is a regular

Theorem Regular grammars generate exactly the class of regular languages: If is a regular grammar then is a regular language If is a regular language then there is a regular grammar with 2

Proof First we prove: If is a regular grammar then is a regular language

Proof First we prove: If is a regular grammar then is a regular language can be: Right-linear grammar or Left-linear grammar 3

The case of Right-Linear Grammars Let be a right-linear grammar We will show: is

The case of Right-Linear Grammars Let be a right-linear grammar We will show: is regular The proof: We will construct an NFA with 4

Grammar is right-linear Example: 5

Grammar is right-linear Example: 5

Construct NFA such that every state is a variable: special final state 6

Construct NFA such that every state is a variable: special final state 6

Add edges for each production: 7

Add edges for each production: 7

8

8

9

9

10

10

11

11

12

12

NFA Grammar 13

NFA Grammar 13

In General A right-linear grammar has variables: and productions: or 14

In General A right-linear grammar has variables: and productions: or 14

We construct the NFA each variable such that: corresponds to a node: 15

We construct the NFA each variable such that: corresponds to a node: 15

For each production: we add transitions and intermediate nodes ……… 16

For each production: we add transitions and intermediate nodes ……… 16

For each production: we add transitions and intermediate nodes ……… 17

For each production: we add transitions and intermediate nodes ……… 17

Resulting NFA looks like this: 18

Resulting NFA looks like this: 18

Now, we need to show: 19

Now, we need to show: 19

The Case: Take We will show: there is a path with label from state

The Case: Take We will show: there is a path with label from state to state in ………… 20

Grammar looks like: strings 21

Grammar looks like: strings 21

22

22

Since: …… …… We have: 26

Since: …… …… We have: 26

Since: We have: and …… We have: 27

Since: We have: and …… We have: 27

The Case: Take We will show that in 28

The Case: Take We will show that in 28

Since there is a path …… 29

Since there is a path …… 29

Write: There is a path …… …… 30

Write: There is a path …… …… 30

Since: …… …… This derivation is possible 31

Since: …… …… This derivation is possible 31

Since: We have: 32

Since: We have: 32

The Case of Left-Linear Grammars Let be a left-linear grammar We will show: is

The Case of Left-Linear Grammars Let be a left-linear grammar We will show: is regular The proof: We will construct a right-linear grammar with 33

Since is left-linear grammar the productions look like: 34

Since is left-linear grammar the productions look like: 34

Construct right-linear grammar In : 35

Construct right-linear grammar In : 35

In : 36

In : 36

It is easy to see that: Since is right-linear, we have: is regular language

It is easy to see that: Since is right-linear, we have: is regular language (homework) is regular language 37

Proof - Part 2 Now we will prove: If is a regular language then

Proof - Part 2 Now we will prove: If is a regular language then there is a regular grammar with Proof outline: we will take an NFA for and convert it to a regular grammar 38

Since is regular There is an NFA such that Example: 39

Since is regular There is an NFA such that Example: 39

Convert to a right-linear grammar 40

Convert to a right-linear grammar 40

41

41

42

42

We can generalize this process for any regular language : For any regular language

We can generalize this process for any regular language : For any regular language we obtain an right-linear grammar with 43

Since is right-linear grammar is also a regular grammar with 44

Since is right-linear grammar is also a regular grammar with 44