240 likes | 272 Vues
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.
E N D
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
Base Cases • • e • a
A Generic NFA • Initial and (single) accept state • For multiple accept states, add e-transitions from all accept states to one • Arbitrary labeled connections (and other states) as well
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
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.
R.E. from 2-state DFA This DFA accepts (r+ts*v)*ts* (Consider all paths from initial to final)
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
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.
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
Left-linear Grammar to NFA • X -> a • a-transition from X to accept state • (a can be e) • X -> aY • a-transition from X to Y • (Y can be X) • Use the same idea to take NFA to grammar!
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!
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
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)
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)
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
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’s 4. But the pumping lemma said that string had to be in the language. 5. We have a contradiction, so the language is not regular.
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
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| ≤ N 3. 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.
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