Finite Automata and Regular Languages

Cd9b247e4507fed75312e9a42070125d?s=47 Tom Stuart
January 27, 2014

Finite Automata and Regular Languages

Cd9b247e4507fed75312e9a42070125d?s=128

Tom Stuart

January 27, 2014
Tweet

Transcript

  1. F I N I T E & R E G

    U L A R A U T O M A T A L A N G U A G E S @tomstuart / London JS / 2014-01-27
  2. deterministic finite automata nondeterministic finite automata regular languages regular expressions

  3. DETERMINISTIC FINITE AUTOMATA

  4. a little imaginary machine with several possible states

  5. 1 2 3

  6. always has a current state

  7. 1 2 3 1

  8. changes state when it reads input

  9. , 1 2 3 1 a b b a a

    b
  10. 1 2 3 a b b , a a b

    a b bb aa 1
  11. 2 3 a b b , a a b a

    b bb aa 2 1
  12. 1 2 3 a b b , a a b

    a b bb aa 2
  13. 1 3 a b b , a a b a

    b bb aa 3 2
  14. 1 2 3 a b b , a a b

    a b bb aa 3
  15. 1 2 3 a b b , a a b

    a b bb aa 3
  16. 1 3 a b b , a a b a

    b bb aa 3 2
  17. some states are "accept states"

  18. , 1 2 3 1 a b b a a

    b
  19. 1 2 3 a b b , a a b

    a b bb aa 1
  20. 2 3 a b b , a a b a

    b bb aa 2 1
  21. 1 2 3 a b b , a a b

    a b bb aa 2
  22. 1 3 a b b , a a b a

    b bb aa 3 2
  23. 1 2 3 a b b , a a b

    a b bb aa 3
  24. 1 2 3 a b b , a a b

    a b bb aa 3
  25. 1 3 a b b , a a b a

    b bb aa 3 2
  26. "accepts" or "rejects" a string

  27. 1 3 a b b , a a b a

    b bb aa 3 2 ✔
  28. , 1 2 3 1 a b b a a

    b
  29. a a 1 2 3 a b b , a

    a b b b bb 1
  30. a a 1 2 3 1 a b , a

    a b b b bb b
  31. a a 1 2 3 1 a b , a

    a b b b bb b
  32. a a 1 2 3 1 a b , a

    a b b b bb b
  33. a a 1 2 3 a b b , a

    a b b b bb 2
  34. a a 1 2 3 a b b , a

    a b b b bb 2
  35. a a 1 2 3 a b b , a

    a b b b bb 2
  36. a a 1 2 3 a b b , a

    a b b b bb ✘ 2
  37. bbbbaa baabba ✔ ✘ ab baba aaaab a baaaa b

    bbbbbb abbbb bbbbab bba ✔ ✔ ✔ ✔ ✔ ✘ ✘ ✘ ✘ ✘
  38. 1 2 3 a b b , a a b

    1
  39. 1 2 3 a b b , a a b

    2
  40. 1 2 3 a b b , a a b

    3
  41. 1 2 3 a b b , a a b

    accepts strings containing "ab"
  42. NONDETERMINISTIC FINITE AUTOMATA

  43. accept strings whose third character is "b"

  44. 1 3 4 5 2 a,b a,b a b a,b

    a,b
  45. accept strings whose third-from-last character is "b"

  46. 1 3 4 2 b a,b a,b a,b

  47. accept strings whose length is a multiple of two or

    three
  48. 1 2 a a

  49. 1 2 a a 3 a

  50. 1 2 a a 4 3 a a a

  51. 4 5 6 a a a 1 2 3 a

    a
  52. nondeterministic finite automata can choose between different rules and change

    state without reading input
  53. but there's an algorithm to convert any nondeterministic finite automaton

    into a deterministic one
  54. 1 3 4 2 b a,b a,b a,b

  55. 1 1,2,4 1,3,4 1,3 b a a 1,2 1,4 1,2,3,4

    1,2,3 b a b a b b a b a b a b a
  56. REGULAR LANGUAGES

  57. 1 2 3 a b b , a a b

  58. bbbbaa baabba ✔ ✘ ab baba aaaab a baaaa b

    bbbbbb abbbb bbbbab bba ✔ ✔ ✔ ✔ ✔ ✘ ✘ ✘ ✘ ✘
  59. baabba ab baba aaaab abbbb bbbbab this is part of

    the language that the machine recognises
  60. some languages are finite, others are infinite

  61. these machines can only recognise regular languages

  62. some regular languages: • the strings "one", "two" and "three"

    • all strings that contain "ab" • all strings whose third character is "b" • all strings whose third-from-last character is "b" • all strings that start and end with "a"
  63. some non-regular languages: • all strings that are palindromes •

    all strings that contain equal numbers of "a" and "b" characters • all strings of brackets that are balanced • all strings that are valid JSON • all strings that contain equal numbers of "a", "b" and "c" characters • all strings whose length is prime • all strings that contain a halting JavaScript program
  64. ( () ✔ ✘ (()) (()(())) ()()()()() (( (() )

    (((())))) ((((())))) (()((()))) (()))() ✔ ✔ ✔ ✔ ✔ ✘ ✘ ✘ ✘ ✘
  65. 0 2 3 1 ( ) ( ) ( )

  66. REGULAR EXPRESSIONS

  67. /dog/ /cat/ /dogs?/ /dog|cat/ /do*g/

  68. /a/

  69. a

  70. /ab/

  71. a b followed by

  72. a b

  73. a b

  74. a b b a

  75. a b b a

  76. a b b a

  77. a b b a

  78. a b b a

  79. a b b a ✔

  80. followed by

  81. None
  82. None
  83. /abc/

  84. c followed by a b

  85. c a b

  86. a b c

  87. /a|b/

  88. a b or

  89. a b

  90. a b a

  91. a b a

  92. a b a

  93. a b a

  94. a b a ✔

  95. a b

  96. a b b

  97. a b b

  98. a b b

  99. a b b

  100. a b b ✔

  101. or

  102. None
  103. /a*/

  104. a zero or more times

  105. a

  106. zero or more times

  107. None
  108. /(a(b|c))*/

  109. /(a(b|c))*/

  110. b c or

  111. b c

  112. /(a(b|c))*/

  113. a followed by c b

  114. a c b

  115. /(a(b|c))*/

  116. zero or more times a c b

  117. a c b

  118. b a c b a a ac b

  119. b a c b a a ac b

  120. b a c b a a ac b

  121. b a c b a a ac b

  122. b a c b a a ac b

  123. b a c b a a ac b

  124. b a c b a a ac b

  125. b a c b a a ac b

  126. b a c b a a ac b

  127. b a c b a a ac b

  128. b a c b a a ac b

  129. b a c b a a ac b

  130. b a c b a a ac b

  131. b a c b a a ac b

  132. b a c b a a ac b

  133. b a c b a a ac b

  134. b a c b a a ac b

  135. b a c b a a ac b ✔

  136. /a+/ = /aa*/ /a?/ = /a|/

  137. every regular expression can be turned into an nondeterministic finite

    automaton
  138. every nondeterministic finite automaton can be turned into a regular

    expression
  139. regular expressions are a compact notation for nondeterministic finite automata

  140. so regular expressions can only recognise regular languages

  141. None
  142. None
  143. balanced = / \A (?<brackets> \( \g<brackets>* \) ) *

    \z /x
  144. thank ! @tomstuart / tom@codon.com / computationbook.com