# HG changeset patch # User Christian Urban # Date 1571220893 -3600 # Node ID 15b69ca63b29b9a2d641d22f3a8ee2a53b5e22ff # Parent a18e3c027c1f254746df1a3a2eac9682297c8d3e optional diff -r a18e3c027c1f -r 15b69ca63b29 coursework/cw02.pdf Binary file coursework/cw02.pdf has changed diff -r a18e3c027c1f -r 15b69ca63b29 coursework/cw02.tex --- a/coursework/cw02.tex Wed Oct 16 11:06:32 2019 +0100 +++ b/coursework/cw02.tex Wed Oct 16 11:14:53 2019 +0100 @@ -172,7 +172,7 @@ Extend your lexer from Q2 to also simplify regular expressions after each derivation step and rectify the computed values after each injection. Use this lexer to tokenize the programs in -Figure~\ref{fib} and \ref{loop}. Give the tokens of these +Figure~\ref{fib}, \ref{loop} and \ref{factor}. Give the tokens of these programs where whitespaces are filtered out. Make sure you can tokenise \textbf{exactly} these programs.\bigskip @@ -188,6 +188,12 @@ (Usually used for timing measurements.)\label{loop}} \end{figure} +\begin{figure}[h] +\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/factors.while}} +\caption{A program that calculates factors for numbers in the WHILE + language.\label{factors}} +\end{figure} + \end{document} %%% Local Variables: diff -r a18e3c027c1f -r 15b69ca63b29 progs/factors.while --- a/progs/factors.while Wed Oct 16 11:06:32 2019 +0100 +++ b/progs/factors.while Wed Oct 16 11:14:53 2019 +0100 @@ -1,14 +1,14 @@ -// Find all factors of a given input number - J.R. Cordy August 2005 -var n; +// Find all factors of a given input number +// by J.R. Cordy August 2005 + write "Input n please"; read n; write "The factors of n are"; -var f; f := 2; -while n != 1 do - while (n / f) * f = n do +while n != 1 do { + while (n / f) * f == n do { write f; - n := n / f; - end - f := f + 1; -end \ No newline at end of file + n := n / f + }; + f := f + 1 +} \ No newline at end of file