31
|
1 |
\documentclass{article}
|
264
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
2 |
\usepackage{../style}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
3 |
|
146
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
4 |
\usepackage{tikz}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
5 |
\usetikzlibrary{automata}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
6 |
|
31
|
7 |
|
264
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
22 |
\item Define the tokens and regular expressions for a language
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
23 |
consisting of numbers, left-parenthesis $($,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
24 |
right-parenthesis $)$, identifiers and the operations $+$,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
25 |
$-$ and $*$. Can the following strings in this language
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
26 |
be lexed?
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
27 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
28 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
29 |
\item $(a + 3) * b$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
30 |
\item $)()++ -33$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
31 |
\item $(a / 3) * 3$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
32 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
33 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
34 |
In case they can, can you give the corresponding token
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
35 |
sequences.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
83 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
84 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
85 |
%\item (Optional) The tokenizer in \texttt{regexp3.scala} takes as
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
86 |
%argument a string and a list of rules. The result is a list of tokens. Improve this tokenizer so
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
87 |
%that it filters out all comments and whitespace from the result.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
88 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
89 |
%\item (Optional) Modify the tokenizer in \texttt{regexp2.scala} so that it
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
90 |
%implements the \texttt{findAll} function. This function takes a regular
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
91 |
%expressions and a string, and returns all substrings in this string that
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
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:
|