| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 24 Oct 2025 10:52:05 +0100 | |
| changeset 1018 | ab6c61f82c91 | 
| parent 992 | c3dd3a98f919 | 
| permissions | -rw-r--r-- | 
| 630 | 1 | % !TEX program = xelatex | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 2 | \documentclass{article}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 3 | \usepackage{../style}
 | 
| 216 
f5ec7c597c5b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 4 | \usepackage{../langs}
 | 
| 918 | 5 | \usepackage[normalem]{ulem}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 6 | |
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 7 | \begin{document}
 | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 8 | |
| 748 | 9 | \section*{Coursework 2}
 | 
| 198 
f54972b0f641
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
182diff
changeset | 10 | |
| 835 | 11 | \noindent This coursework is worth 10\% and is due on \cwTWO{} at
 | 
| 877 | 12 | 16:00. You are asked to implement the Sulzmann \& Lu lexer for the | 
| 748 | 13 | WHILE language. You can do the implementation in any programming | 
| 14 | language you like, but you need to submit the source code with which | |
| 15 | you answered the questions, otherwise a mark of 0\% will be | |
| 968 | 16 | awarded. %You need to submit your written answers as pdf---see attached | 
| 17 | % questionaire. Code send as code. | |
| 18 | If you use Scala in your code, a | |
| 943 | 19 | good place to start is the file \texttt{lexer.sc} and
 | 
| 20 | \texttt{token.sc} uploaded to KEATS. The template file on Github is
 | |
| 968 | 21 | called \texttt{cw02.sc}. The example files are in the subdirectory
 | 
| 22 | \texttt{examples}. The main function that will be tested is
 | |
| 23 | called \texttt{tokenise}. The marks will be distributed such that
 | |
| 24 | 3 marks are given for the correct \texttt{WHILE\_REGS} regular
 | |
| 25 | expression; 5 marks for the correct \texttt{inj} and \texttt{mkeps}
 | |
| 26 | definitions; and two marks when \texttt{tokenise} produces the correct
 | |
| 969 | 27 | results for the example files. | 
| 968 | 28 | |
| 969 | 29 | \subsection*{Testing\alert}
 | 
| 30 | ||
| 31 | For the marking, the functions that will be tested are | |
| 32 | \texttt{tokenise}, \texttt{inj} and \texttt{mkeps}.
 | |
| 968 | 33 | |
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 34 | |
| 750 | 35 | \subsection*{Disclaimer\alert}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 36 | |
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 37 | It should be understood that the work you submit represents | 
| 992 | 38 | your own effort. | 
| 39 | %You have not copied from anyone else | |
| 40 | %including CoPilot, ChatGPT \& Co. | |
| 41 | An | |
| 363 
0d6deecdb2eb
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 42 | exception is the Scala code from KEATS and the code I showed | 
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 43 | during the lectures, which you can both freely use. You can | 
| 992 | 44 | also use your own code from CW~1. | 
| 918 | 45 | %But do not | 
| 46 | %be tempted to ask Github Copilot for help or do any other | |
| 47 | %shenanigans like this! | |
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 48 | |
| 986 | 49 | \subsection*{Task 1}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 50 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 51 | To implement a lexer for the WHILE language, you first | 
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 52 | need to design the appropriate regular expressions for the | 
| 748 | 53 | following eleven syntactic entities: | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 54 | |
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 55 | \begin{enumerate}
 | 
| 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 56 | \item keywords are | 
| 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 57 | |
| 748 | 58 | \begin{center}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 59 | \texttt{while}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 60 | \texttt{if}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 61 | \texttt{then}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 62 | \texttt{else}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 63 | \texttt{do}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 64 | \texttt{for}, 
 | 
| 977 | 65 | \texttt{upto}, 
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 66 | \texttt{true}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 67 | \texttt{false}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 68 | \texttt{read}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 69 | \texttt{write},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 70 | \texttt{skip}
 | 
| 748 | 71 | \end{center} 
 | 
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 72 | |
| 748 | 73 | \item operators are: | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 74 | \texttt{+}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 75 | \texttt{-}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 76 | \texttt{*}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 77 | \texttt{\%},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 78 | \texttt{/},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 79 | \texttt{==}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 80 | \texttt{!=}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 81 | \texttt{>}, 
 | 
| 748 | 82 | \texttt{<},
 | 
| 83 | \texttt{<=}, 
 | |
| 84 | \texttt{>=},
 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 85 | \texttt{:=},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 86 | \texttt{\&\&},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 87 | \texttt{||}
 | 
| 748 | 88 | |
| 89 | \item letters are uppercase and lowercase | |
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 90 | |
| 748 | 91 | \item symbols are letters plus the characters | 
| 92 |   \texttt{.},
 | |
| 93 |   \texttt{\_},
 | |
| 94 |   \texttt{>},
 | |
| 95 |   \texttt{<},
 | |
| 96 |   \texttt{=},
 | |
| 97 |   \texttt{;},
 | |
| 850 | 98 |   \texttt{,} (comma),
 | 
