coursework/cw03.tex
changeset 203 f1335c171d50
parent 202 180cbfc1520a
child 204 fec99c437965
equal deleted inserted replaced
202:180cbfc1520a 203:f1335c171d50
    31   morestring=[b]',
    31   morestring=[b]',
    32   morestring=[b]"""
    32   morestring=[b]"""
    33 }
    33 }
    34 
    34 
    35 \lstdefinelanguage{while}{
    35 \lstdefinelanguage{while}{
    36   morekeywords={while, if, then. else, read, write},
    36   morekeywords={while, if, then. else, read, write, for, upto, do},
    37   otherkeywords={=>,<-,<\%,<:,>:,\#,@},
    37   otherkeywords={=>,<-,<\%,<:,>:,\#,@},
    38   sensitive=true,
    38   sensitive=true,
    39   morecomment=[l]{//},
    39   morecomment=[l]{//},
    40   morecomment=[n]{/*}{*/},
    40   morecomment=[n]{/*}{*/},
    41   morestring=[b]",
    41   morestring=[b]",
   133 
   133 
   134 \noindent
   134 \noindent
   135 There are two ways how this can be implemented: one is to adapt the code generation 
   135 There are two ways how this can be implemented: one is to adapt the code generation 
   136 part of the compiler and generate specific code for \texttt{for}-loops; the other is to
   136 part of the compiler and generate specific code for \texttt{for}-loops; the other is to
   137 translate the abstract syntax tree of \texttt{for}-loops into an abstract syntax tree using
   137 translate the abstract syntax tree of \texttt{for}-loops into an abstract syntax tree using
   138 existing language constructs (for example while loops).
   138 existing language constructs. For example the loop above could be translated
       
   139 to the following \texttt{while}-loop:
       
   140 
       
   141 \begin{center}
       
   142 \begin{minipage}{6cm}
       
   143 \begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
       
   144 i := 2;
       
   145 while (i <= 4) do {
       
   146     write i;
       
   147     i := i + 1;
       
   148 }
       
   149 \end{lstlisting}
       
   150 \end{minipage}
       
   151 \end{center}
   139 
   152 
   140 
   153 
   141 
   154 
   142 \end{document}
   155 \end{document}
   143 
   156