dfa.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  7. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. var DFAMinimizer = require('./dfa-minimizer');
  10. var _require = require('../special-symbols'),
  11. EPSILON_CLOSURE = _require.EPSILON_CLOSURE;
  12. /**
  13. * DFA is build by converting from NFA (subset construction).
  14. */
  15. var DFA = function () {
  16. function DFA(nfa) {
  17. _classCallCheck(this, DFA);
  18. this._nfa = nfa;
  19. }
  20. /**
  21. * Minimizes DFA.
  22. */
  23. _createClass(DFA, [{
  24. key: 'minimize',
  25. value: function minimize() {
  26. this.getTransitionTable();
  27. this._originalAcceptingStateNumbers = this._acceptingStateNumbers;
  28. this._originalTransitionTable = this._transitionTable;
  29. DFAMinimizer.minimize(this);
  30. }
  31. /**
  32. * Returns alphabet for this DFA.
  33. */
  34. }, {
  35. key: 'getAlphabet',
  36. value: function getAlphabet() {
  37. return this._nfa.getAlphabet();
  38. }
  39. /**
  40. * Returns accepting states.
  41. */
  42. }, {
  43. key: 'getAcceptingStateNumbers',
  44. value: function getAcceptingStateNumbers() {
  45. if (!this._acceptingStateNumbers) {
  46. // Accepting states are determined during table construction.
  47. this.getTransitionTable();
  48. }
  49. return this._acceptingStateNumbers;
  50. }
  51. /**
  52. * Returns original accepting states.
  53. */
  54. }, {
  55. key: 'getOriginaAcceptingStateNumbers',
  56. value: function getOriginaAcceptingStateNumbers() {
  57. if (!this._originalAcceptingStateNumbers) {
  58. // Accepting states are determined during table construction.
  59. this.getTransitionTable();
  60. }
  61. return this._originalAcceptingStateNumbers;
  62. }
  63. /**
  64. * Sets transition table.
  65. */
  66. }, {
  67. key: 'setTransitionTable',
  68. value: function setTransitionTable(table) {
  69. this._transitionTable = table;
  70. }
  71. /**
  72. * Sets accepting states.
  73. */
  74. }, {
  75. key: 'setAcceptingStateNumbers',
  76. value: function setAcceptingStateNumbers(stateNumbers) {
  77. this._acceptingStateNumbers = stateNumbers;
  78. }
  79. /**
  80. * DFA transition table is built from NFA table.
  81. */
  82. }, {
  83. key: 'getTransitionTable',
  84. value: function getTransitionTable() {
  85. var _this = this;
  86. if (this._transitionTable) {
  87. return this._transitionTable;
  88. }
  89. // Calculate from NFA transition table.
  90. var nfaTable = this._nfa.getTransitionTable();
  91. var nfaStates = Object.keys(nfaTable);
  92. this._acceptingStateNumbers = new Set();
  93. // Start state of DFA is E(S[nfa])
  94. var startState = nfaTable[nfaStates[0]][EPSILON_CLOSURE];
  95. // Init the worklist (states which should be in the DFA).
  96. var worklist = [startState];
  97. var alphabet = this.getAlphabet();
  98. var nfaAcceptingStates = this._nfa.getAcceptingStateNumbers();
  99. var dfaTable = {};
  100. // Determine whether the combined DFA state is accepting.
  101. var updateAcceptingStates = function updateAcceptingStates(states) {
  102. var _iteratorNormalCompletion = true;
  103. var _didIteratorError = false;
  104. var _iteratorError = undefined;
  105. try {
  106. for (var _iterator = nfaAcceptingStates[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  107. var nfaAcceptingState = _step.value;
  108. // If any of the states from NFA is accepting, DFA's
  109. // state is accepting as well.
  110. if (states.indexOf(nfaAcceptingState) !== -1) {
  111. _this._acceptingStateNumbers.add(states.join(','));
  112. break;
  113. }
  114. }
  115. } catch (err) {
  116. _didIteratorError = true;
  117. _iteratorError = err;
  118. } finally {
  119. try {
  120. if (!_iteratorNormalCompletion && _iterator.return) {
  121. _iterator.return();
  122. }
  123. } finally {
  124. if (_didIteratorError) {
  125. throw _iteratorError;
  126. }
  127. }
  128. }
  129. };
  130. while (worklist.length > 0) {
  131. var states = worklist.shift();
  132. var dfaStateLabel = states.join(',');
  133. dfaTable[dfaStateLabel] = {};
  134. var _iteratorNormalCompletion2 = true;
  135. var _didIteratorError2 = false;
  136. var _iteratorError2 = undefined;
  137. try {
  138. for (var _iterator2 = alphabet[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  139. var symbol = _step2.value;
  140. var onSymbol = [];
  141. // Determine whether the combined state is accepting.
  142. updateAcceptingStates(states);
  143. var _iteratorNormalCompletion3 = true;
  144. var _didIteratorError3 = false;
  145. var _iteratorError3 = undefined;
  146. try {
  147. for (var _iterator3 = states[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  148. var state = _step3.value;
  149. var nfaStatesOnSymbol = nfaTable[state][symbol];
  150. if (!nfaStatesOnSymbol) {
  151. continue;
  152. }
  153. var _iteratorNormalCompletion4 = true;
  154. var _didIteratorError4 = false;
  155. var _iteratorError4 = undefined;
  156. try {
  157. for (var _iterator4 = nfaStatesOnSymbol[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  158. var nfaStateOnSymbol = _step4.value;
  159. if (!nfaTable[nfaStateOnSymbol]) {
  160. continue;
  161. }
  162. onSymbol.push.apply(onSymbol, _toConsumableArray(nfaTable[nfaStateOnSymbol][EPSILON_CLOSURE]));
  163. }
  164. } catch (err) {
  165. _didIteratorError4 = true;
  166. _iteratorError4 = err;
  167. } finally {
  168. try {
  169. if (!_iteratorNormalCompletion4 && _iterator4.return) {
  170. _iterator4.return();
  171. }
  172. } finally {
  173. if (_didIteratorError4) {
  174. throw _iteratorError4;
  175. }
  176. }
  177. }
  178. }
  179. } catch (err) {
  180. _didIteratorError3 = true;
  181. _iteratorError3 = err;
  182. } finally {
  183. try {
  184. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  185. _iterator3.return();
  186. }
  187. } finally {
  188. if (_didIteratorError3) {
  189. throw _iteratorError3;
  190. }
  191. }
  192. }
  193. var dfaStatesOnSymbolSet = new Set(onSymbol);
  194. var dfaStatesOnSymbol = [].concat(_toConsumableArray(dfaStatesOnSymbolSet));
  195. if (dfaStatesOnSymbol.length > 0) {
  196. var dfaOnSymbolStr = dfaStatesOnSymbol.join(',');
  197. dfaTable[dfaStateLabel][symbol] = dfaOnSymbolStr;
  198. if (!dfaTable.hasOwnProperty(dfaOnSymbolStr)) {
  199. worklist.unshift(dfaStatesOnSymbol);
  200. }
  201. }
  202. }
  203. } catch (err) {
  204. _didIteratorError2 = true;
  205. _iteratorError2 = err;
  206. } finally {
  207. try {
  208. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  209. _iterator2.return();
  210. }
  211. } finally {
  212. if (_didIteratorError2) {
  213. throw _iteratorError2;
  214. }
  215. }
  216. }
  217. }
  218. return this._transitionTable = this._remapStateNumbers(dfaTable);
  219. }
  220. /**
  221. * Remaps state numbers in the resulting table:
  222. * combined states '1,2,3' -> 1, '3,4' -> 2, etc.
  223. */
  224. }, {
  225. key: '_remapStateNumbers',
  226. value: function _remapStateNumbers(calculatedDFATable) {
  227. var newStatesMap = {};
  228. this._originalTransitionTable = calculatedDFATable;
  229. var transitionTable = {};
  230. Object.keys(calculatedDFATable).forEach(function (originalNumber, newNumber) {
  231. newStatesMap[originalNumber] = newNumber + 1;
  232. });
  233. for (var originalNumber in calculatedDFATable) {
  234. var originalRow = calculatedDFATable[originalNumber];
  235. var row = {};
  236. for (var symbol in originalRow) {
  237. row[symbol] = newStatesMap[originalRow[symbol]];
  238. }
  239. transitionTable[newStatesMap[originalNumber]] = row;
  240. }
  241. // Remap accepting states.
  242. this._originalAcceptingStateNumbers = this._acceptingStateNumbers;
  243. this._acceptingStateNumbers = new Set();
  244. var _iteratorNormalCompletion5 = true;
  245. var _didIteratorError5 = false;
  246. var _iteratorError5 = undefined;
  247. try {
  248. for (var _iterator5 = this._originalAcceptingStateNumbers[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
  249. var _originalNumber = _step5.value;
  250. this._acceptingStateNumbers.add(newStatesMap[_originalNumber]);
  251. }
  252. } catch (err) {
  253. _didIteratorError5 = true;
  254. _iteratorError5 = err;
  255. } finally {
  256. try {
  257. if (!_iteratorNormalCompletion5 && _iterator5.return) {
  258. _iterator5.return();
  259. }
  260. } finally {
  261. if (_didIteratorError5) {
  262. throw _iteratorError5;
  263. }
  264. }
  265. }
  266. return transitionTable;
  267. }
  268. /**
  269. * Returns original DFA table, where state numbers
  270. * are combined numbers from NFA.
  271. */
  272. }, {
  273. key: 'getOriginalTransitionTable',
  274. value: function getOriginalTransitionTable() {
  275. if (!this._originalTransitionTable) {
  276. // Original table is determined during table construction.
  277. this.getTransitionTable();
  278. }
  279. return this._originalTransitionTable;
  280. }
  281. /**
  282. * Checks whether this DFA accepts a string.
  283. */
  284. }, {
  285. key: 'matches',
  286. value: function matches(string) {
  287. var state = 1;
  288. var i = 0;
  289. var table = this.getTransitionTable();
  290. while (string[i]) {
  291. state = table[state][string[i++]];
  292. if (!state) {
  293. return false;
  294. }
  295. }
  296. if (!this.getAcceptingStateNumbers().has(state)) {
  297. return false;
  298. }
  299. return true;
  300. }
  301. }]);
  302. return DFA;
  303. }();
  304. module.exports = DFA;