author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 20 Oct 2015 00:01:56 +0100 | |
changeset 359 | db106e5b7c4d |
parent 322 | 698ed1c96cd0 |
child 401 | 5d85dc9779b1 |
permissions | -rw-r--r-- |
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 |
|
359
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
322
diff
changeset
|
14 |
\HEADER |
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
322
diff
changeset
|
15 |
|
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
322
diff
changeset
|
16 |
|
93
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
\begin{enumerate} |
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
18 |
\item Consider the basic regular expressions |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
19 |
|
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
20 |
\begin{center} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
21 |
$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
|
22 |
\end{center} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
23 |
|
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
24 |
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
|
25 |
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
|
26 |
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
|
27 |
in a case. |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
28 |
|
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
29 |
\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
|
30 |
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
|
31 |
following extended regular expressions: |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
32 |
|
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
33 |
\begin{center} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
34 |
$r ::= \varnothing \;|\; |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
35 |
\epsilon \;|\; |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
36 |
c \;|\; |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
37 |
r_1 + r_2 \;|\; |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
38 |
r_1 \cdot r_2 \;|\; |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
39 |
r^* \;|\; |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
40 |
\sim r$ |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
41 |
\end{center} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
42 |
|
322
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
43 |
%\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
|
44 |
%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
|
45 |
%recognise comments of the form |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
46 |
% |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
47 |
%\begin{center} |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
48 |
%\texttt{$\slash$*~\ldots{}~*$\slash$} |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
49 |
%\end{center} |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
50 |
% |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
51 |
%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
|
52 |
%comment delimiters. |
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
53 |
|
93
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
\item Define the following regular expressions |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
\begin{center} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
\begin{tabular}{ll} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
$r^+$ & (one or more matches)\\ |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
$r^?$ & (zero or one match)\\ |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
$r^{\{n\}}$ & (exactly $n$ matches)\\ |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
$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
|
62 |
& \phantom{(}assumption $m \le n$)\\ |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
\end{tabular} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
\end{center} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
66 |
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
|
67 |
|
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
\begin{center} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
$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
|
70 |
\end{center} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
72 |
\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
|
73 |
consisting of identifiers, left-parenthesis \texttt{(}, |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
\texttt{-} and \texttt{*}. |
93
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
|
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
78 |
Decide whether the following strings |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
79 |
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
|
80 |
|
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
81 |
\begin{enumerate} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
82 |
\item \texttt{"(a3+3)*b"} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
83 |
\item \texttt{")()++-33"} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
84 |
\item \texttt{"(b42/3)*3"} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
85 |
\end{enumerate} |
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
86 |
|
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
|
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
91 |
\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
|
92 |
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
|
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 |
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
|
96 |
\] |
4725bba8ef26
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
97 |
|
4725bba8ef26
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
98 |
holds. |
294
c29853b672fb
updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
99 |
|
93
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
\end{enumerate} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
|
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
\end{document} |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
|
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
%%% Local Variables: |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
%%% mode: latex |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
%%% TeX-master: t |
4794759139ea
better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
%%% End: |