conversions & pumping lemma cpsc 388 fall 2001 ellen walker hiram college

23
Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Upload: kerry-park

Post on 19-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Conversions & Pumping Lemma

CPSC 388 Fall 2001Ellen WalkerHiram College

Page 2: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

For every r.e. there is a DFA...

• If we can construct an NFA, that’s good enough

• Prove by induction:– Base cases: , , a (element of

) -- build an NFA for each.– Step cases: r | s , rs , r* -- build resulting NFA from NFA’s from r and/or s

Page 3: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Base Cases

• a

a

Page 4: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

A Generic NFA

• Initial and (single) accept state– For multiple accept states, add

-transitions from all accept states to one

• Arbitrary labeled connections (and other states) as well

Page 5: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

NFA for Concatenation (rs)

Page 6: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

NFA for choice (r | s)

Page 7: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

NFA for star (a*)

Page 8: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Completing the Proof

• Because every r.e. can be constructed from basic r.e. (and each has an NFA)

• And there are only 3 ways to combine r.e.’s (and we have NFA for each)

• Therefore, we can create an NFA for every regular expression

Page 9: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

From DFA to R.E.

• Every expression that a DFA accepts is represented by a path from an initial state to an accept state

• If there are intermediate states, they can be deleted and represented by the r.e.’s necessary to get past them.

Page 10: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

R.E. from 2-state DFA

s

t

r

v

This DFA accepts (r+ts*v)*ts*

(Consider all paths from initial to final)

Page 11: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Extending to Larger DFA’s

• For any state that is neither initial nor final, compute r.e. for each path *through* the state

• Replace the state by links labeled with the full regular expressions

• Repeat until all states are initial or final, then make a choice of all paths

Page 12: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Now find the expression for...

• All strings that do not contain the sequence “ab”

• Use – R.E. -> NFA for not-L ((a+b)*ab(a+b)*)

– NFA -> DFA– Swap accept vs. not accept states (for L)

– DFA -> R.E.

Page 13: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

A Regular Grammar

• A regular grammar is a grammar with all rules of the form– X -> a– X -> aY (left linear rule)– X -> Ya (right linear rule)

• Either left-linear or right-linear rules may be used, but not both in the same grammar

Page 14: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Left-linear Grammar to NFA

• X -> a– a-transition from X to accept state– (a can be )

• X -> aY– a-transition from X to Y – (Y can be X)

• Use the same idea to take NFA to grammar!

Page 15: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

A non-regular language

• All strings of the form a*b* where the number of a’s is equal to the number of b’s– This language requires a way of keeping track of how many a’s we’ve seen -- but the only way we can keep track is by the number of states!

Page 16: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Proving a Language Non-Regular

• “I tried real hard and couldn’t come up with a DFA, NFA, or regular expression…” -- not a proof!

• “There is a property that all regular languages have that this one doesn’t…” -- now that’s a proof

Page 17: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Long Strings Need Loops

• A DFA has a finite number of states (N)

• If we have N (or more) characters in a string, we must have visited one of the states twice within the first N-1 chars.

• Therefore, there must be a loop.– Therefore, we can repeat part of the string an arbitrary # of times (including 0)

Page 18: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

A Pumping Lemma

• Every string that is accepted by a DFA and is longer than N (number of states) can be divided into 3 parts (xyz), where– The length of xy ≤ N– The length of y>0– The strings xz, xyyz, xyyyz, etc. are also in the language

• (i.e. “y” is the part accepted by the loop)

Page 19: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Using the Pumping Lemma

• Pick a string in the language that is longer than N

• Show that there is no way to divide it up according to the rules of the lemma

• Therefore, by contradiction, the language cannot be regular

Page 20: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

An example: a*b*, #a’s = #b’s

1. Assume it’s regular. Pick the string with N a’s and N b’s

2. Since |xy| ≤ N, y must have all a’s

3. “Pumping” xyyz has too many a’s4. But the pumping lemma said that string had to be in the language.

5. We have a contradiction, so the language is not regular.

Page 21: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Notes on applying the pumping lemma

• It only has to fail for one string in the language

• (But…) It has to fail for all legal assignments of xyz

• Sometimes it’s easier to “pump down” to xz instead of “pumping up” to xyyz

Page 22: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

L= a* where the number of a’s is square

1. Assume the language is regular. Pick the string of N2 a’s

2. If we divide it we have |xy| ≤ N3. xyyz is at most (N2+N) a’s.4. But, the next biggest square is N2+N+1 (so this string cannot be accepted)!

5. So the pumping lemma doesn’t hold, so the language is not regular.

Page 23: Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College

Proving a language...

• Regular:– Give a regular expression– Give a finite automaton (NFA or DFA)

– Create a regular grammar

• Non-Regular:– Proof by contradiction using the pumping lemma