author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 06 Oct 2014 20:55:16 +0100 | |
changeset 266 | ae039d6ae3f2 |
parent 264 | 4deef8ac5d72 |
child 267 | a1544b804d1e |
permissions | -rw-r--r-- |
31 | 1 |
\documentclass{article} |
264
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
2 |
\usepackage{../style} |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
3 |
|
146
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
4 |
\usepackage{tikz} |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
5 |
\usetikzlibrary{automata} |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
6 |
|
31 | 7 |
|
264
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
8 |
%%\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions |
32 | 9 |
|
31 | 10 |
\begin{document} |
11 |
||
12 |
\section*{Homework 4} |
|
13 |
||
14 |
\begin{enumerate} |
|
34 | 15 |
\item Why is every finite set of strings a regular language? |
16 |
||
42 | 17 |
\item What is the language recognised by the regular expressions $(\varnothing^*)^*$. |
34 | 18 |
|
166
ef48e378c44e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
146
diff
changeset
|
19 |
\item If a regular expression $r$ does not contain any occurrence of $\varnothing$, |
42 | 20 |
is it possible for $L(r)$ to be empty? |
32 | 21 |
|
264
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
22 |
\item Define the tokens and regular expressions for a language |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
23 |
consisting of numbers, left-parenthesis $($, |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
24 |
right-parenthesis $)$, identifiers and the operations $+$, |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
25 |
$-$ and $*$. Can the following strings in this language |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
26 |
be lexed? |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
27 |
|
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
28 |
\begin{itemize} |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
29 |
\item $(a + 3) * b$ |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
30 |
\item $)()++ -33$ |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
31 |
\item $(a / 3) * 3$ |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
32 |
\end{itemize} |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
33 |
|
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
34 |
In case they can, can you give the corresponding token |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
35 |
sequences. |
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
36 |
|
32 | 37 |
\item Assume that $s^{-1}$ stands for the operation of reversing a |
38 |
string $s$. Given the following \emph{reversing} function on regular |
|
39 |
expressions |
|
40 |
||
34 | 41 |
\begin{center} |
42 |
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l} |
|
43 |
$rev(\varnothing)$ & $\dn$ & $\varnothing$\\ |
|
44 |
$rev(\epsilon)$ & $\dn$ & $\epsilon$\\ |
|
45 |
$rev(c)$ & $\dn$ & $c$\\ |
|
46 |
$rev(r_1 + r_2)$ & $\dn$ & $rev(r_1) + rev(r_2)$\\ |
|
47 |
$rev(r_1 \cdot r_2)$ & $\dn$ & $rev(r_2) \cdot rev(r_1)$\\ |
|
48 |
$rev(r^*)$ & $\dn$ & $rev(r)^*$\\ |
|
49 |
\end{tabular} |
|
50 |
\end{center} |
|
51 |
||
52 |
||
32 | 53 |
and the set |
54 |
||
31 | 55 |
\begin{center} |
32 | 56 |
$Rev\,A \dn \{s^{-1} \;|\; s \in A\}$ |
31 | 57 |
\end{center} |
58 |
||
34 | 59 |
prove whether |
32 | 60 |
|
31 | 61 |
\begin{center} |
32 | 62 |
$L(rev(r)) = Rev (L(r))$ |
31 | 63 |
\end{center} |
64 |
||
32 | 65 |
holds. |
31 | 66 |
|
42 | 67 |
\item Give a regular expression over the alphabet $\{a,b\}$ recognising all strings |
68 |
that do not contain any substring $bb$ and end in $a$. |
|
69 |
||
70 |
\item Assume the delimiters for comments are \texttt{$\slash$*} and \texttt{*$\slash$}. |
|
71 |
Give a regular expression that can recognise comments |
|
72 |
of the form |
|
73 |
||
74 |
\begin{center} |
|
75 |
\texttt{$\slash$*~\ldots{}~*$\slash$} |
|
76 |
\end{center} |
|
77 |
||
78 |
where the three dots stand for arbitrary characters, but not comment delimiters. |
|
79 |
(Hint: You can assume you are already given a regular expression written \texttt{ALL}, |
|
55 | 80 |
that can recognise any character, and a regular expression \texttt{NOT} that recognises |
81 |
the complement of a regular expression.) |
|
42 | 82 |
|
146
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
83 |
|
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
84 |
|
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
85 |
%\item (Optional) The tokenizer in \texttt{regexp3.scala} takes as |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
86 |
%argument a string and a list of rules. The result is a list of tokens. Improve this tokenizer so |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
87 |
%that it filters out all comments and whitespace from the result. |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
88 |
|
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
89 |
%\item (Optional) Modify the tokenizer in \texttt{regexp2.scala} so that it |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
90 |
%implements the \texttt{findAll} function. This function takes a regular |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
91 |
%expressions and a string, and returns all substrings in this string that |
9da175d5eb63
added new hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
92 |
%match the regular expression. |
31 | 93 |
\end{enumerate} |
94 |
||
42 | 95 |
% explain what is a context-free grammar and the language it generates |
96 |
% |
|
97 |
% |
|
264
4deef8ac5d72
uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
166
diff
changeset
|
98 |
% |
42 | 99 |
% |
100 |
% |
|
101 |
% does (a + b)*b+ and (a*b+) + (b*b+) define the same language |
|
43 | 102 |
|
31 | 103 |
|
104 |
\end{document} |
|
105 |
||
106 |
%%% Local Variables: |
|
107 |
%%% mode: latex |
|
108 |
%%% TeX-master: t |
|
109 |
%%% End: |