| 833 | 99 |   \texttt{$\backslash$} and
 | 
| 748 | 100 |   \texttt{:}
 | 
| 101 | ||
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 102 | \item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
 | 
| 934 | 103 | \item digits are \pcode{0} to \pcode{9}
 | 
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 104 | \item there are semicolons \texttt{;}
 | 
| 447 
68769db65185
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
428diff
changeset | 105 | \item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
 | 
| 845 | 106 |   \texttt{$\backslash$t} or \texttt{$\backslash$r}
 | 
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 107 | \item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
 | 
| 934 | 108 | or digits | 
| 992 | 109 | \item for numbers give | 
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 110 | a regular expression that can recognise \pcode{0}, but not numbers 
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 111 | with leading zeroes, such as \pcode{001}
 | 
| 934 | 112 | \item strings are enclosed by double quotes, like \texttt{"\ldots"}, and consisting of
 | 
| 113 |   symbols, digits, parentheses, whitespaces and \texttt{$\backslash$n} (note the latter is not the escaped version but \texttt{$\backslash$} followed by \texttt{n}, otherwise we would not be able to indicate in our strings when to write a newline).
 | |
| 946 | 114 | \item comments start with \texttt{//} and contain symbols, spaces, parentheses and digits until the end-of-the-line markers
 | 
| 934 | 115 | \item endo-of-line-markers are \texttt{$\backslash$n} and \texttt{$\backslash$r$\backslash$n}  
 | 
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 116 | \end{enumerate}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 117 | |
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 118 | \noindent | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 119 | You can use the basic regular expressions | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 120 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 121 | \[ | 
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 122 | \ZERO,\; \ONE,\; c,\; r_1 + r_2,\; r_1 \cdot r_2,\; r^* | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 123 | \] | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 124 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 125 | \noindent | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 126 | but also the following extended regular expressions | 
| 182 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 127 | |
| 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 128 | \begin{center}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 129 | \begin{tabular}{ll}
 | 
| 494 | 130 | $[c_1,c_2,\ldots,c_n]$ & a set of characters\\ | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 131 | $r^+$ & one or more times $r$\\ | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 132 | $r^?$ & optional $r$\\ | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 133 | $r^{\{n\}}$ & n-times $r$\\
 | 
| 182 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 134 | \end{tabular}
 | 
| 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 135 | \end{center}
 | 
| 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 136 | |
| 458 | 137 | \noindent | 
| 473 | 138 | Later on you will also need the record regular expression: | 
| 458 | 139 | |
| 140 | \begin{center}
 | |
| 141 | \begin{tabular}{ll}
 | |
| 142 | $REC(x:r)$ & record regular expression\\ | |
| 143 | \end{tabular}
 | |
| 144 | \end{center}
 | |
| 145 | ||
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 146 | \noindent Try to design your regular expressions to be as | 
| 494 | 147 | small as possible. For example you should use character sets | 
| 148 | for identifiers and numbers. Feel free to use the general | |
| 149 | character constructor \textit{CFUN} introduced in CW 1.
 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 150 | |
| 986 | 151 | \subsection*{Task 2}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 152 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 153 | Implement the Sulzmann \& Lu lexer from the lectures. For | 
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 154 | this you need to implement the functions $nullable$ and $der$ | 
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 155 | (you can use your code from CW~1), as well as $mkeps$ and | 
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 156 | $inj$. These functions need to be appropriately extended for | 
| 992 | 157 | the extended regular expressions from Task 1. The definitions | 
| 968 | 158 | you need to create are: | 
| 159 | ||
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 160 | |
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 161 | \begin{center}
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 162 | \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
 | 
| 494 | 163 | $mkeps([c_1,c_2,\ldots,c_n])$ & $\dn$ & $?$\\ | 
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 164 | $mkeps(r^+)$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 165 | $mkeps(r^?)$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 166 | $mkeps(r^{\{n\}})$             & $\dn$ & $?$\medskip\\
 | 
| 494 | 167 | $inj\, ([c_1,c_2,\ldots,c_n])\,c\,\ldots$ & $\dn$ & $?$\\ | 
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 168 | $inj\, (r^+)\,c\,\ldots$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 169 | $inj\, (r^?)\,c\,\ldots$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 170 | $inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & $?$\\
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 171 | \end{tabular}
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 172 | \end{center}
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 173 | |
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 174 | \noindent where $inj$ takes three arguments: a regular | 
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 175 | expression, a character and a value. Test your lexer code | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 176 | with at least the two small examples below: | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 177 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 178 | \begin{center}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 179 | \begin{tabular}{ll}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 180 | regex: & string:\smallskip\\ | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 181 | $a^{\{3\}}$ & $aaa$\\
 | 
| 458 | 182 | $(a + \ONE)^{\{3\}}$ & $aa$
 | 
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 183 | \end{tabular}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 184 | \end{center}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 185 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 186 | |
| 598 | 187 | \noindent Both strings should be successfully lexed by the | 
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 188 | respective regular expression, that means the lexer returns | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 189 | in both examples a value. | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 190 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 191 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 192 | Also add the record regular expression from the | 
| 968 | 193 | lectures to your lexer and complete the function | 
| 194 | \pcode{env} so that it returns all assignments from a value (this then 
 | |
| 195 | allows you to extract easily the tokens from a value in the next | |
| 986 | 196 | task).\medskip | 
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 197 | |
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 198 | \noindent | 
| 968 | 199 | Finally make that the function \texttt{lexing\_simp} generates
 | 
| 992 | 200 | with the regular expression from Task 1 for the string | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 201 | |
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 202 | \begin{center}
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 203 | \code{"read n;"}
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 204 | \end{center} 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 205 | |
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 206 | \noindent | 
| 968 | 207 | the following pairs: | 
| 208 | ||
| 209 | \begin{center}
 | |
| 210 | \texttt{List((k,read), (w, ), (i,n), (s,;))}
 | |
| 211 | \end{center} 
 | |
| 212 | ||
| 213 | ||
| 214 | ||
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 215 | |
| 333 
8890852e18b7
updated coursework
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
328diff
changeset | 216 | |
| 986 | 217 | \subsection*{Task 3}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 218 | |
| 992 | 219 | Make sure your lexer from Task 2 also simplifies regular expressions after | 
| 968 | 220 | each derivation step and rectifies the computed values after each | 
| 221 | injection. Use this lexer to tokenise the six WHILE programs | |
| 222 | in the \texttt{examples} directory. Make sure that the \texttt{tokenise}
 | |
| 223 | function filters out whitespaces and comments.\bigskip | |
| 182 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 224 | |
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 225 | |
| 968 | 226 | % \begin{figure}[h]
 | 
| 227 | % \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/fib.while}}
 | |
| 228 | % \caption{Fibonacci program in the WHILE language.\label{fib}}
 | |
| 229 | % \end{figure}
 | |
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 230 | |
| 968 | 231 | % \begin{figure}[h]
 | 
| 232 | % \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/loops.while}}
 | |
| 233 | % \caption{The three-nested-loops program in the WHILE language. 
 | |
| 234 | % (Usually used for timing measurements.)\label{loop}}
 | |
| 235 | % \end{figure}
 | |
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 236 | |
| 968 | 237 | % \begin{figure}[h]
 | 
| 238 | % \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/factors.while}}
 | |
| 239 | % \caption{A program that calculates factors for numbers in the WHILE
 | |
| 240 | %   language.\label{factors}}
 | |
| 241 | % \end{figure}
 | |
| 659 | 242 | |
| 968 | 243 | % \begin{figure}[h]
 | 
| 244 | % \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/collatz2.while}}
 | |
| 245 | % \caption{A program that calculates the Collatz series for numbers
 | |
| 246 | %   between 1 and 100.\label{collatz}}
 | |
| 247 | % \end{figure}
 | |
| 748 | 248 | |
| 968 | 249 | % \clearpage | 
| 250 | % \newpage | |
| 251 | % \section*{Answers}
 | |
| 918 | 252 | |
| 968 | 253 | % \mbox{}
 | 
| 918 | 254 | |
| 968 | 255 | % \noindent | 
| 986 | 256 | % \textbf{Task 2:}\\ (Use mathematical notation, such as $r^+$, rather than code, such as \code{PLUS(r)})
 | 
| 918 | 257 | |
| 968 | 258 | % \begin{center}
 | 
| 259 | %   \def\arraystretch{1.6}  
 | |
| 260 | % \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
 | |
| 261 | % $mkeps([c_1,c_2,\ldots,c_n])$  & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 262 | % $mkeps(r^+)$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 263 | % $mkeps(r^?)$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 264 | % $mkeps(r^{\{n\}})$             & $\dn$ & \uline{\hspace{8cm}}\bigskip\\
 | |
| 265 | % $inj\, ([c_1,c_2,\ldots,c_n])\,c\,\ldots$  & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 266 | % $inj\, (r^+)\,c\,\ldots$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 267 | % $inj\, (r^?)\,c\,\ldots$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 268 | % $inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 269 | % \end{tabular}
 | |
| 270 | % \end{center}\bigskip
 | |
| 918 | 271 | |
| 968 | 272 | % \noindent | 
| 273 | % Tokens for \code{"read n;"}\\
 | |
| 918 | 274 | |
| 968 | 275 | % \noindent | 
| 276 | % \uline{\hfill}\medskip
 | |
| 918 | 277 | |
| 968 | 278 | % \noindent | 
| 279 | % \uline{\hfill}\medskip
 | |
| 918 | 280 | |
| 968 | 281 | % \noindent | 
| 282 | % \uline{\hfill}\medskip
 | |
| 918 | 283 | |
| 968 | 284 | % \noindent | 
| 285 | % \uline{\hfill}\medskip
 | |
| 918 | 286 | |
| 287 | ||
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 288 | \end{document}
 | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 289 | |
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 290 | %%% Local Variables: | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 291 | %%% mode: latex | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 292 | %%% TeX-master: t | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 293 | %%% End: |