author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 02 Dec 2022 16:26:20 +0000 | |
changeset 901 | 33cff35bdc1a |
parent 805 | 526e10d97435 |
child 906 | 2bf1516d730f |
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 |
||
647 | 36 |
\item Describe what the following JVM instructions do! |
459 | 37 |
|
647 | 38 |
|
39 |
\begin{lstlisting}[language=JVMIS2,numbers=none] |
|
40 |
ldc 3 |
|
459 | 41 |
iload 3 |
42 |
istore 1 |
|
525 | 43 |
ifeq label |
647 | 44 |
if_icmpge label |
459 | 45 |
\end{lstlisting} |
46 |
||
901 | 47 |
\item What does the following JVM function calculate? |
48 |
||
49 |
\begin{lstlisting}[language=JVMIS2,numbers=none] |
|
50 |
.method public static bar(I)I |
|
51 |
.limit locals 1 |
|
52 |
.limit stack 9 |
|
53 |
iload 0 |
|
54 |
ldc 0 |
|
55 |
if_icmpne If_else_8 |
|
56 |
ldc 0 |
|
57 |
goto If_end_9 |
|
58 |
If_else_8: |
|
59 |
iload 0 |
|
60 |
ldc 1 |
|
61 |
if_icmpne If_else_10 |
|
62 |
ldc 1 |
|
63 |
goto If_end_11 |
|
64 |
If_else_10: |
|
65 |
iload 0 |
|
66 |
ldc 1 |
|
67 |
isub |
|
68 |
invokestatic bar(I)I |
|
69 |
iload 0 |
|
70 |
ldc 2 |
|
71 |
isub |
|
72 |
invokestatic bar(I)I |
|
73 |
iadd |
|
74 |
If_end_11: |
|
75 |
If_end_9: |
|
76 |
ireturn |
|
77 |
.end method |
|
78 |
\end{lstlisting} |
|
577 | 79 |
|
703 | 80 |
\item What does the following LLVM function calculate? Give the |
81 |
corresponding arithmetic expression . |
|
648 | 82 |
|
83 |
\begin{lstlisting}[language=LLVM,numbers=none] |
|
84 |
define i32 @foo(i32 %a, i32 %b) { |
|
85 |
%1 = mul i32 %a, %a |
|
86 |
%2 = mul i32 %a, 2 |
|
87 |
%3 = mul i32 %2, %b |
|
88 |
%4 = add i32 %1, %3 |
|
89 |
%5 = mul i32 %b, %b |
|
90 |
%6 = add i32 %5, %4 |
|
91 |
ret i32 %6 |
|
92 |
} |
|
93 |
\end{lstlisting} |
|
94 |
||
705 | 95 |
\item What is the difference between a parse tree and an abstract |
96 |
syntax tree? Give some simple examples for each of them. |
|
648 | 97 |
|
98 |
||
805 | 99 |
\item Give a description of how the Brzozowski matcher works. |
100 |
The description should be coherent and logical. |
|
577 | 101 |
|
805 | 102 |
\item Give a description of how a compiler for the While-language can |
103 |
be implemented. You should assume you are producing code for the JVM. |
|
104 |
The description should be coherent and logical. |
|
105 |
||
106 |
||
459 | 107 |
\item \POSTSCRIPT |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
|
314
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
109 |
% \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
|
110 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
111 |
% \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
|
112 |
% \;\;then\;\;$L(r) \not= \{\}$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
113 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
114 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
115 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
116 |
% holds, or equivalently |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
117 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
118 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
119 |
% $L(r) = \{\}$ \;\;implies\;\; $\varnothing$ occurs in $r$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
120 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
121 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
122 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
123 |
% 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
|
124 |
% 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
|
125 |
% the following function: |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
126 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
127 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
128 |
% \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
|
129 |
% $occurs(\varnothing)$ & $\dn$ & $true$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
130 |
% $occurs(\epsilon)$ & $\dn$ & $f\!alse$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
131 |
% $occurs (c)$ & $\dn$ & $f\!alse$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
132 |
% $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
|
133 |
% $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
|
134 |
% $occurs (r^*)$ & $\dn$ & $occurs(r)$ \\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
135 |
% \end{tabular} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
136 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
137 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
138 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
139 |
% Now you can prove |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
140 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
141 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
142 |
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
143 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
144 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
145 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
146 |
% 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
|
147 |
% 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
|
148 |
% 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
|
149 |
% 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
|
150 |
% 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
|
151 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
152 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
153 |
% \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
|
154 |
% $occurs(\sim r)$ & $\dn$ & $occurs(r)$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
155 |
% \end{tabular} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
156 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
157 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
158 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
159 |
% 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
|
160 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
161 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
162 |
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
163 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
164 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
165 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
166 |
% 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
|
167 |
% property. |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
\end{enumerate} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
170 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
\end{document} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
%%% Local Variables: |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
%%% mode: latex |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
%%% TeX-master: t |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
%%% End: |