| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 13 Oct 2023 15:07:37 +0100 | |
| changeset 940 | 1c1fbf45a03c | 
| parent 933 | 4d9674598682 | 
| child 942 | 7f52427568ff | 
| 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 | |
| 918 | 16 | awarded. You need to submit your written | 
| 17 | answers as pdf---see attached questionaire. Code send as code. If you use | |
| 933 | 18 | Scala in your code, a good place to start is the file \texttt{lexer.sc}
 | 
| 19 | and \texttt{token.sc}
 | |
| 20 | that are uploaded to Github. | |
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 21 | |
| 750 | 22 | \subsection*{Disclaimer\alert}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 23 | |
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 24 | It should be understood that the work you submit represents | 
| 918 | 25 | your own effort. You have not copied from anyone else | 
| 26 | including CoPilot, ChatGPT \& Co. An | |
| 363 
0d6deecdb2eb
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 27 | 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 | 28 | during the lectures, which you can both freely use. You can | 
| 918 | 29 | also use your own code from the CW~1. | 
| 30 | %But do not | |
| 31 | %be tempted to ask Github Copilot for help or do any other | |
| 32 | %shenanigans like this! | |
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 33 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 34 | \subsection*{Question 1}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 35 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 36 | 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 | 37 | need to design the appropriate regular expressions for the | 
| 748 | 38 | following eleven syntactic entities: | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 39 | |
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 40 | \begin{enumerate}
 | 
| 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 41 | \item keywords are | 
| 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 42 | |
| 748 | 43 | \begin{center}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 44 | \texttt{while}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 45 | \texttt{if}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 46 | \texttt{then}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 47 | \texttt{else}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 48 | \texttt{do}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 49 | \texttt{for}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 50 | \texttt{to}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 51 | \texttt{true}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 52 | \texttt{false}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 53 | \texttt{read}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 54 | \texttt{write},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 55 | \texttt{skip}
 | 
| 748 | 56 | \end{center} 
 | 
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 57 | |
| 748 | 58 | \item operators are: | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 59 | \texttt{+}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 60 | \texttt{-}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 61 | \texttt{*}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 62 | \texttt{\%},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 63 | \texttt{/},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 64 | \texttt{==}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 65 | \texttt{!=}, 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 66 | \texttt{>}, 
 | 
| 748 | 67 | \texttt{<},
 | 
| 68 | \texttt{<=}, 
 | |
| 69 | \texttt{>=},
 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 70 | \texttt{:=},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 71 | \texttt{\&\&},
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 72 | \texttt{||}
 | 
| 748 | 73 | |
| 74 | \item letters are uppercase and lowercase | |
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 75 | |
| 748 | 76 | \item symbols are letters plus the characters | 
| 77 |   \texttt{.},
 | |
| 78 |   \texttt{\_},
 | |
| 79 |   \texttt{>},
 | |
| 80 |   \texttt{<},
 | |
| 81 |   \texttt{=},
 | |
| 82 |   \texttt{;},
 | |
| 850 | 83 |   \texttt{,} (comma),
 | 
| 833 | 84 |   \texttt{$\backslash$} and
 | 
| 748 | 85 |   \texttt{:}
 | 
| 86 | ||
| 180 
50e8dcd95ae3
added cw
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
179diff
changeset | 87 | \item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
 | 
| 933 | 88 | \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 | 89 | \item there are semicolons \texttt{;}
 | 
| 447 
68769db65185
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
428diff
changeset | 90 | \item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
 | 
| 845 | 91 |   \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 | 92 | \item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
 | 
| 933 | 93 | or digits | 
| 94 | \item numbers for numbers give | |
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 95 | 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 | 96 | with leading zeroes, such as \pcode{001}
 | 
| 933 | 97 | \item strings are enclosed by double quotes, like \texttt{"\ldots"}, and consisting of
 | 
| 98 |   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).
 | |
| 99 | \item comments start with \texttt{//} and contain symbols, spaces and digits until the end-of-the-line markers
 | |
| 100 | \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 | 101 | \end{enumerate}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 102 | |
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 103 | \noindent | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 104 | You can use the basic regular expressions | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 105 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 106 | \[ | 
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 107 | \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 | 108 | \] | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 109 | |
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 110 | \noindent | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 111 | but also the following extended regular expressions | 
| 182 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 112 | |
| 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 113 | \begin{center}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 114 | \begin{tabular}{ll}
 | 
| 494 | 115 | $[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 | 116 | $r^+$ & one or more times $r$\\ | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 117 | $r^?$ & optional $r$\\ | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 118 | $r^{\{n\}}$ & n-times $r$\\
 | 
| 182 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 119 | \end{tabular}
 | 
| 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 120 | \end{center}
 | 
| 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 121 | |
| 458 | 122 | \noindent | 
| 473 | 123 | Later on you will also need the record regular expression: | 
| 458 | 124 | |
| 125 | \begin{center}
 | |
| 126 | \begin{tabular}{ll}
 | |
| 127 | $REC(x:r)$ & record regular expression\\ | |
| 128 | \end{tabular}
 | |
| 129 | \end{center}
 | |
| 130 | ||
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 131 | \noindent Try to design your regular expressions to be as | 
| 494 | 132 | small as possible. For example you should use character sets | 
| 133 | for identifiers and numbers. Feel free to use the general | |
| 134 | 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 | 135 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 136 | \subsection*{Question 2}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 137 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 138 | 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 | 139 | 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 | 140 | (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 | 141 | $inj$. These functions need to be appropriately extended for | 
| 918 | 142 | the extended regular expressions from Q1. Write down in the | 
| 143 | questionaire at the end the | |
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 144 | clauses for | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 145 | |
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 146 | \begin{center}
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 147 | \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
 | 
| 494 | 148 | $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 | 149 | $mkeps(r^+)$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 150 | $mkeps(r^?)$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 151 | $mkeps(r^{\{n\}})$             & $\dn$ & $?$\medskip\\
 | 
| 494 | 152 | $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 | 153 | $inj\, (r^+)\,c\,\ldots$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 154 | $inj\, (r^?)\,c\,\ldots$ & $\dn$ & $?$\\ | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 155 | $inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & $?$\\
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 156 | \end{tabular}
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 157 | \end{center}
 | 
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 158 | |
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 159 | \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 | 160 | 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 | 161 | with at least the two small examples below: | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 162 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 163 | \begin{center}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 164 | \begin{tabular}{ll}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 165 | regex: & string:\smallskip\\ | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 166 | $a^{\{3\}}$ & $aaa$\\
 | 
| 458 | 167 | $(a + \ONE)^{\{3\}}$ & $aa$
 | 
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 168 | \end{tabular}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 169 | \end{center}
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 170 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 171 | |
| 598 | 172 | \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 | 173 | respective regular expression, that means the lexer returns | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 174 | in both examples a value. | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 175 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 176 | |
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 177 | Also add the record regular expression from the | 
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 178 | lectures to your lexer and implement a function, say | 
| 396 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 179 | \pcode{env}, that returns all assignments from a value (such
 | 
| 
4cd75c619e06
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
395diff
changeset | 180 | that you can extract easily the tokens from a value).\medskip | 
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 181 | |
| 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 182 | \noindent | 
| 933 | 183 | Finally give \textbf{all} the tokens for your regular expressions from Q1 and the
 | 
| 369 
43c0ed473720
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
364diff
changeset | 184 | string | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 185 | |
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 186 | \begin{center}
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 187 | \code{"read n;"}
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 188 | \end{center} 
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 189 | |
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 190 | \noindent | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 191 | and use your \pcode{env} function to give the token sequence.
 | 
| 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 192 | |
| 333 
8890852e18b7
updated coursework
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
328diff
changeset | 193 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
396diff
changeset | 194 | \subsection*{Question 3}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 195 | |
| 748 | 196 | Extend your lexer from Q2 to also simplify regular expressions after | 
| 197 | each derivation step and rectify the computed values after each | |
| 933 | 198 | injection. Use this lexer to tokenize six WHILE programs some of which | 
| 199 | are given in Figures~\ref{fib} -- \ref{collatz}. You can find these programms also on
 | |
| 200 | Github under the \texttt{cw2} directory. Give the tokens of these
 | |
| 201 | programs where whitespaces and comments are | |
| 748 | 202 | filtered out. Make sure you can tokenise \textbf{exactly} these
 | 
| 203 | programs.\bigskip | |
| 182 
9ce2414e470e
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
181diff
changeset | 204 | |
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 205 | |
| 578 | 206 | \begin{figure}[h]
 | 
| 860 | 207 | \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/fib.while}}
 | 
| 181 
1f98d215df71
added material
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
180diff
changeset | 208 | \caption{Fibonacci program in the WHILE language.\label{fib}}
 | 
| 
1f98d215df71
added material
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
180diff
changeset | 209 | \end{figure}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 210 | |
| 578 | 211 | \begin{figure}[h]
 | 
| 860 | 212 | \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/loops.while}}
 | 
