Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Non regular languages

2,510 views

Published on

Published in: Technology, Education
  • Be the first to comment

Non regular languages

  1. 1. Non-regular languages
  2. 2. n n {a b : n 0}Non-regular languages R {ww : w {a, b}*} Regular languages a *b b*c a b c ( a b) * etc...
  3. 3. How can we prove that a language Lis not regular?Prove that there is no DFA that accepts LProblem: this is not easy to proveSolution: the Pumping Lemma !!!
  4. 4. • DFA with 4 states b b b q1 a q2 b q3 b q4 a a
  5. 5. In walks of strings: a no state aa is repeated • aab b b b q1 a q2 a q3 b q4 a a
  6. 6. In walks of strings: aabb a state is repeated bbaa • abbabb abbbabbabb... b b b q1 a q2 a q3 b q4 a a
  7. 7. In walks of strings: aabb a state is repeated bbaa • abbabb abbbabbabb... b b b q1 a q2 a q3 b q4 a a
  8. 8. If string w has length | w | 4:• Then the transitions of string w are more than the states of the DFA Thus, a state must be repeated b b b q1 a q2 a q3 b q4 a a
  9. 9. In general, for any DFA: String w has length number of statesA state q must be repeated in the walk of w walk of w ...... q ...... Repeated state
  10. 10. The Pumping Lemma
  11. 11. Take an infinite regular language L DFA that accepts L m states
  12. 12. Take string w with w L There is a walk with label w: ......... walk w
  13. 13. If string w has length | w | m number of states of DFAthen, from the pigeonhole principle: a state q is repeated in the walk w ...... q ...... walk w
  14. 14. Let q be the first state repeated ...... q ...... walk w
  15. 15. Write w xyz y ...... q ...... x z
  16. 16. Observations: length |x y| m number of states length | y| 1 of DFA y ...... q ...... x z
  17. 17. Observation: The string xz is accepted y ...... q ...... x z
  18. 18. Observation: The string xyyz is accepted y ...... q ...... x z
  19. 19. Observation: The string xyyyz is accepted y ...... q ...... x z
  20. 20. iIn General: The string xy z is accepted i 0, 1, 2, ... y ...... q ...... x z
  21. 21. In General: i i 0, 1, 2, ... xy z L The original language y ...... q ...... x z
  22. 22. In other words, we described: The Pumping Lemma !!!
  23. 23. The Pumping Lemma: • Given a infinite regular language L• there exists an integer m• for any string w L with length | w | m• we can write w xyz• with |x y| m and | y| 1 i• such that: xy z L i 0, 1, 2, ...
  24. 24. Theorem: The language n n L {a b : n 0} is not regular Proof: Use the Pumping Lemma
  25. 25. n n L {a b : n 0}Assume for contradictionthat L is a regular languageSince L is infinitewe can apply the Pumping Lemma
  26. 26. n n L {a b : n 0}Let m be the integer in the Pumping LemmaPick a string w such that: w L length | w| m m m We pick w a b
  27. 27. m mWrite: a b xyzFrom the Pumping Lemmait must be that length | x y | m, | y | 1 m m m m xyz a b a...aa...aa...ab...b x y z k Thus: y a , k 1
  28. 28. m m k xyz a b y a , k 1 iFrom the Pumping Lemma: xy z L i 0, 1, 2, ... 2 Thus: xy z L
  29. 29. m m k xyz a b y a , k 1 2From the Pumping Lemma: xy z L m k m 2 xy z a...aa...aa...aa...ab...b L x y y z m k m Thus: a b L
  30. 30. m k m a b L For all k > = 1BUT: n n L {a b : n 0} m k m a b L CONTRADICTION!!!
  31. 31. Therefore: Our assumption that L L is a regular language is not trueConclusion: L is not a regular language
  32. 32. Non-regular language n n {a b : n 0} Regular languages
  33. 33. Non-regular languages R L {ww : w *} Regular languages
  34. 34. Theorem: The language R {a, b} L {ww : w *} is not regular Proof: Use the Pumping Lemma
  35. 35. R L {ww : w *}Assume for contradictionthat L is a regular languageSince L is infinitewe can apply the Pumping Lemma
  36. 36. R L {ww : w *}Let m be the integer in the Pumping LemmaPick a string w such that: w L and length | w| m m m m m We pick w a b b a
  37. 37. m m m mWrite a b b a xyzFrom the Pumping Lemmait must be that length | x y | m, | y | 1 m m m m xyz a...aa...a...ab...bb...ba...a x y z k Thus: y a , k 1
  38. 38. m m m m kxyz a b b a y a , k 1From the Pumping Lemma: i xy z L i 0, 1, 2, ... 2 Thus: xy z L
  39. 39. m m m m kxyz a b b a y a , k 1 2From the Pumping Lemma: xy z L m+k m m m xy z = a...aa...aa...a...ab...bb...ba...a ∈ L 2 x y y z m k m m m Thus: a b b a L
  40. 40. m k m m m a b b a L k 1BUT: R L {ww : w *} m k m m m a b b a L CONTRADICTION!!!
  41. 41. Therefore: Our assumption that L is a regular language is not trueConclusion: L is not a regular language
  42. 42. Non-regular languages n l n l L {a b c : n, l 0} Regular languages
  43. 43. Theorem: The language n l n l L {a b c : n, l 0} is not regular Proof: Use the Pumping Lemma
  44. 44. n l n l L {a b c : n, l 0}Assume for contradictionthat L is a regular languageSince L is infinitewe can apply the Pumping Lemma
  45. 45. n l n l L {a b c : n, l 0}Let m be the integer in the Pumping LemmaPick a string w such that: w L and length | w| m We pick w a b c m m 2m
  46. 46. m m 2mWrite a b c xyzFrom the Pumping Lemmait must be that length | x y | m, | y | 1 m m 2m xyz a...aa...aa...ab...bc...cc...c x y zThus: k y a , k 1
  47. 47. m m 2m k xyz a b c y a , k 1From the Pumping Lemma: i xy z L i 0, 1, 2, ... Thus: 0 xy z xz L
  48. 48. m m 2m k xyz a b c y a , k 1From the Pumping Lemma: xz L m k m 2m xz a...aa...ab...bc...cc...c L x z m k m 2m Thus: a b c L
  49. 49. m k m 2m a b c L k 1BUT: n l n l L {a b c : n, l 0} m k m 2m a b c L CONTRADICTION!!!
  50. 50. Therefore: Our assumption that L is a regular language is not trueConclusion: L is not a regular language
  51. 51. Non-regular languages n! L {a : n 0} Regular languages
  52. 52. ExampleL ={ w | w =0n where n is a perfect square} Let us assume that L is regular.Use pumping Lemma Let m be the corresponding no. for pumping lemma. w = 0square of m|w| >= m w = x y z where | x y| <= m; |y | >=1
  53. 53. Consider x yy z it should be in L|xyyz| must be a perfect square but |xyyz| = m2 + |y| <= m2 + m < m2 + m +m+1 = (m+1)2 m2 < |xyyz |< (m+1)2 so length of the string x yyz can not be a perfect square.So it can not bea member of L. A contradiction! Hence L is non regular
  54. 54. Theorem: The language n! L {a : n 0} is not regular n! 1 2  (n 1) nProof: Use the Pumping Lemma
  55. 55. n! L {a : n 0}Assume for contradictionthat L is a regular languageSince L is infinitewe can apply the Pumping Lemma
  56. 56. n! L {a : n 0}Let m be the integer in the Pumping LemmaPick a string w such that: w L length | w| m m! We pick w a
  57. 57. m!Write a xyzFrom the Pumping Lemmait must be that length | x y | m, | y | 1 m m! m m! xyz a a...aa...aa...aa...aa...a x y z k Thus: y a , 1 k m
  58. 58. m! k xyz a y a , 1 k mFrom the Pumping Lemma: i xy z L i 0, 1, 2, ... Thus: 2 xy z L
  59. 59. m! k xyz a y a , 1 k m 2From the Pumping Lemma: xy z L m k m! m 2 xy z a...aa...aa...aa...aa...aa...a L x y y z Thus: a m! k L
  60. 60. m! k a L 1 k mSince: n! L {a : n 0} There must exist p such that: m! k p!
  61. 61. However: m! k m! m for m 1 m! m! m!m m! m!(m 1) (m 1)! m! k (m 1)! m! k p! for any p
  62. 62. m! k 1 k m a L n!BUT: L {a : n 0} m! k a L CONTRADICTION!!!
  63. 63. Therefore: Our assumption that L is a regular language is not trueConclusion: L is not a regular language
  64. 64. Minimization of Finite Automata
  65. 65. • State minimization problem• There can be more than one DFA thataccepts the same language Among these equivalent DFAs, it is oftenuseful to find the smallest, that is, the DFAwith the minimum possible number of statesThis is especially important when DFAs areused for designing computer hardwarecircuits
  66. 66. a,b b 1 3 a a0 b 4 b 5 2 b a a a,b
  67. 67. Equivalent finite automata a,b b 0 1 2 a a ,b
  68. 68. Unreachable states A DFA sometimes contains states thatcannot possibly be reached from the initial stateThese can easily be identified and they can be removed without changing the language accepted by the DFA
  69. 69. Two states p and q of a dfa are indistinguishable if * (p, w) F implies * (q,w ) FAnd *(p, w) F implies * (q,w) F for all w * .If on the other hand there exists some string w such that *(p,w) F and *(q,w) FOr vice versa, then p and q are said to be distinguishable by a string w.
  70. 70. Procedure: mark1- Remove all inaccessible states2- Consider all pairs of states (p, q) .If p F and q F or vice versa mark the pair (p, q) as distinguishable.3-Repeat the following step until no previously unmarked pairs are marked.For all pairs (p, q) and all a compute (p,a)=pa and (q,a) = qa .If (pa, qq) are distinguishable , mark (p, q) distinguishable.
  71. 71. Procedure Reduce1- Use procedure mark to find all pairs of distinguishable states.Then from this, find the sets of all indistinguishable states , say{qi,qj…qk},{ql, qm …qn} etc,2-For each set {qi,qj…qk} of such indistinguishable states, create a state labeled ij..k for the new machine M’3-For each transition rule of M of the form- (qr, a) = qp find the sets to which qr and qp belong.
  72. 72. If qp {ql, qm,…qn}And qr (qi, qj,…qk} add to ’ a rule ’ (ij…k, a) = lm…n4- The initial state q0’ is that state of M’ whose label includes the q0.initial state of M5- F’ is the set of all states whose label contain I such that qi F
  73. 73. Example 1 0,1 0 1 0 00 1 4 2 1 0 1 3
  74. 74. Equivalent FA 0 0,1 0,10 123 1 4
  75. 75. 0 1 00 1 1 1 2 0
  76. 76. 0XX0,1 10,1XX0
  77. 77. Exampleminimize 4 0 1 0 0,1 0 1 2 0 3 1 0,1 5 1 0,1
  78. 78. Minimal state FA 0,1001 35 1
  79. 79. Example 0 4 0 2 0 1 0 11 5 1 1 3 0 0 6 1 1 0 7 1
  80. 80. Min state FA 1 0 1 0124 357 6 1 0
  81. 81. L1= L3 = 1 L5 = * 01 L2= 0 *L4 = 00 L7 = * 11 L6 = l = *10
  82. 82. Lex
  83. 83. Lex: a lexical analyzer• A Lex program recognizes strings• For each kind of string found the lex program takes an action
  84. 84. Output Identifier: Var Input Operand: =Var = 12 + 9; Integer: 12if (test > 20) Operand: + temp = 0; Integer: 9 Lex Semicolumn: ;else program while (a < 20) Keyword: if temp++; Parenthesis: ( Identifier: test ....
  85. 85. In Lex strings are described with regular expressions Lex programRegular expressions “+” “-” /* operators */ “=“ “if” /* keywords */ “then”
  86. 86. Lex programRegular expressions(0|1|2|3|4|5|6|7|8|9)+ /* integers */(a|b|..|z|A|B|...|Z)+ /* identifiers */
  87. 87. Internal Structure of Lex LexRegular Minimal NFA DFAexpressions DFA The final states of the DFA are associated with actions
  88. 88. Lex matches the longest input string Example: Regular Expressions “if” “ifend” Input: ifend if ifnMatches: “ifend” “if” nomatch
  89. 89. Input Output Integer1234 test Identifiervar 566 78 Identifier9800 + Integer Integertemp Integer Error in line: 3 Identifier

×