\documentclass{article}
\usepackage{charter}
\usepackage{hyperref}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{xcolor}
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
\begin{document}
\definecolor{javared}{rgb}{0.6,0,0} % for strings
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
\lstdefinelanguage{scala}{
morekeywords={abstract,case,catch,class,def,%
do,else,extends,false,final,finally,%
for,if,implicit,import,match,mixin,%
new,null,object,override,package,%
private,protected,requires,return,sealed,%
super,this,throw,trait,true,try,
type,val,var,while,with,yield},
otherkeywords={=>,<-,<\%,<:,>:,\#,@},
sensitive=true,
morecomment=[l]{//},
morecomment=[n]{/*}{*/},
morestring=[b]",
morestring=[b]',
morestring=[b]"""
}
\lstdefinelanguage{while}{
morekeywords={while, if, then. else, read, write, for, upto, do},
otherkeywords={=>,<-,<\%,<:,>:,\#,@},
sensitive=true,
morecomment=[l]{//},
morecomment=[n]{/*}{*/},
morestring=[b]",
morestring=[b]',
morestring=[b]"""
}
\lstset{language=Scala,
basicstyle=\ttfamily,
keywordstyle=\color{javapurple}\bfseries,
stringstyle=\color{javagreen},
commentstyle=\color{javagreen},
morecomment=[s][\color{javadocblue}]{/**}{*/},
numbers=left,
numberstyle=\tiny\color{black},
stepnumber=1,
numbersep=10pt,
tabsize=2,
showspaces=false,
showstringspaces=false}
\section*{Coursework 3}
\noindent
This coursework is worth 4\% and is due on 13 December at 16:00. You are asked to
implement a compiler for the WHILE language that targets the
assembler language provided by the Jasmin. This assembler
is available from
\begin{center}
\url{http://jasmin.sourceforge.net}
\end{center}
\noindent
There is a user guide for Jasmin
\begin{center}
\url{http://jasmin.sourceforge.net/guide.html}
\end{center}
\noindent
and also a description of some of the instructions that the JVM understands
\begin{center}
\url{http://jasmin.sourceforge.net/instructions.html}
\end{center}
\noindent
\noindent
You need to submit a document containing the answers for the two questions
below. You can do the implementation in any programming language you like, but you need
to submit the source code with which you answered the questions. However, the coursework
will \emph{only} be judged according to the answers. You can submit your answers
in a txt-file or as pdf.\bigskip
\subsection*{Question 1 (marked with 2\%)}
You need to lex and parse WHILE programs and submit the assembler
instructions for the Fibonacci program and for the program you submitted
in Coursework 2 in Question 3. The latter should be so modified that
a user can input the upper bound on the console (in the original question
it was fixed to 100).
\subsection*{Question 2 (marked with 2\%)}
Extend the syntax of you language so that it contains also \texttt{for}-loops, like
\begin{center}
\texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block}
\end{center}
\noindent
The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic
expression, then go through the loop, at the end increase the value of the variable by 1,
and finally test wether the value is not less or equal anymore to the value of the second
arithmetic expression. For example the following instance of a \texttt{for}-loop
is supposed to print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
\begin{center}
\begin{minipage}{6cm}
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
for i := 2 upto 4 do {
write i
}
\end{lstlisting}
\end{minipage}
\end{center}
\noindent
There are two ways how this can be implemented: one is to adapt the code generation
part of the compiler and generate specific code for \texttt{for}-loops; the other is to
translate the abstract syntax tree of \texttt{for}-loops into an abstract syntax tree using
existing language constructs. For example the loop above could be translated
to the following \texttt{while}-loop:
\begin{center}
\begin{minipage}{6cm}
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
i := 2;
while (i <= 4) do {
write i;
i := i + 1;
}
\end{lstlisting}
\end{minipage}
\end{center}
\subsection*{Further Information}
The Java infrastructure unfortunately does not contain an assembler out-of-the-box
(therefore
you need to download the additional package Jasmin---see above). But it does contain a
disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it
generates readable assembler code from Java Byte Code. Have a look at the
following example. Compile using the usual Java compiler,
\texttt{java}, the simple Hello World program below:
\begin{center}
\begin{minipage}{10cm}
\begin{lstlisting}[language=Java,basicstyle=\ttfamily]
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
\end{lstlisting}
\end{minipage}
\end{center}
\noindent
You can use the command
\begin{center}
\texttt{javap -v HelloWorld}
\end{center}
\noindent
in order to see which Java Byte Code has been generated for this
program. You can compare this with the code generated for the Scala
version of Hello Worlds.
\begin{center}
\begin{minipage}{10cm}
\begin{lstlisting}[language=Scala,basicstyle=\ttfamily]
object HelloWorld {
def main(args: Array[String]) {
println("Hello World!")
}
}
\end{lstlisting}
\end{minipage}
\end{center}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: