{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Week 3: Regular expressions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from tock import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Monday reading\n", "\n", "Read Section 1.3, but you can save Lemma 1.60 and its proof for next time." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuesday class\n", "\n", "## Regular expressions\n", "\n", "Regular expressions were invented by Stephen Kleene (pronounced clay-knee) back in the 1950s as a characterization of the languages recognized by the earliest neural networks. But they became widely used through various Unix tools, like `grep`, which is where you have likely encountered them.\n", "\n", "![Regex Golf](http://imgs.xkcd.com/comics/regex_golf.png)\n", "\n", "There are some differences between Unix regular expressions and true regular expressions. True regular expressions have three operations: concatenation, union ($\\cup$ in the book, `|` in Unix), and Kleene star (`*`). The order of operations is star, then union, then concatenation. Use parentheses to change the order of operations, just as in arithmetic expressions.\n", "\n", "Unix regular expressions use a dot (`.`) to match any symbol; the book uses $\\Sigma$ for this purpose.\n", "\n", "Another difference is that Unix regular expressions usually match anywhere in a string, whereas true regular expressions usually must match the entire string. When using `grep`, use the `-Ex` flags to approximate true regular expressions (`-E` to get the union operator, `-x` to match the whole line)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%file movies.txt\n", "the phantom menace\n", "attack of the clones\n", "revenge of the sith\n", "a new hope\n", "the empire strikes back\n", "return of the jedi\n", "the force awakens\n", "rogue one\n", "the last jedi\n", "solo\n", "the rise of skywalker\n", "the motion picture\n", "the wrath of khan\n", "the search for spock\n", "the voyage home\n", "the final frontier\n", "the undiscovered country\n", "generations\n", "first contact\n", "insurrection\n", "nemesis\n", "into darkness\n", "beyond" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the phantom menace\r\n", "attack of the clones\r\n", "revenge of the sith\r\n", "a new hope\r\n", "the empire strikes back\r\n", "return of the jedi\r\n", "beyond\r\n" ] } ], "source": [ "!grep -Ex '.*(m | (t|n)|b).*' movies.txt" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Almost right.\n", "\n", "**Question.** Can you fix the regular expression so that it also accepts `the force awakens`, `rogue one`, `the last jedi`, `solo`, and `the rise of skywalker`, but not `beyond`?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting regular expressions to NFAs\n", "\n", "Regular expressions are the third \"model of computation\" of this course, and they, too, are equivalent to both DFAs and NFAs. The proof involves algorithms to convert between regular expressions and NFAs. Today we're converting regular expressions to NFAs, which is not as difficult as the subset construction from last time.\n", "\n", "The algorithm in the book is a slight variation on the algorithm from a paper by Ken Thompson, one of the co-developers of Unix, [for the QED editor](https://swtch.com/~rsc/regexp/regexp1.html), from which are descended `sed`, `grep`, and `vi`. (But many modern regular expression engines actually do not use this algorithm; they use one that is asymptotically much slower!)\n", "\n", "Here's a step by step visualization of the example from the book. You can try different regular expressions to see how they get converted. (Note that in Tock, unlike in Unix tools, symbols in regular expressions are separated by spaces.)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "subexpression: a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "q1\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "q2\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: b" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "1\n", "\n", "q3\n", "\n", "\n", "\n", "_START->1\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "q4\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: a b" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "2\n", "\n", "q1\n", "\n", "\n", "\n", "_START->2\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "q2\n", "\n", "\n", "\n", "1\n", "\n", "q3\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "3\n", "\n", "\n", "q4\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "1\n", "\n", "q5\n", "\n", "\n", "\n", "_START->1\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "q6\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: a b|a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "4\n", "\n", "q7\n", "\n", "\n", "\n", "_START->4\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "q1\n", "\n", "\n", "\n", "1\n", "\n", "q2\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a\n", "\n", "\n", "\n", "2\n", "\n", "q3\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "5\n", "\n", "\n", "q4\n", "\n", "\n", "\n", "2->5\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3\n", "\n", "q5\n", "\n", "\n", "\n", "6\n", "\n", "\n", "q6\n", "\n", "\n", "\n", "3->6\n", "\n", "\n", "a\n", "\n", "\n", "\n", "4->0\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "ε\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: (a b|a)*" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "3\n", "\n", "\n", "q8\n", "\n", "\n", "\n", "_START->3\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "q1\n", "\n", "\n", "\n", "1\n", "\n", "q2\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a\n", "\n", "\n", "\n", "2\n", "\n", "q3\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "7\n", "\n", "\n", "q4\n", "\n", "\n", "\n", "2->7\n", "\n", "\n", "b\n", "\n", "\n", "\n", "5\n", "\n", "q7\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "4\n", "\n", "q5\n", "\n", "\n", "\n", "6\n", "\n", "\n", "q6\n", "\n", "\n", "\n", "4->6\n", "\n", "\n", "a\n", "\n", "\n", "\n", "5->0\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "5->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "6->5\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "7->5\n", "\n", "\n", "ε\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m = from_regexp('(a b|a)*', display_steps=True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "subexpression: a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "1\n", "\n", "q01\n", "\n", "\n", "\n", "_START->1\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "q02\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: b" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "q03\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "q04\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: a|b" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "1\n", "\n", "q05\n", "\n", "\n", "\n", "_START->1\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "q03\n", "\n", "\n", "\n", "3\n", "\n", "\n", "q04\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "b\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "4\n", "\n", "q01\n", "\n", "\n", "\n", "1->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "2\n", "\n", "\n", "q02\n", "\n", "\n", "\n", "4->2\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: (a|b)*" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "3\n", "\n", "\n", "q06\n", "\n", "\n", "\n", "_START->3\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "q03\n", "\n", "\n", "\n", "1\n", "\n", "\n", "q04\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4\n", "\n", "q05\n", "\n", "\n", "\n", "1->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "2\n", "\n", "\n", "q02\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "3->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "4->0\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "5\n", "\n", "q01\n", "\n", "\n", "\n", "4->5\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "5->2\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "1\n", "\n", "q07\n", "\n", "\n", "\n", "_START->1\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "q08\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: b" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "q09\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "q10\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "1\n", "\n", "q11\n", "\n", "\n", "\n", "_START->1\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "q12\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "subexpression: (a|b)* a b a" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "6\n", "\n", "q06\n", "\n", "\n", "\n", "_START->6\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "q04\n", "\n", "\n", "\n", "2\n", "\n", "q05\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "4\n", "\n", "q07\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "q03\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "10\n", "\n", "q01\n", "\n", "\n", "\n", "2->10\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "3\n", "\n", "q11\n", "\n", "\n", "\n", "11\n", "\n", "\n", "q12\n", "\n", "\n", "\n", "3->11\n", "\n", "\n", "a\n", "\n", "\n", "\n", "5\n", "\n", "q08\n", "\n", "\n", "\n", "4->5\n", "\n", "\n", "a\n", "\n", "\n", "\n", "7\n", "\n", "q09\n", "\n", "\n", "\n", "5->7\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "6->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "6->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "8\n", "\n", "q10\n", "\n", "\n", "\n", "7->8\n", "\n", "\n", "b\n", "\n", "\n", "\n", "8->3\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "9\n", "\n", "q02\n", "\n", "\n", "\n", "9->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "9->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "10->9\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m = from_regexp('(a|b)* a b a', display_steps=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question.** If a regular expression has $n$ symbols, how big will the resulting NFA be?\n", "\n", "For a very cool \"real-time\" visualization of (a slightly different version of) this construction, see [Debuggex](https://www.debuggex.com)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Wednesday reading\n", "\n", "Read or reread Lemma 1.60 and its proof, which (in my opinion) is the most complicated one so far." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Thursday class\n", "\n", "Please have your teams for the programming project registered by the end of this week.\n", "\n", "## NFAs to regular expressions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Today we're completing the proof from last time by converting NFAs to regular expressions. The algorithm for this is known as the _state elimination_ algorithm, because it eliminates the states of the NFA one by one. \n", "\n", "To make this construction simpler, the new concept of a generalized NFA (GNFA) is introduced. In this course, GNFAs are a \"throwaway\" formalism; they exist just to make this proof possible. But elsewhere they might be useful in their own right. A GNFA is a NFA whose transitions can be labeled with regular expressions instead of symbols. A GNFA can follow a transition if the next $k \\geq 0$ symbols match the regular expression; when it follows the transition, it consumes those $k$ symbols.\n", "\n", "The algorithm first changes the NFA into a GNFA that has a single start state with no incoming edges and a single accept state with no outgoing edges. This makes the final step of the algorithm simpler. The book also creates a lot of transitions labeled $\\emptyset$; these are not shown in the figures below, and they're not shown in the book, either. In fact, as far as I'm concerned, you can pretend they don't exist.\n", "\n", "The algorithm then eliminates the (non-start, non-accept) states one by one. When there is just one transition left, its label is the final answer. (If you're not using $\\emptyset$ transitions, then there is a special case: if there are no transitions left, then the answer is $\\emptyset$.) The order of elimination is arbitrary, although some orders may lead to more compact regular expressions than others. Tock eliminates states in reverse alphabetical order, for no particularly good reason." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start with a simpler example than the one in the book, a NFA whose state diagram is _acyclic_." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "1\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3\n", "\n", "\n", "4\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a\n", "\n", "\n", "\n", "1\n", "\n", "3\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "c\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m = read_csv('acyclic.csv')\n", "m" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "1\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "4\n", "\n", "2\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "a\n", "\n", "\n", "\n", "5\n", "\n", "4\n", "\n", "\n", "\n", "2->5\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "c\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "b\n", "\n", "\n", "\n", "5->1\n", "\n", "\n", "ε\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 4" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "1\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "a\n", "\n", "\n", "\n", "4\n", "\n", "2\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 3" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "1\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3\n", "\n", "2\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "b c\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "1\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "a|a b c\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 1" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a|a b c\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "a|a b c" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_regexp(m, display_steps=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eliminating 4 is not very interesting; let's skip it for now. Eliminating 3 concatenates the `b` and `c` transitions into a single `bc` transition. Then, eliminating 2 concatenates the `a` and `bc` transitions into a single `abc` transition. But now there are two parallel transitions, from state 1 to the accept state. So these are unioned into a single `a|abc` transition. Finally, eliminating 1 is not very interesting.\n", "\n", "The general procedure for eliminating a state is shown in Figure 1.63. Now here's an example with a cycle in it; these give rise to Kleene stars in the resulting expression." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "1\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a\n", "\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "a\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m66 = read_csv('sipser-1-66.csv')\n", "m66" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "3\n", "\n", "1\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a|b\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "1\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "b (a|b)*\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 1" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a* b (a|b)*\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "a* b (a|b)*" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_regexp(m66, display_steps=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eliminating state 2 looks similar to before, but the self-loops turn into subexpressions inside Kleene stars.\n", "\n", "One more example:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "1\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "3\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2\n", "\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "a\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "b\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "a\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "a\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m68 = read_csv('sipser-1-68.csv')\n", "m68" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "4\n", "\n", "1\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3->4\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4->2\n", "\n", "\n", "a\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 3" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "3\n", "\n", "1\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "a\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a|b a\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "b b\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 2" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "2\n", "\n", "1\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "ε\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "b|(a|b a) b*\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "b b|(a|b a) b* a\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "eliminate 1" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "\n", "_START\n", "\n", "\n", "\n", "0\n", "\n", "start\n", "\n", "\n", "\n", "_START->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "accept\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "(b b|(a|b a) b* a)* (b|(a|b a) b*)\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "(b b|(a|b a) b* a)* (b|(a|b a) b*)" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_regexp(m68, display_steps=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(The answer is different from the book's because the elimination order was different.) The thing to note here is that if there are multiple incoming transitions and/or multiple outgoing transitions, you must consider all possible combinations. For example, when eliminating state 3, there are two outgoing transitions (to states 1 and 2), so a self-loop from 1 to 1 is created, and the transition from 1 to 2 is modified." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 1 }