author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 22 Nov 2013 16:56:51 +0000 | |
changeset 198 | f54972b0f641 |
parent 182 | 9ce2414e470e |
child 199 | 89cafeb66f40 |
permissions | -rw-r--r-- |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{charter} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{hyperref} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
\usepackage{amssymb} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
\usepackage{amsmath} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
\usepackage{listings} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\usepackage{xcolor} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
\begin{document} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
\definecolor{javared}{rgb}{0.6,0,0} % for strings |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
\lstdefinelanguage{scala}{ |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
morekeywords={abstract,case,catch,class,def,% |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
do,else,extends,false,final,finally,% |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
for,if,implicit,import,match,mixin,% |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
new,null,object,override,package,% |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
private,protected,requires,return,sealed,% |
179
d575895689b5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
178
diff
changeset
|
24 |
super,this,throw,trait,true,try, |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
type,val,var,while,with,yield}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
otherkeywords={=>,<-,<\%,<:,>:,\#,@}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
sensitive=true, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
morecomment=[l]{//}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
morecomment=[n]{/*}{*/}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
morestring=[b]", |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
morestring=[b]', |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
morestring=[b]""" |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
\lstdefinelanguage{while}{ |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
morekeywords={while, if, then. else, read, write}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
otherkeywords={=>,<-,<\%,<:,>:,\#,@}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
sensitive=true, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
morecomment=[l]{//}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
morecomment=[n]{/*}{*/}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
morestring=[b]", |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
morestring=[b]', |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
morestring=[b]""" |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
\lstset{language=Scala, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
basicstyle=\ttfamily, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
keywordstyle=\color{javapurple}\bfseries, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
stringstyle=\color{javagreen}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
commentstyle=\color{javagreen}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
morecomment=[s][\color{javadocblue}]{/**}{*/}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
numbers=left, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
numberstyle=\tiny\color{black}, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
stepnumber=1, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
numbersep=10pt, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
tabsize=2, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
showspaces=false, |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
showstringspaces=false} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
\section*{Coursework 2} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
|
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
65 |
{\bf UPDATE:} There was a typo in Q1 about the regular expressions for comments: |
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
66 |
they should, of course, start with $\slash\slash$, as in C for example, not with |
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
67 |
$\backslash\backslash$.\bigskip\bigskip |
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
68 |
|
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
69 |
\noindent |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
70 |
This coursework is worth 3\% and is due on 27 November at 16:00. You are asked to |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
72 |
\begin{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
73 |
\item implement a tokeniser for the WHILE language, |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
74 |
\item implement a parser and an evaluator for boolean and arithmetic expressions, and |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
75 |
\item write a WHILE program for printing out prime numbers. |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
76 |
\end{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
77 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
78 |
\noindent |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
You need to submit a document containing the answers for the questions |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
below. You can do the implementation in any programming language you like, but you need |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
to submit the source code with which you answered the questions. However, the coursework |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
will \emph{only} be judged according to the answers. You can submit your answers |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
83 |
in a txt-file or as pdf.\bigskip |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
\subsection*{Question 1 (marked with 1\%)} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
88 |
Implement a tokeniser for the WHILE language. You first need to design the appropriate |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
89 |
regular expressions for the following nine syntactic entities: |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
91 |
\begin{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
92 |
\item keywords are |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
93 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
94 |
\begin{quote} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
\texttt{while}, \texttt{if}, \texttt{then}, \texttt{else}, \texttt{do}, \texttt{for}, \texttt{to}, \texttt{true}, \texttt{false} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
\texttt{andalso}, \texttt{orelse}, \texttt{read}, \texttt{write} |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
97 |
\end{quote} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
98 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
99 |
\item operators are |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
100 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
101 |
\begin{quote} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
102 |
\texttt{+}, \texttt{-}, \texttt{*}, \texttt{\%}, \texttt{==}, \texttt{!=}, \texttt{>}, \texttt{<}, \texttt{:=} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
103 |
\end{quote} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
104 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
105 |
\item strings are enclosed by \texttt{"\ldots"} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
106 |
\item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
107 |
\item there are semicolons \texttt{;} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
108 |
\item whitespaces are either \texttt{" "} or \texttt{$\backslash$n} |
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
109 |
\item comments either start with $\slash\,\slash$ and run to the end of the corresponding line |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
110 |
(indicated by \texttt{$\backslash$n}), or they can also run over several lines but then need to be enclosed by |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
111 |
$\slash\texttt{*}$ as the beginning marker and $\texttt{*}\slash{}$\smallskip as the end marker |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
112 |
\item identifiers are letters followed by underscores \texttt{\_\!\_}, letters |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
113 |
or digits |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
114 |
\item numbers are \texttt{0}, \text{1}, \ldots |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
115 |
\end{enumerate} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
\noindent |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
118 |
Once you have implemented all regular expressions for 1 - 9, then |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
119 |
give the token sequence for the Fibonacci program shown below in Fig.~\ref{fib}. |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
\subsection*{Question 2 (marked with 1\%)} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
123 |
Implement parser combinators and an evaluation function for arithmetic and boolean |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
124 |
expressions. Arithmetic operations should include \texttt{+}, \texttt{-}, \texttt{*} and |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
125 |
\texttt{\%} (quotient). Boolean |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
126 |
operations should include \texttt{==} (equal), \texttt{!=} (unequal), \texttt{<} and |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
127 |
\texttt{>}. |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
Using the parser and evaluation function, calculate the values for |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
\begin{itemize} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
\item \texttt{17 < 3 * 3 * 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
\item \texttt{(29 - 20) * 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
\item \texttt{79 - 20 * 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
\item \texttt{2 * 2 != 12 \% 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
\end{itemize} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
\subsection*{Question 3 (marked with 1\%)} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
Write a program in the WHILE programming language that prints out all prime numbers between |
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
141 |
0 and a fixed number (say 100). A partial grammar of the WHILE language is given below. |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
142 |
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
143 |
\begin{center} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
144 |
\begin{tabular}{@{}lcl@{}} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
145 |
\textit{Stmt} & $\rightarrow$ & $\texttt{skip}$\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
146 |
& $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
147 |
& $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
148 |
& $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
149 |
& $|$ & \texttt{read}\;\textit{Id}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
150 |
& $|$ & \texttt{write}\;\textit{Id}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
151 |
& $|$ & \texttt{write}\;\textit{String}\medskip\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
152 |
\textit{Stmts} & $\rightarrow$ & \textit{Stmt} \;\texttt{;}\; \textit{Stmts}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
153 |
& $|$ & \textit{Stmt}\medskip\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
154 |
\textit{Block} & $\rightarrow$ & \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
155 |
& $|$ & \textit{Stmt}\medskip\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
156 |
\textit{AExp} & $\rightarrow$ & \ldots\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
157 |
\textit{BExp} & $\rightarrow$ & \ldots\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
158 |
\end{tabular} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
159 |
\end{center} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
160 |
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
161 |
\noindent |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
162 |
As another guidance for your program have a look at the Fibonacci program |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
163 |
and ``three-nested-loops'' program shown below in Figures \ref{fib} and \ref{loop}. |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
164 |
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
|
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
166 |
\begin{figure}[h] |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
\begin{center} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
\mbox{\lstinputlisting[language=while]{../progs/fib.while}} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
\end{center} |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
170 |
\caption{Fibonacci program in the WHILE language.\label{fib}} |
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
171 |
\end{figure} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
|
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
173 |
\begin{figure}[h] |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
\begin{center} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
\mbox{\lstinputlisting[language=while]{../progs/loops.while}} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
\end{center} |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
177 |
\caption{The three-nested-loops program in the WHILE language. Usually used for timing measurements.\label{loop}} |
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
178 |
\end{figure} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
179 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
180 |
\end{document} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
181 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
182 |
%%% Local Variables: |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
183 |
%%% mode: latex |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
%%% TeX-master: t |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
185 |
%%% End: |