hws/hw04.tex
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--
fixed typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     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
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     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
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
     9
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
\begin{document}
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\section*{Homework 4}
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
\begin{enumerate}
34
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    15
\item Why is every finite set of strings a regular language?
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    16
42
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    17
\item What is the language recognised by the regular expressions $(\varnothing^*)^*$.
34
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    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
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    20
is it possible for $L(r)$ to be empty?
32
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    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
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    37
\item Assume that $s^{-1}$ stands for the operation of reversing a
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    38
string $s$. Given the following \emph{reversing} function on regular 
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    39
expressions
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    40
34
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    41
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    42
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    43
$rev(\varnothing)$   & $\dn$ & $\varnothing$\\
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    44
$rev(\epsilon)$         & $\dn$ & $\epsilon$\\
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    45
$rev(c)$                      & $\dn$ & $c$\\
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    46
$rev(r_1 + r_2)$        & $\dn$ & $rev(r_1) + rev(r_2)$\\
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    47
$rev(r_1 \cdot r_2)$  & $\dn$ & $rev(r_2) \cdot rev(r_1)$\\
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    48
$rev(r^*)$                   & $\dn$ & $rev(r)^*$\\
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    49
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    50
\end{center}
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    51
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    52
32
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    53
and the set
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    54
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
\begin{center}
32
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    56
$Rev\,A \dn \{s^{-1} \;|\; s \in A\}$
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
\end{center}
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
34
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
    59
prove whether
32
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    60
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
\begin{center}
32
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    62
$L(rev(r)) = Rev (L(r))$
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
\end{center}
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
32
d085fe0c086f started
Christian Urban <urbanc@in.tum.de>
parents: 31
diff changeset
    65
holds.
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
42
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    67
\item Give a regular expression over the alphabet $\{a,b\}$ recognising all strings 
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    68
that do not contain any substring $bb$ and end in $a$.
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    69
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    70
\item Assume the delimiters for comments are \texttt{$\slash$*} and \texttt{*$\slash$}.
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    71
Give a regular expression that can recognise comments
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    72
of the form 
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    73
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    74
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    75
\texttt{$\slash$*~\ldots{}~*$\slash$} 
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    76
\end{center}
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    77
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    78
where the three dots stand for arbitrary characters, but not comment delimiters.
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    79
(Hint: You can assume you are already given a regular expression written \texttt{ALL},
55
cceed8d66b28 updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    80
that can recognise any character, and a regular expression \texttt{NOT} that recognises
cceed8d66b28 updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    81
the complement of a regular expression.)
42
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    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
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
\end{enumerate}
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
42
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    95
% explain what is a context-free grammar and the language it generates 
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    96
%
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    97
%
264
4deef8ac5d72 uodated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
    98
% 
42
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    99
%
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   100
%
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   101
% does (a + b)*b+ and (a*b+) + (b*b+) define the same language
43
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   102
31
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
\end{document}
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
%%% Local Variables: 
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
%%% mode: latex
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
%%% TeX-master: t
e22ba348b209 added hw04
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
%%% End: