coursework/cw02.tex
changeset 748 383f2a5952ce
parent 719 0de3527e6ae3
child 750 e93a9e74ca8e
equal deleted inserted replaced
747:077fc2e5412c 748:383f2a5952ce
     3 \usepackage{../style}
     3 \usepackage{../style}
     4 \usepackage{../langs}
     4 \usepackage{../langs}
     5 
     5 
     6 \begin{document}
     6 \begin{document}
     7 
     7 
     8 \section*{Coursework 2 (Strand 1)}
     8 \section*{Coursework 2}
     9 
     9 
    10 \noindent This coursework is worth 5\% and is due on
    10 \noindent This coursework is worth 8\% and is due on \cwTWO{} at
    11 \cwTWO{} at 18:00. You are asked to implement the Sulzmann \&
    11 18:00. You are asked to implement the Sulzmann \& Lu lexer for the
    12 Lu lexer for the WHILE language. You can do the
    12 WHILE language. You can do the implementation in any programming
    13 implementation in any programming language you like, but you
    13 language you like, but you need to submit the source code with which
    14 need to submit the source code with which you answered the
    14 you answered the questions, otherwise a mark of 0\% will be
    15 questions, otherwise a mark of 0\% will be awarded. You can
    15 awarded. You can submit your answers in a txt-file or as pdf. Code
    16 submit your answers in a txt-file or as pdf. Code submit as 
    16 submit as code. Please package everything in a zip-file that creates a
    17 code.
    17 directory with the name \texttt{YournameYourfamilyname} on my end. Thanks!
    18 
    18 
    19 \subsection*{Disclaimer}
    19 \subsection*{Disclaimer}
    20 
    20 
    21 It should be understood that the work you submit represents
    21 It should be understood that the work you submit represents
    22 your own effort. You have not copied from anyone else. An
    22 your own effort. You have not copied from anyone else. An
    26 
    26 
    27 \subsection*{Question 1}
    27 \subsection*{Question 1}
    28 
    28 
    29 To implement a lexer for the WHILE language, you first
    29 To implement a lexer for the WHILE language, you first
    30 need to design the appropriate regular expressions for the
    30 need to design the appropriate regular expressions for the
    31 following eight syntactic entities:
    31 following eleven syntactic entities:
    32 
    32 
    33 \begin{enumerate}
    33 \begin{enumerate}
    34 \item keywords are
    34 \item keywords are
    35 
    35 
    36 \begin{quote}
    36 \begin{center}
    37 \texttt{while}, 
    37 \texttt{while}, 
    38 \texttt{if}, 
    38 \texttt{if}, 
    39 \texttt{then}, 
    39 \texttt{then}, 
    40 \texttt{else}, 
    40 \texttt{else}, 
    41 \texttt{do}, 
    41 \texttt{do}, 
    44 \texttt{true}, 
    44 \texttt{true}, 
    45 \texttt{false}, 
    45 \texttt{false}, 
    46 \texttt{read}, 
    46 \texttt{read}, 
    47 \texttt{write},
    47 \texttt{write},
    48 \texttt{skip}
    48 \texttt{skip}
    49 \end{quote} 
    49 \end{center} 
    50 
    50 
    51 \item operators are
    51 \item operators are:
    52 
       
    53 \begin{quote}
       
    54 \texttt{+}, 
    52 \texttt{+}, 
    55 \texttt{-}, 
    53 \texttt{-}, 
    56 \texttt{*}, 
    54 \texttt{*}, 
    57 \texttt{\%},
    55 \texttt{\%},
    58 \texttt{/},
    56 \texttt{/},
    59 \texttt{==}, 
    57 \texttt{==}, 
    60 \texttt{!=}, 
    58 \texttt{!=}, 
    61 \texttt{>}, 
    59 \texttt{>}, 
    62 \texttt{<}, 
    60 \texttt{<},
       
    61 \texttt{<=}, 
       
    62 \texttt{>=},
    63 \texttt{:=},
    63 \texttt{:=},
    64 \texttt{\&\&},
    64 \texttt{\&\&},
    65 \texttt{||}
    65 \texttt{||}
    66 \end{quote} 
    66 
    67 
    67 \item letters are uppercase and lowercase
    68 \item strings are enclosed by \texttt{"\ldots"} 
    68 
       
    69 \item symbols are letters plus the characters
       
    70   \texttt{.},
       
    71   \texttt{\_},
       
    72   \texttt{>},
       
    73   \texttt{<},
       
    74   \texttt{=},
       
    75   \texttt{;},
       
    76   \texttt{,} and
       
    77   \texttt{:}
       
    78 
       
    79 \item strings are enclosed by \texttt{"\ldots"} and consisting of
       
    80   symbols, whitespaces and digits
    69 \item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
    81 \item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
    70 \item there are semicolons \texttt{;}
    82 \item there are semicolons \texttt{;}
    71 \item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
    83 \item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
    72   \texttt{$\backslash$t}
    84   \texttt{$\backslash$t}
    73 \item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
    85 \item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
    74 or digits
    86 or digits
    75 \item numbers are \pcode{0}, \pcode{1}, \ldots and so on; give 
    87 \item numbers are \pcode{0}, \pcode{1}, \ldots and so on; give 
    76 a regular expression that can recognise \pcode{0}, but not numbers 
    88 a regular expression that can recognise \pcode{0}, but not numbers 
    77 with leading zeroes, such as \pcode{001}
    89 with leading zeroes, such as \pcode{001}
       
    90 \item comments start with \texttt{//} and contain symbols, spaces and digits until the end of the line
    78 \end{enumerate}
    91 \end{enumerate}
    79 
    92 
    80 \noindent
    93 \noindent
    81 You can use the basic regular expressions 
    94 You can use the basic regular expressions 
    82 
    95 
   167 and use your \pcode{env} function to give the token sequence.
   180 and use your \pcode{env} function to give the token sequence.
   168 
   181 
   169 
   182 
   170 \subsection*{Question 3}
   183 \subsection*{Question 3}
   171 
   184 
   172 Extend your lexer from Q2 to also simplify regular expressions
   185 Extend your lexer from Q2 to also simplify regular expressions after
   173 after each derivation step and rectify the computed values after each
   186 each derivation step and rectify the computed values after each
   174 injection. Use this lexer to tokenize the programs in
   187 injection. Use this lexer to tokenize the programs in
   175 Figure~\ref{fib}, \ref{loop} and \ref{factors}. Give the tokens of these
   188 Figures~\ref{fib} -- \ref{collatz}. You can find the programms also on
   176 programs where whitespaces are filtered out. Make sure you can
   189 KEATS. Give the tokens of these programs where whitespaces are
   177 tokenise \textbf{exactly} these programs.\bigskip
   190 filtered out. Make sure you can tokenise \textbf{exactly} these
   178 
   191 programs.\bigskip
   179 
   192 
   180 \begin{figure}[h]
   193 
   181 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/fib.while}}
   194 \begin{figure}[h]
       
   195 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/fib.while}}
   182 \caption{Fibonacci program in the WHILE language.\label{fib}}
   196 \caption{Fibonacci program in the WHILE language.\label{fib}}
   183 \end{figure}
   197 \end{figure}
   184 
   198 
   185 \begin{figure}[h]
   199 \begin{figure}[h]
   186 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/loops.while}}
   200 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/loops.while}}
   187 \caption{The three-nested-loops program in the WHILE language. 
   201 \caption{The three-nested-loops program in the WHILE language. 
   188 (Usually used for timing measurements.)\label{loop}}
   202 (Usually used for timing measurements.)\label{loop}}
   189 \end{figure}
   203 \end{figure}
   190 
   204 
   191 \begin{figure}[h]
   205 \begin{figure}[h]
   192 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/factors.while}}
   206 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/factors.while}}
   193 \caption{A program that calculates factors for numbers in the WHILE
   207 \caption{A program that calculates factors for numbers in the WHILE
   194   language.\label{factors}}
   208   language.\label{factors}}
       
   209 \end{figure}
       
   210 
       
   211 \begin{figure}[h]
       
   212 \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/collatz2.while}}
       
   213 \caption{A program that calculates the Collatz series for numbers
       
   214   between 1 and 100.\label{collatz}}
   195 \end{figure}
   215 \end{figure}
   196 
   216 
   197 \end{document}
   217 \end{document}
   198 
   218 
   199 %%% Local Variables: 
   219 %%% Local Variables: