hws/hw05.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 17 Oct 2015 11:24:41 +0100
changeset 355 a259eec25156
parent 322 698ed1c96cd0
child 359 db106e5b7c4d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 267
diff changeset
     2
\usepackage{../style}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 267
diff changeset
     3
\usepackage{../graphics}
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\begin{document}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     8
% explain what is a context-free grammar and the language it generates 
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     9
%
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
    10
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
    11
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
\section*{Homework 5}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
\begin{enumerate}
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    15
\item Consider the basic regular expressions
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    16
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    17
\begin{center}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    18
$r ::= \varnothing \;|\; \epsilon \;|\; c  \;|\; r_1 + r_2  \;|\; r_1 \cdot r_2 \;|\; r^*$
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    19
\end{center}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    20
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    21
and suppose you want to show a property $P(r)$ for all regular
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    22
expressions $r$ by structural induction. Write down which cases do you 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    23
need to analyse. State clearly the induction hypotheses if applicable
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    24
in a case.
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    25
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    26
\item Define a regular expression, written $ALL$, that can match 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    27
every string. This definition should be in terms of the
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    28
following extended regular expressions:
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    29
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    30
\begin{center}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    31
$r ::= \varnothing \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    32
       \epsilon \;|\;  
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    33
       c  \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    34
       r_1 + r_2 \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    35
       r_1 \cdot r_2 \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    36
       r^* \;|\;
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    37
       \sim r$
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    38
\end{center}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    39
322
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    40
%\item Assume the delimiters for comments are \texttt{$\slash$*}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    41
%and \texttt{*$\slash$}. Give a regular expression that can
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    42
%recognise comments of the form
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    43
%
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    44
%\begin{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    45
%\texttt{$\slash$*~\ldots{}~*$\slash$} 
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    46
%\end{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    47
%
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    48
%where the three dots stand for arbitrary characters, but not
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    49
%comment delimiters.
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    50
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
\item Define the following regular expressions 
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
\begin{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
\begin{tabular}{ll}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
$r^+$ & (one or more matches)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
$r^?$   & (zero or one match)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
$r^{\{n\}}$ & (exactly $n$ matches)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
$r^{\{m, n\}}$ & (at least $m$ and maximal $n$ matches, with the\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
&  \phantom{(}assumption $m \le n$)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
\end{tabular}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
\end{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    63
in terms of the usual basic regular expressions
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
\begin{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
$r ::= \varnothing \;|\; \epsilon \;|\; c  \;|\; r_1 + r_2  \;|\; r_1 \cdot r_2 \;|\; r^*$
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
\end{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    69
\item Give the regular expressions for lexing a language
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    70
consisting of identifiers, left-parenthesis \texttt{(},
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    71
right-parenthesis \texttt{)}, numbers that can be either
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    72
positive or negative, and the operations \texttt{+},
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    73
\texttt{-} and \texttt{*}. 
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    75
Decide whether the following strings 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    76
can be lexed in this language?
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    77
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    78
\begin{enumerate}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    79
\item \texttt{"(a3+3)*b"}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    80
\item \texttt{")()++-33"}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    81
\item \texttt{"(b42/3)*3"}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    82
\end{enumerate}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    83
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    84
In case they can, give the corresponding token sequences. (Hint: 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    85
Observe the maximal munch rule and the priorities of your regular
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    86
expressions that make the process of lexing unambiguous.)
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    87
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    88
\item (Optional) Recall the definitions for $Der$ and $der$ from the lectures. 
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    89
Prove by induction on $r$ the property that 
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    90
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    91
\[
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    92
L(der\,c\,r) = Der\,c\,(L(r))
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    93
\]
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    94
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    95
holds.
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    96
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
\end{enumerate}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
\end{document}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
%%% Local Variables: 
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
%%% mode: latex
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
%%% TeX-master: t
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
%%% End: