coursework/cw04.tex
changeset 391 f352cb238c32
parent 390 a1fe591a3df3
child 419 4110ab35e5d8
equal deleted inserted replaced
390:a1fe591a3df3 391:f352cb238c32
   145 
   145 
   146 Extend the syntax of your language so that it contains also
   146 Extend the syntax of your language so that it contains also
   147 \texttt{for}-loops, like
   147 \texttt{for}-loops, like
   148 
   148 
   149 \begin{center}
   149 \begin{center}
   150 \texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} 
   150 \lstset{language=While}
       
   151 \code{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \code{upto} 
       
   152 \;\textit{AExp}\; \code{do} \textit{Block} 
   151 \end{center}
   153 \end{center}
   152 
   154 
   153 \noindent The intended meaning is to first assign the variable
   155 \noindent The intended meaning is to first assign the variable
   154 \textit{Id} the value of the first arithmetic expression, then
   156 \textit{Id} the value of the first arithmetic expression, test
   155 go through the loop, at the end increase the value of the
   157 wether this value is less or equal than the value of the
   156 variable by 1, and finally test wether the value is not less
   158 second arithmetic expression. If yes, go through the loop, and
   157 or equal to the value of the second arithmetic expression (in
   159 at the end increase the value of the loop variable by 1 and
   158 case it is greater, you should leave the loop). For example
   160 start again with the test. If no, leave the loop. For example
   159 the following instance of a \texttt{for}-loop is supposed to
   161 the following instance of a \code{for}-loop is supposed to
   160 print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
   162 print out the numbers \pcode{2}, \pcode{3}, \pcode{4}.
   161 
   163 
   162 
   164 
   163 \begin{center}
   165 \begin{center}
   164 \begin{minipage}{12cm}
   166 \begin{minipage}{12cm}
   165 \begin{lstlisting}[language=While, numbers=none]
   167 \begin{lstlisting}[language=While, numbers=none]
   170 \end{minipage}
   172 \end{minipage}
   171 \end{center}
   173 \end{center}
   172 
   174 
   173 \noindent There are two ways how this can be implemented: one
   175 \noindent There are two ways how this can be implemented: one
   174 is to adapt the code generation part of the compiler and
   176 is to adapt the code generation part of the compiler and
   175 generate specific code for \texttt{for}-loops; the other is to
   177 generate specific code for \code{for}-loops; the other is to
   176 translate the abstract syntax tree of \texttt{for}-loops into
   178 translate the abstract syntax tree of \code{for}-loops into
   177 an abstract syntax tree using existing language constructs.
   179 an abstract syntax tree using existing language constructs.
   178 For example the loop above could be translated to the
   180 For example the loop above could be translated to the
   179 following \texttt{while}-loop:
   181 following \code{while}-loop:
   180 
   182 
   181 \begin{center}
   183 \begin{center}
   182 \begin{minipage}{12cm}
   184 \begin{minipage}{12cm}
   183 \begin{lstlisting}[language=While, numbers=none]
   185 \begin{lstlisting}[language=While, numbers=none]
   184 i := 2;
   186 i := 2;