Top Banner
1 Chapter 11 - 4 Regular Language Topics
21

1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

Jan 01, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

1

Chapter 11 - 4

Regular Language Topics

Page 2: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

2

Section 11.4 Regular Language Topics

• Regular languages are also characterized by special grammars called regular grammars whose productions take the following form, where w is a string of terminals.

A → wB or A → w. • Example. A regular grammar for the language of

a*b* is

S → ٨ | aS | T

T → b | bT.

Page 3: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

3

Quiz

• Write a regular grammar for {ab, acb, accb, …, acnb, …}.

• Solution: A regular expression for the language is ac*b. A regular grammar is

S → aT

T → b | cT.

Page 4: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

4

Left/Right-regular grammar

• Regular grammars as we have defined them are often called right-regular because there are also left-regular grammars that require productions to have the form A → Bw or A → w.

• Any language with a right-regular grammar also has a left-regular grammar, and conversely.

Page 5: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

5

Example

• For the regular expression a*bc* we have the following grammars.

• Right regular: S → aS | bT

T → ٨ | cT.

• Left Regular: S → Sc | Tb

T → ٨ | Ta.

Page 6: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

6

derivation

• Notice in the previous example that right-regular grammars derive strings by examining the leftmost letter first, while left-regular grammars examine the rightmost letter first.

• Example/Quiz. Find a right-regular grammar and a left-regular grammar for the language of the regular expression a(b + c)*de*.

• Right regular: S → aT T → bT | cT | dU

U → ٨ | eU.• Left Regular: S → Se | Td

T → a | Tb | Tc.

Page 7: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

7

Transforming an NFA to a Regular Grammar

1. State names become the nonterminals. So rename them to be uppercase letters.

2. The start state becomes the start symbol of the grammar.

3. For each state transition from I to J labeled with x construct a production I → xJ.

4. For each final state F construct a production F → ٨.

Page 8: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

8

Example/Quiz

• Transform the following NFA into a regular grammar. Solution: S → aI | F I → aI | bF F → ٨ This grammar can be simplified to S → aI | ٨I → aI | b

Page 9: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

9

Example/Quiz

• Transform the regular expression a*(ba)* into a regular grammar.

• Solution: Draw an NFA and then use the algorithm.

An NFA:

The resulting grammar: S → aS | bI | ٨ I → aF F → bJ | ٨ J → aF.

Page 10: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

10

Quiz

• Simplify the grammar.

• Answer:

S → aS | baF | ٨ F → baF | ٨

Page 11: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

11

Quiz• What happens when a DFA has an error state, like the following

DFA?

• Answer: The resulting grammar: S → aF | bE F → aE | bJ | ٨ J → aF | bE E → aE | bE.

But E does not derive a terminal string. So the grammar simplifies to

S → aFF → bJ | ٨ J → aF.

Page 12: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

12

Quiz

• Simplify the grammar further.

• Answer: S → aF

F → baF | ٨.

Page 13: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

13

Transforming a Regular Grammar to an NFA

1. Replace any production with multiple terminals by productions with single terminals.

2. The start state is the grammar start symbol. 3. Transform I → aJ into a transition from I to J

labeled with a. 4. Transform I → J into a transition from I to J

labeled with ٨. 5. Transform each I → a into a transition from I to

new single final state F labeled with a. 6. The final states are F together with each state I

with a production I → ٨.

Page 14: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

14

Example/Quiz

• Transform the following regular grammar into a NFA. S → abS | T | ٨T → cT | d

• Solution. Transform S → abS into S → aI and I → bS, so the grammar becomes S → aI | T | ٨ I → bST → cT | d

Now the NFA can be drawn:

Page 15: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

15

Quiz

• What is the regular expression for the language of the grammar?

• Answer: (ab)*(٨ + c*d)

Page 16: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

16

Properties of Regular Languages

• When we know some properties of regular languages they can help us argue, BWOC, that certain languages are not regular.

Page 17: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

17

The Pumping Lemma• If L is an infinite regular language, then it is recognized by a DFA

with, say, m states.• If s in L and | s | ≥ m, then an acceptance path for s must pass

through some state twice.• The following graph depicts the situation.

• The dotted arrows represent the path to acceptance for s and the letters x, y, and z represent the concatenation of the letters along the edges of the path. So s = xyz and y ≠ ٨. Assume that the middle state is the first repeated state on the path. So | xy | ≤ m. Since the loop can be traversed any number of times, we have the pumping property: xykz ∈ L for all k ∈ N

Page 18: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

18

Example

• The language L = {anbn | n ∈ N} is not regular.• Proof: Assume, BWOC, that L is regular. Since L is

infinite, the pumping lemma applies.Choose s = ambm. Then s = xyz, where y ≠ ٨, | xy | ≤ m,

and xykz ∈ L for all k ∈ N.Since | xy | ≤ m and s = ambm = xyz, it follows that xy is a

string of a’s.Since y ≠ ٨, it must be that y = ai for some i > 0.We’ll try for a contradiction with k = 2. The pumping

property implies xy2z ∈ L.But xy2z = am + ibm, which is not in L because i > 0.This contradiction implies that L is not regular. QED.

Page 19: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

19

Quiz

• In the previous proof we exhibited a contradiction when k = 2. Find similar contradictions for k = 0 and k = 3.

• Answer: (k = 0) The pumping property implies xy0z ∈ L. In other words, xz ∈ L.But xz = am-ibm, which is not in L because i > 0. This

contradiction implies that L is not regular. QED.(k = 3) The pumping property implies xy3z ∈ L. But xy3z

= am + 2ibm, which is not in L because i > 0. This contradiction implies that L is not

regular. QED.

Page 20: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

20

Example/Quiz• Prove the language L = {aabnac2n | n ∈ N} is not regular. • Proof: Assume, BWOC, that L is regular. Since L is infinite, the pumping lemma

applies. Choose s = aabmac2m. Then s = xyz, where y ≠ ٨, | xy | ≤ m and xykz ∈ L for all k ∈

N. Since | xy | ≤ m, there are three cases for y: Case 1: y = aabi for some 0 ≤ i ≤ m-2. (Remember, | xy | ≤ m, so x = ٨ in this case.) Case 2: y = abi for some 0 ≤ i ≤ m-2. (In this case, we have x = a.) Case 3: y = bi for some 0 < i ≤ m-2-j. (In this case, x = aabj for some 0 ≤ j ≤ m-3) We can obtain a contradiction in each case by considering k = 0, which implies xz ∈

L. Case 1: xz = bm-iac2m which is not in L because strings of L must begin with aa. Case 2: xz = abm-iac2m which is not in L because strings of L must begin with aa. Case 3: xz = aabm-iac2m which is not in L because strings in L must have twice as

many c’s as b’s which can’t be the case since i > 0.So each of the three cases yields a contradiction. Therefore L is not regular. QED.

Page 21: 1 Chapter 11 - 4 Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.

21

The End of Chapter 11 - 4