coursework/cw02.tex
changeset 182 9ce2414e470e
parent 181 1f98d215df71
child 198 f54972b0f641
equal deleted inserted replaced
181:1f98d215df71 182:9ce2414e470e
   131 \end{itemize} 
   131 \end{itemize} 
   132  
   132  
   133 \subsection*{Question 3 (marked with 1\%)}
   133 \subsection*{Question 3 (marked with 1\%)}
   134 
   134 
   135 Write a program in the WHILE programming language that prints out all prime numbers between
   135 Write a program in the WHILE programming language that prints out all prime numbers between
   136 0 and a fixed number (say 100). Take the grammar of this language from the lectures. As another 
   136 0 and a fixed number (say 100). A partial grammar of the WHILE language is given below. 
   137 guidance have a look at the Fibonacci program
   137 
   138 and ``three-nested-loops'' program shown below. For example, printing a variable \texttt{x} in the 
   138 \begin{center}
   139 WHILE language can be done by using the command \mbox{\texttt{write x}}. 
   139 \begin{tabular}{@{}lcl@{}}
       
   140 \textit{Stmt} & $\rightarrow$ &  $\texttt{skip}$\\
       
   141               & $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\
       
   142               & $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\
       
   143               & $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\
       
   144               & $|$ & \texttt{read}\;\textit{Id}\\
       
   145               & $|$ & \texttt{write}\;\textit{Id}\\
       
   146               & $|$ & \texttt{write}\;\textit{String}\medskip\\
       
   147 \textit{Stmts} & $\rightarrow$ &  \textit{Stmt} \;\texttt{;}\; \textit{Stmts}\\
       
   148               & $|$ & \textit{Stmt}\medskip\\
       
   149 \textit{Block} & $\rightarrow$ &  \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\
       
   150                 & $|$ & \textit{Stmt}\medskip\\
       
   151 \textit{AExp} & $\rightarrow$ & \ldots\\
       
   152 \textit{BExp} & $\rightarrow$ & \ldots\\
       
   153 \end{tabular}
       
   154 \end{center}
       
   155 
       
   156 \noindent
       
   157 As another guidance for your program have a look at the Fibonacci program
       
   158 and ``three-nested-loops'' program shown below in Figures \ref{fib} and \ref{loop}. 
       
   159 
   140 
   160 
   141 \begin{figure}[h]
   161 \begin{figure}[h]
   142 \begin{center}
   162 \begin{center}
   143 \mbox{\lstinputlisting[language=while]{../progs/fib.while}}
   163 \mbox{\lstinputlisting[language=while]{../progs/fib.while}}
   144 \end{center}
   164 \end{center}