author | Christian Urban <christian.urban@kcl.ac.uk> |
Tue, 04 Apr 2023 22:31:09 +0100 | |
changeset 906 | 2bf1516d730f |
parent 901 | 33cff35bdc1a |
child 916 | 10f834eb0a9e |
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} |
906 | 5 |
\usepackage{../grammar} |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
\section*{Homework 9} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
359
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
314
diff
changeset
|
11 |
\HEADER |
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
314
diff
changeset
|
12 |
|
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
\begin{enumerate} |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
208
diff
changeset
|
14 |
\item Describe what is meant by \emph{eliminating tail |
459 | 15 |
recursion}? When can this optimization be applied and |
16 |
why is it of benefit? |
|
17 |
||
18 |
\item A programming language has arithmetic expression. For an |
|
19 |
arithmetic expression the compiler of this language produces the |
|
20 |
following snippet of JVM code. |
|
21 |
||
22 |
\begin{lstlisting}[language=JVMIS,numbers=none] |
|
23 |
ldc 1 |
|
24 |
ldc 2 |
|
25 |
ldc 3 |
|
26 |
imul |
|
27 |
ldc 4 |
|
28 |
ldc 3 |
|
29 |
isub |
|
30 |
iadd |
|
31 |
iadd |
|
32 |
\end{lstlisting} |
|
33 |
||
34 |
Give the arithmetic expression that produced this code. Make sure |
|
35 |
you give all necessary parentheses. |
|
36 |
||
647 | 37 |
\item Describe what the following JVM instructions do! |
459 | 38 |
|
647 | 39 |
|
40 |
\begin{lstlisting}[language=JVMIS2,numbers=none] |
|
41 |
ldc 3 |
|
459 | 42 |
iload 3 |
43 |
istore 1 |
|
525 | 44 |
ifeq label |
647 | 45 |
if_icmpge label |
459 | 46 |
\end{lstlisting} |
47 |
||
901 | 48 |
\item What does the following JVM function calculate? |
49 |
||
50 |
\begin{lstlisting}[language=JVMIS2,numbers=none] |
|
51 |
.method public static bar(I)I |
|
52 |
.limit locals 1 |
|
53 |
.limit stack 9 |
|
54 |
iload 0 |
|
55 |
ldc 0 |
|
56 |
if_icmpne If_else_8 |
|
57 |
ldc 0 |
|
58 |
goto If_end_9 |
|
59 |
If_else_8: |
|
60 |
iload 0 |
|
61 |
ldc 1 |
|
62 |
if_icmpne If_else_10 |
|
63 |
ldc 1 |
|
64 |
goto If_end_11 |
|
65 |
If_else_10: |
|
66 |
iload 0 |
|
67 |
ldc 1 |
|
68 |
isub |
|
69 |
invokestatic bar(I)I |
|
70 |
iload 0 |
|
71 |
ldc 2 |
|
72 |
isub |
|
73 |
invokestatic bar(I)I |
|
74 |
iadd |
|
75 |
If_end_11: |
|
76 |
If_end_9: |
|
77 |
ireturn |
|
78 |
.end method |
|
79 |
\end{lstlisting} |
|
577 | 80 |
|
703 | 81 |
\item What does the following LLVM function calculate? Give the |
82 |
corresponding arithmetic expression . |
|
648 | 83 |
|
84 |
\begin{lstlisting}[language=LLVM,numbers=none] |
|
85 |
define i32 @foo(i32 %a, i32 %b) { |
|
86 |
%1 = mul i32 %a, %a |
|
87 |
%2 = mul i32 %a, 2 |
|
88 |
%3 = mul i32 %2, %b |
|
89 |
%4 = add i32 %1, %3 |
|
90 |
%5 = mul i32 %b, %b |
|
91 |
%6 = add i32 %5, %4 |
|
92 |
ret i32 %6 |
|
93 |
} |
|
94 |
\end{lstlisting} |
|
95 |
||
906 | 96 |
\item As an optimisation technique, a compiler might want to detect `dead code' and |
97 |
not generate anything for this code. Why does this optimisation technique have the |
|
98 |
potential of speeding up the run-time of a program? (Hint: On what CPUs are programs |
|
99 |
run nowadays?) |
|
100 |
||
101 |
\item In an earlier question, we analysed the advantages of having a lexer-phase |
|
102 |
before running the parser (having a lexer is definitely a good thing to have). But you |
|
103 |
might wonder if a lexer can also be implemented by a parser and some simple |
|
104 |
grammar rules. Consider for example: |
|
105 |
||
106 |
\begin{plstx}[margin=1cm] |
|
107 |
: \meta{S\/} ::= (\meta{Kw\/}\mid \meta{Id\/}\mid \meta{Ws\/}) \cdot \meta{S\/} \;\mid\; \epsilon\\ |
|
108 |
: \meta{Kw\/} ::= \texttt{if} \mid \texttt{then} \mid \ldots\\ |
|
109 |
: \meta{Id\/} ::= (\texttt{a} \mid\ldots\mid \texttt{z}) \cdot \meta{Id\/} \;\mid\; \epsilon\\ |
|
110 |
: \meta{Ws\/} ::= \ldots\\ |
|
111 |
\end{plstx} |
|
112 |
||
113 |
What is wrong with implementing a lexer in this way? |
|
114 |
||
705 | 115 |
\item What is the difference between a parse tree and an abstract |
116 |
syntax tree? Give some simple examples for each of them. |
|
648 | 117 |
|
118 |
||
805 | 119 |
\item Give a description of how the Brzozowski matcher works. |
120 |
The description should be coherent and logical. |
|
577 | 121 |
|
805 | 122 |
\item Give a description of how a compiler for the While-language can |
123 |
be implemented. You should assume you are producing code for the JVM. |
|
124 |
The description should be coherent and logical. |
|
125 |
||
126 |
||
459 | 127 |
\item \POSTSCRIPT |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
|
314
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
129 |
% \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
|
130 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
131 |
% \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
|
132 |
% \;\;then\;\;$L(r) \not= \{\}$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
133 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
134 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
135 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
136 |
% holds, or equivalently |
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 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
139 |
% $L(r) = \{\}$ \;\;implies\;\; $\varnothing$ occurs in $r$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
140 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
141 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
142 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
143 |
% 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
|
144 |
% 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
|
145 |
% the following function: |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
146 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
147 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
148 |
% \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
|
149 |
% $occurs(\varnothing)$ & $\dn$ & $true$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
150 |
% $occurs(\epsilon)$ & $\dn$ & $f\!alse$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
151 |
% $occurs (c)$ & $\dn$ & $f\!alse$\\ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
152 |
% $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
|
153 |
% $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
|
154 |
% $occurs (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 |
% Now you can prove |
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 |
% 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
|
167 |
% 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
|
168 |
% 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
|
169 |
% 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
|
170 |
% 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
|
171 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
172 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
173 |
% \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
|
174 |
% $occurs(\sim r)$ & $\dn$ & $occurs(r)$ |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
175 |
% \end{tabular} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
176 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
177 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
178 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
179 |
% 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
|
180 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
181 |
% \begin{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
182 |
% $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$. |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
183 |
% \end{center} |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
184 |
% |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
185 |
% \noindent |
7dd5797a5ffa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
186 |
% 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
|
187 |
% property. |
208
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
188 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
189 |
\end{enumerate} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
190 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
191 |
\end{document} |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
192 |
|
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
193 |
%%% Local Variables: |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
%%% mode: latex |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
195 |
%%% TeX-master: t |
bd5a8a6b3871
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
196 |
%%% End: |