| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Thu, 19 Sep 2024 19:21:52 +0100 | |
| changeset 962 | 2ecd511e62fa | 
| parent 961 | 4543807efe9d | 
| child 963 | 4e3f7b3574a9 | 
| permissions | -rw-r--r-- | 
| 631 | 1 | % !TEX program = xelatex | 
| 0 | 2 | \documentclass{article}
 | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 3 | \usepackage{../style}
 | 
| 0 | 4 | |
| 5 | \begin{document}
 | |
| 6 | ||
| 7 | \section*{Homework 1}
 | |
| 8 | ||
| 331 
a2c18456c6b7
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
294diff
changeset | 9 | \HEADER | 
| 
a2c18456c6b7
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
294diff
changeset | 10 | |
| 0 | 11 | \begin{enumerate}
 | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 12 | |
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 13 | \item {\bf (Optional)} If you want to run the code presented
 | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 14 | in the lectures, install the Scala programming language | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 15 | available (for free) from | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 16 | |
| 743 | 17 |       \begin{center}
 | 
| 18 |         \url{http://www.scala-lang.org}
 | |
| 19 |       \end{center}
 | |
| 20 | ||
| 961 | 21 | % and the Ammonite REPL from | 
| 22 | % | |
| 23 |       % \begin{center}
 | |
| 24 |       % \url{https://ammonite.io}
 | |
| 25 |       % \end{center}      
 | |
| 0 | 26 | |
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 27 | If you want to follow the code I present during the | 
| 961 | 28 | lectures, it might be useful to install VS Code or Codium. | 
| 29 |       I will be using Scala Version 3.5, which has the \texttt{scala-cli}
 | |
| 30 | REPL used in PEP already built in. | |
| 31 | ||
| 32 | %handout about Scala. | |
| 33 | %Make sure Ammonite | |
| 34 | %uses the Scala 3 compiler. | |
| 0 | 35 | |
| 639 | 36 | %\item {\bf (Optional)} Have a look at the crawler programs.
 | 
| 37 | % Can you find a usage for them in your daily programming | |
| 38 | % life? Can you improve them? For example in cases there | |
| 39 | % are links that appear on different recursion levels, the | |
| 40 | % crawlers visit such web-pages several times. Can this be | |
| 41 | % avoided? Also, the crawlers flag as problematic any page | |
| 42 | % that gives an error, but probably only 404 Not Found | |
| 43 | % errors should be flagged. Can you change that?) | |
| 104 
ffde837b1db1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
102diff
changeset | 44 | |
| 640 | 45 | \item {\bf (Optional)} Have a look at the catastrophic backtracking
 | 
| 46 | programs uploaded on KEATS. Convince yourself that they really require | |
| 47 | a lot of computation time. If you have similar examples in your own | |
| 48 | favourite programming language, I am happy to hear about it. | |
| 49 | ||
| 50 | ||
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 51 | \item Read the handout of the first lecture and the handout | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 52 | about notation. Make sure you understand the concepts of | 
| 498 | 53 | strings and languages. In the context of the CFL-course, | 
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 54 |       what is meant by the term \emph{language}?
 | 
| 9 | 55 | |
| 876 | 56 |       \solution{A language - in this context - is just a set of
 | 
| 57 | strings. Some of these sets can actually not be described by | |
| 58 | regular expressions. Only regular​ languages can. This is | |
| 59 | something for lecture 3.} | |
| 60 | ||
| 550 | 61 | \item Give the definition for regular expressions---this is an | 
| 498 | 62 | inductive datatype. What is the | 
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 63 | meaning of a regular expression? (Hint: The meaning is | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 64 | defined recursively.) | 
| 0 | 65 | |
| 876 | 66 |       \solution{Here I would also expect the grammar for basic regular
 | 
| 67 | expressions and the definition of the recursive L-function. Discuss | |
| 68 | differences between $r_1 + r_2$ and $r^+$. Discuss differences between | |
| 69 | ``real-life regexes'' and regexes in this module.} | |
| 70 | ||
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 71 | \item Assume the concatenation operation of two strings is | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 72 | written as $s_1 @ s_2$. Define the operation of | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 73 |       \emph{concatenating} two sets of strings. This operation
 | 
| 394 
2f9fe225ecc8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
355diff
changeset | 74 | is also written as $\_ \,@\, \_$. According to | 
| 
2f9fe225ecc8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
355diff
changeset | 75 |       this definition, what is $A \,@\, \{\}$ equal to?
 | 
| 498 | 76 | Is in general $A\,@\,B$ equal to $B\,@\,A$? | 
| 0 | 77 | |
| 876 | 78 |       \solution{ What is $A @ {[]}$? Are there special cases
 | 
| 961 | 79 | where $A @ B = B @ A$? Obviously when $A = B$ the stament is true. | 
| 80 |         But there are also cases when $A \not= B$, for example $A = \{a\}$
 | |
| 81 |       and $B = \{aaa\}$.}
 | |
| 876 | 82 | |
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 83 | \item Assume a set $A$ contains 4 strings and a set $B$ | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 84 | contains 7 strings. None of the strings is the empty | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 85 | string. How many strings are in $A \,@\, B$? | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 86 | |
| 961 | 87 |       \solution{Everyone will probably answer with 28, but there are corner cases where there are fewer
 | 
| 876 | 88 | than 28 elements. Can students think of such corner cases? | 
| 89 |       For example $A = \{a, ab, \ldots\}$, $B = \{bc, c,\ldots\}$ }
 | |
| 90 | ||
| 267 
a1544b804d1e
updated homeworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
258diff
changeset | 91 | \item How is the power of a language defined? (Hint: There are two | 
| 
a1544b804d1e
updated homeworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
258diff
changeset | 92 |   rules, one for $\_^0$ and one for $\_^{n+1}$.)
 | 
| 109 
f2a90dda7e3b
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
104diff
changeset | 93 | |
| 876 | 94 |      \solution{Two rules: 0-case and n+1 case.}
 | 
| 95 | ||
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 96 | \item Let $A = \{[a], [b], [c], [d]\}$. (1) How many strings
 | 
| 438 
84608b4b3578
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
416diff
changeset | 97 | are in $A^4$? (2) Consider also the case of $A^4$ where one of | 
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 98 | the strings in $A$ is the empty string, for example $A = | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 99 |       \{[a], [b], [c], []\}$.
 | 
| 293 
ca349cfe3474
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
267diff
changeset | 100 | |
| 876 | 101 |       \solution{121 is correct. But make sure you understand why it is 121
 | 
| 102 | in cases you do not have a computer at your fingertips.} | |
| 103 | ||
| 507 | 104 | \item (1) How many basic regular expressions are there to match | 
| 776 | 105 |       \textbf{only} the string $abcd$? (2) How many if they cannot include
 | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
438diff
changeset | 106 | $\ONE$ and $\ZERO$? (3) How many if they are also not | 
| 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
438diff
changeset | 107 | allowed to contain stars? (4) How many if they are also | 
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 108 | not allowed to contain $\_ + \_$? | 
| 0 | 109 | |
| 961 | 110 |       \solution{1-3 are infinite (tell the idea why and give examples);
 | 
| 111 | 4 is five - remember regexes are trees (that is the main point of the question.} | |
| 876 | 112 | |
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 113 | \item When are two regular expressions equivalent? Can you | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 114 | think of instances where two regular expressions match | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 115 | the same strings, but it is not so obvious that they do? | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 116 | For example $a + b$ and $b + a$ do not count\ldots they | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 117 | obviously match the same strings, namely $[a]$ and | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 118 | $[b]$. | 
| 403 
564f7584eff1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
401diff
changeset | 119 | |
| 876 | 120 |       \solution{for example $r^* = 1 + r \cdot r^*$ for any regular expression $r$.
 | 
| 961 | 121 | Can students think about why this is the case? - this would need a proof.} | 
| 876 | 122 | |
| 416 | 123 | \item What is meant by the notions \emph{evil regular expressions}
 | 
| 726 | 124 |   and by \emph{catastrophic backtracking}?
 | 
| 125 | ||
| 961 | 126 |   \solution{catastrophic backtracking also applies to other regexes,
 | 
| 127 | not just $(a^*)^*b$. Maybe | |
| 128 |     \url{https://www.trevorlasn.com/blog/when-regex-goes-wrong/} is
 | |
| 129 | of help - even the CrowdStrike issue had an underlying problem | |
| 130 | with a regex, though this one was not due to catastrophic | |
| 131 | backtracking.} | |
| 876 | 132 | |
| 726 | 133 | \item Given the regular expression $(a + b)^* \cdot b \cdot (a + b)^*$, | 
| 841 | 134 | which of the following regular expressions are equivalent | 
| 726 | 135 | |
| 136 | \begin{center}
 | |
| 137 | \begin{tabular}{ll}    
 | |
| 138 | 1) & $(ab + bb)^* \cdot (a + b)^*$\\ % no | |
| 139 | 2) & $(a + b)^* \cdot (ba + bb + b) \cdot (a + b)^*$\\ % yes | |
| 140 | 3) & $(a + b)^* \cdot (a + b) \cdot (a + b)^*$ % no | |
| 141 | \end{tabular}
 | |
| 142 | \end{center}
 | |
| 876 | 143 | |
| 144 |   \solution{no, yes (why?), no.}
 | |
| 921 | 145 | |
| 146 | ||
| 147 | \item Given the extended regular expression \texttt{[b-d]a?e+},
 | |
| 148 | what does the equivalent basic regular expression look like? | |
| 726 | 149 | |
| 934 | 150 |   \solution{$(b + c + d) \cdot (a + \ONE) \cdot (e \cdot e^*)$}
 | 
| 921 | 151 | |
| 152 | ||
| 403 
564f7584eff1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
401diff
changeset | 153 | \item \POSTSCRIPT | 
| 0 | 154 | \end{enumerate}
 | 
| 155 | ||
| 156 | \end{document}
 | |
| 157 | ||
| 158 | %%% Local Variables: | |
| 159 | %%% mode: latex | |
| 160 | %%% TeX-master: t | |
| 161 | %%% End: |