| 275 
618c7640cf66
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
216diff
changeset | 213 | \caption{The three-nested-loops program in the WHILE language. 
 | 
| 578 | 214 | (Usually used for timing measurements.)\label{loop}}
 | 
| 181 
1f98d215df71
added material
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
180diff
changeset | 215 | \end{figure}
 | 
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 216 | |
| 659 | 217 | \begin{figure}[h]
 | 
| 860 | 218 | \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/factors.while}}
 | 
| 659 | 219 | \caption{A program that calculates factors for numbers in the WHILE
 | 
| 220 |   language.\label{factors}}
 | |
| 221 | \end{figure}
 | |
| 222 | ||
| 748 | 223 | \begin{figure}[h]
 | 
| 933 | 224 | \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/collatz2.while}}
 | 
| 748 | 225 | \caption{A program that calculates the Collatz series for numbers
 | 
| 226 |   between 1 and 100.\label{collatz}}
 | |
| 227 | \end{figure}
 | |
| 228 | ||
| 918 | 229 | \clearpage | 
| 230 | \newpage | |
| 231 | \section*{Answers}
 | |
| 232 | ||
| 233 | \mbox{}
 | |
| 234 | ||
| 235 | \noindent | |
| 933 | 236 | \textbf{Question 2:}\\ (Use mathematical notation, such as $r^+$, rather than code, such as \code{PLUS(r)})
 | 
| 918 | 237 | |
| 238 | \begin{center}
 | |
| 239 |   \def\arraystretch{1.6}  
 | |
| 240 | \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
 | |
| 241 | $mkeps([c_1,c_2,\ldots,c_n])$  & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 242 | $mkeps(r^+)$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 243 | $mkeps(r^?)$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 244 | $mkeps(r^{\{n\}})$             & $\dn$ & \uline{\hspace{8cm}}\bigskip\\
 | |
| 245 | $inj\, ([c_1,c_2,\ldots,c_n])\,c\,\ldots$  & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 246 | $inj\, (r^+)\,c\,\ldots$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 247 | $inj\, (r^?)\,c\,\ldots$                   & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 248 | $inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & \uline{\hspace{8cm}}\\
 | |
| 249 | \end{tabular}
 | |
| 250 | \end{center}\bigskip
 | |
| 251 | ||
| 252 | \noindent | |
| 253 | Tokens for \code{"read n;"}\\
 | |
| 254 | ||
| 255 | \noindent | |
| 256 | \uline{\hfill}\medskip
 | |
| 257 | ||
| 258 | \noindent | |
| 259 | \uline{\hfill}\medskip
 | |
| 260 | ||
| 261 | \noindent | |
| 262 | \uline{\hfill}\medskip
 | |
| 263 | ||
| 264 | \noindent | |
| 265 | \uline{\hfill}\medskip
 | |
| 266 | ||
| 267 | ||
| 178 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 268 | \end{document}
 | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 269 | |
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 270 | %%% Local Variables: | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 271 | %%% mode: latex | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 272 | %%% TeX-master: t | 
| 
d36363d648e3
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 273 | %%% End: |