# HG changeset patch # User Christian Urban # Date 1385205395 0 # Node ID f1335c171d50bbcc1f08ae574cceb4488cdb42c9 # Parent 180cbfc1520a34a18996a14f452a70cf2f2f41dc added diff -r 180cbfc1520a -r f1335c171d50 coursework/cw03.pdf Binary file coursework/cw03.pdf has changed diff -r 180cbfc1520a -r f1335c171d50 coursework/cw03.tex --- a/coursework/cw03.tex Sat Nov 23 11:09:27 2013 +0000 +++ b/coursework/cw03.tex Sat Nov 23 11:16:35 2013 +0000 @@ -33,7 +33,7 @@ } \lstdefinelanguage{while}{ - morekeywords={while, if, then. else, read, write}, + morekeywords={while, if, then. else, read, write, for, upto, do}, otherkeywords={=>,<-,<\%,<:,>:,\#,@}, sensitive=true, morecomment=[l]{//}, @@ -135,7 +135,20 @@ 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 while loops). +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}