Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | 142x 142x 142x 142x 142x 142x 80x 323x 98x 62x 36x 36x 36x 36x 225x 20x 205x 20x 18x 18x 18x 62x 102x 18x 18x 18x 18x 82x 2x 80x 20x 82x 18x 18x 18x 18x 18x 18x 18x 84x 22x 22x 22x 22x 22x 62x 1x 1x 1x 1x 1x 1x 1x 1x 61x 61x 5x 5x 5x 56x 56x 56x 56x 56x 62x 52x 10x 10x 28x 26x 26x 26x 138x 138x 138x 26x 76x 56x 56x 76x 3x 3x 38x 38x 8x 8x 12x 12x 12x 8x 8x 13x 26x 26x 26x 26x 13x 14x 14x 14x 14x 14x 14x 76x 68x 68x 76x 16x 16x 16x 16x 16x 16x 48x 48x 48x 48x 48x 48x 48x 48x 48x 285x 285x 285x 339x 237x 237x 237x 237x 44x 102x 102x 539x 48x 48x 285x 42x 42x 270x 270x 334x 42x 42x 42x 22x 22x 22x 22x 22x 22x 22x 22x 6x 6x 6x 6x 6x 6x 6x 6x 22x 22x 22x 42x 42x 16x 16x 16x 16x 42x 42x 6x 6x 6x 6x 6x 6x 6x 6x 6x 22x 22x 22x 22x 42x 42x 13x 42x 42x 42x 16x 42x 16x 42x 42x 16x 16x 6x 6x 6x 6x 6x 6x 6x 6x 6x 22x 18x 4x 6x 6x 6x 6x 6x 3x 3x 3x 6x 11x 11x 11x 11x 11x 26x 26x 26x 66x 18x 52x 26x 26x 52x 52x 52x 52x 114x 52x 62x 52x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 6x 6x 6x 6x 6x 7x 7x 3x 6x 6x 6x 6x 6x 11x 11x 22x 22x 11x 11x 6x 16x 16x 42x 11x 42x 6x 11x 11x 11x 11x 11x 6x 6x 6x 6x 6x 6x 6x | /* eslint-disable @typescript-eslint/no-non-null-assertion */ export { parseRegex, regexToNfa, nfaToDfa, minDfa, State, } type Node = { begin: number; end: number; type?: string; text?: string; parts?: Node[]; sub?: Node; } type State = { key?: string; items?: State[]; symbols?: string[]; type?: string; edges?: [string, State][]; id?: string; trans?: Record<string, State>; } /** * Try parsing simple regular expression to syntax tree. * * Basic grammars: * Empty: S -> ϵ * Cat: S -> S S * Or: S -> S | S * Star: S -> S * * Text: S -> [0-9a-zA-Z] * S -> ( S ) * * Extension: * Plus: S -> S + -> S S * * Ques: S -> S ? -> (S | ϵ) * * @param {string} text The input regular expression * @return {string|object} Returns a string that is an error message if failed to parse the expression, * otherwise returns an object which is the syntax tree. * * Edited from https://github.com/CyberZHG/toolbox/blob/gh-pages/js/lexical.js */ function parseRegex(text: string) { function parseSub(text: string[], begin: number, end: number, first: boolean): string | Node { let i: number, sub, last = 0, node: Node = { begin: begin, end: end }, virNode: Node, tempNode: Node, stack = 0, parts: Node[] = []; Iif (text.length === 0) { return "Error: empty input at " + begin.toString() + "."; } if (first) { for (i = 0; i <= text.length; i += 1) { if (i === text.length || (text[i] === "|" && stack === 0)) { if (last === 0 && i === text.length) { return parseSub(text, begin + last, begin + i, false); } sub = parseSub(text.slice(last, i), begin + last, begin + i, true); Iif (typeof sub === "string") { return sub; } parts.push(sub); last = i + 1; } else if (text[i] === "(") { stack += 1; } else if (text[i] === ")") { stack -= 1; } } Iif (parts.length === 1) { return parts[0]; } node.type = "or"; node.parts = parts; } else { for (i = 0; i < text.length; i += 1) { if (text[i] === "(") { last = i + 1; i += 1; stack = 1; while (i < text.length && stack !== 0) { if (text[i] === "(") { stack += 1; } else if (text[i] === ")") { stack -= 1; } i += 1; } Iif (stack !== 0) { return "Error: missing right bracket for " + (begin + last).toString() + "."; } i -= 1; sub = parseSub(text.slice(last, i), begin + last, begin + i, true); Iif (typeof sub === "string") { return sub; } sub.begin -= 1; sub.end += 1; parts.push(sub); } else if (text[i] === "*") { Iif (parts.length === 0) { return "Error: unexpected * at " + (begin + i).toString() + "."; } tempNode = { begin: parts[parts.length - 1].begin, end: parts[parts.length - 1].end + 1 }; tempNode.type = "star"; tempNode.sub = parts[parts.length - 1]; parts[parts.length - 1] = tempNode; } else if (text[i] === "+") { Iif (parts.length === 0) { return "Error: unexpected + at " + (begin + i).toString() + "."; } virNode = { begin: parts[parts.length - 1].begin, end: parts[parts.length - 1].end + 1 }; virNode.type = "star"; virNode.sub = parts[parts.length - 1]; tempNode = { begin: parts[parts.length - 1].begin, end: parts[parts.length - 1].end + 1 }; tempNode.type = "cat"; tempNode.parts = [parts[parts.length - 1], virNode]; parts[parts.length - 1] = tempNode; } else Iif (text[i] === "?") { Iif (parts.length === 0) { return "Error: unexpected ? at " + (begin + i).toString() + "."; } virNode = { begin: parts[parts.length - 1].begin, end: parts[parts.length - 1].end + 1 }; virNode.type = "empty"; virNode.sub = parts[parts.length - 1]; tempNode = { begin: parts[parts.length - 1].begin, end: parts[parts.length - 1].end + 1 }; tempNode.type = "or"; tempNode.parts = [parts[parts.length - 1], virNode]; parts[parts.length - 1] = tempNode; } else if (text[i] === "ϵ") { tempNode = { begin: begin + i, end: begin + i + 1 }; tempNode.type = "empty"; parts.push(tempNode); } else Iif (text[i].length == 2) { tempNode = { begin: begin + i, end: begin + i + 1 }; tempNode.type = "text"; tempNode.text = text[i][0]; parts.push(tempNode); } else { tempNode = { begin: begin + i, end: begin + i + 1 }; tempNode.type = "text"; tempNode.text = text[i]; parts.push(tempNode); } } if (parts.length === 1) { return parts[0]; } node.type = "cat"; node.parts = parts; } return node; } let new_text: string[] = []; let i = 0; while (i < text.length) { Iif (text[i] === "\\") { const escapeMap: Record<string, string> = { n: "\n", r: "\r", t: "\t", v: "\v", f: "\f", "^": String.fromCharCode(128) }; const char = text[i + 1]; new_text.push(escapeMap[char] || char.repeat(2)); i += 2; } else { new_text.push(text[i]); i += 1; } } return parseSub(new_text, 0, new_text.length, true); } /** * Convert regular expression to nondeterministic finite automaton. * * @param {string} text @see parseRegex() * @return {State|string} */ function regexToNfa(text: string) { function generateGraph(node: Node, start: State, end: State, count: number) { let i: number, last: State, temp: State, tempStart: State, tempEnd: State; if (!Object.prototype.hasOwnProperty.call(start, "id")) { start.id = count.toString(); count += 1; } switch (node.type) { case "empty": start.edges!.push(["ϵ", end]); break; case "text": start.edges!.push([node.text!, end]); break; case "cat": last = start; for (i = 0; i < node.parts!.length - 1; i += 1) { temp = { type: "", edges: [] }; count = generateGraph(node.parts![i], last, temp, count); last = temp; } count = generateGraph(node.parts![node.parts!.length - 1], last, end, count); break; case "or": for (i = 0; i < node.parts!.length; i += 1) { tempStart = { type: "", edges: [] }; tempEnd = { type: "", edges: [["ϵ", end]] }; start.edges!.push(["ϵ", tempStart]); count = generateGraph(node.parts![i], tempStart, tempEnd, count); } break; case "star": tempStart = { type: "", edges: [] }; tempEnd = { type: "", edges: [ ["ϵ", tempStart], ["ϵ", end], ], }; start.edges!.push(["ϵ", tempStart]); start.edges!.push(["ϵ", end]); count = generateGraph(node.sub!, tempStart, tempEnd, count); break; } if (!Object.prototype.hasOwnProperty.call(end, "id")) { end.id = count.toString(); count += 1; } return count; } let ast = parseRegex(text), start: State = { type: "start", edges: [] }, accept: State = { type: "accept", edges: [] }; Iif (typeof ast === "string") { return ast; } generateGraph(ast, start, accept, 0); return start; } /** * Convert nondeterministic finite automaton to deterministic finite automaton. * * @param {State} nfa @see regexToNfa(), the function assumes that the given NFA is valid. * @return {State} dfa Returns the first element of the DFA. */ function nfaToDfa(nfa: State): State { function getClosure(nodes: State[]): State { let i: number, closure: State[] = [], stack: State[] = [], symbols: string[] = [], type = "", top: State; for (i = 0; i < nodes.length; i += 1) { stack.push(nodes[i]); closure.push(nodes[i]); Iif (nodes[i].type === "accept") { type = "accept"; } } while (stack.length > 0) { top = stack.pop()!; // If top is of type string and starts with "Error" then return error Iif (typeof top === "string" && top[0] === "E") { console.log(top); continue; } for (i = 0; i < top.edges!.length; i += 1) { if (top.edges![i][0] === "ϵ") { if (closure.indexOf(top.edges![i][1]) < 0) { stack.push(top.edges![i][1]); closure.push(top.edges![i][1]); if (top.edges![i][1].type === "accept") { type = "accept"; } } } else { if (symbols.indexOf(top.edges![i][0]) < 0) { symbols.push(top.edges![i][0]); } } } } closure.sort((a: State, b: State) => Number(a.id)! - Number(b.id)!); symbols.sort(); return { key: closure .map((x) => x.id) .join(","), items: closure, symbols: symbols, type: type, edges: [], trans: {}, }; } function getClosedMove(closure: State, symbol: string) { let node: State, nexts: State[] = []; for (let i = 0; i < closure.items!.length; i += 1) { node = closure.items![i]; for (let j = 0; j < node.edges!.length; j += 1) { if (symbol === node.edges![j][0]) { if (nexts.indexOf(node.edges![j][1]) < 0) { nexts.push(node.edges![j][1]); } } } } return getClosure(nexts); } function toAlphaCount(n: number) { let a = "A".charCodeAt(0), z = "Z".charCodeAt(0), len = z - a + 1, s = ""; while (n >= 0) { s = String.fromCharCode((n % len) + a) + s; n = Math.floor(n / len) - 1; } return s; } let i, first = getClosure([nfa]), states: Record<string, State> = {}, front = 0, top: State, closure: State, queue = [first], count = 0; first.id = toAlphaCount(count); states[first.key!] = first; while (front < queue.length) { top = queue[front]; front += 1; for (i = 0; i < top.symbols!.length; i += 1) { closure = getClosedMove(top, top.symbols![i]); if (!Object.prototype.hasOwnProperty.call(states, closure.key!)) { count += 1; closure.id = toAlphaCount(count); states[closure.key!] = closure; queue.push(closure); } top.trans![top.symbols![i]] = states[closure.key!]; top.edges!.push([top.symbols![i], states[closure.key!]]); } } return first; } /** * Convert the DFA to its minimum form using Hopcroft's algorithm. * * @param {State} dfa @see nfaToDfa(), the function assumes that the given DFA is valid. * @return {State} dfa Returns the first element of the minimum DFA. */ function minDfa(dfa: State) { function getReverseEdges(start: State): [string[], Record<string, State>, Record<string, Record<string, string[]>>] { let i: number, top: State, symbol: string, next: State | undefined, front = 0, queue: State[] = [start], visited: Record<string, boolean> = {}, symbols: Record<string, boolean> = {}, // The input alphabet idMap: Record<string, State> = {}, // Map id to states revEdges: Record<string, Record<string, string[]>> = {}; // Map id to the ids which connects to the id with an alphabet visited[start.id!] = true; while (front < queue.length) { top = queue[front]; front += 1; idMap[top.id!] = top; for (i = 0; i < top.symbols!.length; i += 1) { symbol = top.symbols![i]; if (!Object.prototype.hasOwnProperty.call(symbols, symbol)) { symbols[symbol] = true; } next = top.trans![symbol]; if (next) { if (!Object.prototype.hasOwnProperty.call(revEdges, next.id!)) { revEdges[next.id!] = {}; } if (!Object.prototype.hasOwnProperty.call(revEdges[next.id!], symbol)) { revEdges[next.id!][symbol] = []; } revEdges[next.id!][symbol].push(top.id!); if (!Object.prototype.hasOwnProperty.call(visited, next.id!)) { visited[next.id!] = true; queue.push(next); } } } } return [Object.keys(symbols), idMap, revEdges]; } function hopcroft( symbols: string[], idMap: Record<string, State>, revEdges: Record<string, Record<string, string[]>> ) { let i: number, j: number, k: number, keys: string[], key: string, key1: string, key2: string, top: string | string[], group1: string[], group2: string[], symbol: string, revGroup: Record<string, boolean>, ids = Object.keys(idMap).sort(), partitions: Record<string, string[]> = {}, front = 0, queue: (string | null)[] = [], visited: Record<string, number> = {}; group1 = []; group2 = []; for (i = 0; i < ids.length; i += 1) { if (idMap[ids[i]].type === 'accept') { group1.push(ids[i]); } else { group2.push(ids[i]); } } key = group1.join(','); partitions[key] = group1; queue.push(key); visited[key] = 0; if (group2.length !== 0) { key = group2.join(','); partitions[key] = group2; queue.push(key); } while (front < queue.length) { top = queue[front] as string; front += 1; if (top) { top = top.split(','); for (i = 0; i < symbols.length; i += 1) { symbol = symbols[i]; revGroup = {}; for (j = 0; j < top.length; j += 1) { if (Object.prototype.hasOwnProperty.call(revEdges, top[j]) && Object.prototype.hasOwnProperty.call(revEdges[top[j]], symbol)) { for (k = 0; k < revEdges[top[j]][symbol].length; k += 1) { revGroup[revEdges[top[j]][symbol][k]] = true; } } } keys = Object.keys(partitions); for (j = 0; j < keys.length; j += 1) { key = keys[j]; group1 = []; group2 = []; for (k = 0; k < partitions[key].length; k += 1) { if (Object.prototype.hasOwnProperty.call(revGroup, partitions[key][k])) { group1.push(partitions[key][k]); } else { group2.push(partitions[key][k]); } } if (group1.length !== 0 && group2.length !== 0) { delete partitions[key]; key1 = group1.join(','); key2 = group2.join(','); partitions[key1] = group1; partitions[key2] = group2; Iif (Object.prototype.hasOwnProperty.call(visited, key1)) { queue[visited[key1]] = null; visited[key1] = queue.length; queue.push(key1); visited[key2] = queue.length; queue.push(key2); } else if (group1.length <= group2.length) { visited[key1] = queue.length; queue.push(key1); } else { visited[key2] = queue.length; queue.push(key2); } } } } } } return Object.values(partitions); } function buildMinNfa( start: State, partitions: string[][], idMap: Record<string, State>, revEdges: Record<string, Record<string, string[]>> ) { let i: number, j: number, temp: string[], node: State, symbol: string, nodes: State[] = [], group: Record<string, number> = {}, edges: Record<string, Record<string, Record<string, boolean>>> = {}; partitions.sort((a, b) => { let ka = a.join(','), kb = b.join(','); if (ka < kb) return -1; if (ka > kb) return 1; return 0; }); for (i = 0; i < partitions.length; i += 1) { if (partitions[i].indexOf(start.id!) >= 0) { Iif (i > 0) { temp = partitions[i]; partitions[i] = partitions[0]; partitions[0] = temp; } break; } } for (i = 0; i < partitions.length; i += 1) { node = { id: (i + 1).toString(), key: partitions[i].join(','), items: [], symbols: [], type: idMap[partitions[i][0]].type, edges: [], trans: {}, }; for (j = 0; j < partitions[i].length; j += 1) { node.items!.push(idMap[partitions[i][j]]); group[partitions[i][j]] = i; } edges[i] = {}; nodes.push(node); } Object.keys(revEdges).forEach((to) => { Object.keys(revEdges[to]).forEach((symbol) => { revEdges[to][symbol].forEach((from) => { if (!Object.prototype.hasOwnProperty.call(edges[group[from]], group[to])) { edges[group[from]][group[to]] = {}; } edges[group[from]][group[to]][symbol] = true; }); }); }); Object.keys(edges).forEach((from) => { Object.keys(edges[from]).forEach((to) => { symbol = Object.keys(edges[from][to]).sort().join(','); nodes[parseInt(from)].symbols!.push(symbol); nodes[parseInt(from)].edges!.push([symbol, nodes[parseInt(to)]]); nodes[parseInt(from)].trans![symbol] = nodes[parseInt(to)]; }); }); return nodes[0]; } let edgesTuple = getReverseEdges(dfa), symbols = edgesTuple[0], idMap = edgesTuple[1], revEdges = edgesTuple[2], partitions = hopcroft(symbols, idMap, revEdges); return buildMinNfa(dfa, partitions, idMap, revEdges); } |