author | Christian Urban <urbanc@in.tum.de> |
Tue, 27 Nov 2018 01:15:54 +0000 | |
changeset 610 | 7ec1bdb670ba |
parent 577 | 7a437f1f689d |
child 647 | 180600c04da2 |
permissions | -rw-r--r-- |
208
bd5a8a6b3871
added
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:
208
diff
changeset
|
2 |
\usepackage{../style} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
208
diff
changeset
|
3 |
\usepackage{../graphics} |
459 | 4 |
\usepackage{../langs} |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
\begin{document} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
\section*{Homework 9} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
359
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
314
diff
changeset
|
10 |
\HEADER |
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
314
diff
changeset
|
11 |
|
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
\begin{enumerate} |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
208
diff
changeset
|
13 |
\item Describe what is meant by \emph{eliminating tail |
459 | 14 |
recursion}? When can this optimization be applied and |
15 |
why is it of benefit? |
|
16 |
||
17 |
\item A programming language has arithmetic expression. For an |
|
18 |
arithmetic expression the compiler of this language produces the |
|
19 |
following snippet of JVM code. |
|
20 |
||
21 |
\begin{lstlisting}[language=JVMIS,numbers=none] |
|
22 |
ldc 1 |
|
23 |
ldc 2 |
|
24 |
ldc 3 |
|
25 |
imul |
|
26 |
ldc 4 |
|
27 |
ldc 3 |
|
28 |
isub |
|
29 |
iadd |
|
30 |
iadd |
|
31 |
\end{lstlisting} |
|
32 |
||
33 |
Give the arithmetic expression that produced this code. Make sure |
|
34 |
you give all necessary parentheses. |
|
35 |
||
525 | 36 |
\item Describe what the following three JVM instructions do! |
459 | 37 |
|
38 |
\begin{lstlisting}[language=JVMIS,numbers=none] |
|
39 |
iload 3 |
|
40 |
istore 1 |
|
525 | 41 |
ifeq label |
459 | 42 |
\end{lstlisting} |
43 |
||
577 | 44 |
|
45 |
\item Early in the module, we saw that the regular expression matchers |
|
46 |
in Java, Python and Ruby are very slow with some (basic) regular |
|
47 |
expressions. What is the main reason for this ineffcient computation? |
|
48 |
||
459 | 49 |
\item \POSTSCRIPT |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
|
314
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
51 |
% \item It is true (I confirmed it) that |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
52 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
53 |
% \begin{center} if $\varnothing$ does not occur in $r$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
54 |
% \;\;then\;\;$L(r) \not= \{\}$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
55 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
56 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
57 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
58 |
% holds, or equivalently |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
59 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
60 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
61 |
% $L(r) = \{\}$ \;\;implies\;\; $\varnothing$ occurs in $r$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
62 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
63 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
64 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
65 |
% You can prove either version by induction on $r$. The best way to |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
66 |
% make more formal what is meant by `$\varnothing$ occurs in $r$', you can define |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
67 |
% the following function: |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
68 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
69 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
70 |
% \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
71 |
% $occurs(\varnothing)$ & $\dn$ & $true$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
72 |
% $occurs(\epsilon)$ & $\dn$ & $f\!alse$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
73 |
% $occurs (c)$ & $\dn$ & $f\!alse$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
74 |
% $occurs (r_1 + r_2)$ & $\dn$ & $occurs(r_1) \vee occurs(r_2)$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
75 |
% $occurs (r_1 \cdot r_2)$ & $\dn$ & $occurs(r_1) \vee occurs(r_2)$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
76 |
% $occurs (r^*)$ & $\dn$ & $occurs(r)$ \\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
77 |
% \end{tabular} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
78 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
79 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
80 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
81 |
% Now you can prove |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
82 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
83 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
84 |
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
85 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
86 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
87 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
88 |
% The interesting cases are $r_1 + r_2$ and $r^*$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
89 |
% The other direction is not true, that is if $occurs(r)$ then $L(r) = \{\}$. A counter example |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
90 |
% is $\varnothing + a$: although $\varnothing$ occurs in this regular expression, the corresponding |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
91 |
% language is not empty. The obvious extension to include the not-regular expression, $\sim r$, |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
92 |
% also leads to an incorrect statement. Suppose we add the clause |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
93 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
94 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
95 |
% \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
96 |
% $occurs(\sim r)$ & $\dn$ & $occurs(r)$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
97 |
% \end{tabular} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
98 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
99 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
100 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
101 |
% to the definition above, then it will not be true that |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
102 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
103 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
104 |
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
105 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
106 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
107 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
108 |
% Assume the alphabet contains just $a$ and $b$, find a counter example to this |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
109 |
% property. |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
\end{enumerate} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
\end{document} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
%%% Local Variables: |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
%%% mode: latex |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
%%% TeX-master: t |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
%%% End: |