author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 22 Nov 2021 11:24:08 +0000 | |
changeset 850 | ea320c9427d2 |
parent 845 | ddd9659971ec |
child 860 | 6f80e6df34f7 |
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:
216
diff
changeset
|
3 |
\usepackage{../style} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
4 |
\usepackage{../langs} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
\begin{document} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
748 | 8 |
\section*{Coursework 2} |
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
9 |
|
835 | 10 |
\noindent This coursework is worth 10\% and is due on \cwTWO{} at |
748 | 11 |
18:00. You are asked to implement the Sulzmann \& Lu lexer for the |
12 |
WHILE language. You can do the implementation in any programming |
|
13 |
language you like, but you need to submit the source code with which |
|
14 |
you answered the questions, otherwise a mark of 0\% will be |
|
15 |
awarded. You can submit your answers in a txt-file or as pdf. Code |
|
16 |
submit as code. Please package everything in a zip-file that creates a |
|
17 |
directory with the name \texttt{YournameYourfamilyname} on my end. Thanks! |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
18 |
|
750 | 19 |
\subsection*{Disclaimer\alert} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
21 |
It should be understood that the work you submit represents |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
22 |
your own effort. You have not copied from anyone else. An |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
23 |
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:
396
diff
changeset
|
24 |
during the lectures, which you can both freely use. You can |
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
25 |
also use your own code from the CW~1. |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
27 |
\subsection*{Question 1} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
29 |
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:
333
diff
changeset
|
30 |
need to design the appropriate regular expressions for the |
748 | 31 |
following eleven syntactic entities: |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
33 |
\begin{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
34 |
\item keywords are |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
35 |
|
748 | 36 |
\begin{center} |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
37 |
\texttt{while}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
38 |
\texttt{if}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
39 |
\texttt{then}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
40 |
\texttt{else}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
41 |
\texttt{do}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
42 |
\texttt{for}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
43 |
\texttt{to}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
44 |
\texttt{true}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
45 |
\texttt{false}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
46 |
\texttt{read}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
47 |
\texttt{write}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
48 |
\texttt{skip} |
748 | 49 |
\end{center} |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
50 |
|
748 | 51 |
\item operators are: |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
52 |
\texttt{+}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
53 |
\texttt{-}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
54 |
\texttt{*}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
55 |
\texttt{\%}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
56 |
\texttt{/}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
57 |
\texttt{==}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
58 |
\texttt{!=}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
59 |
\texttt{>}, |
748 | 60 |
\texttt{<}, |
61 |
\texttt{<=}, |
|
62 |
\texttt{>=}, |
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
63 |
\texttt{:=}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
64 |
\texttt{\&\&}, |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
65 |
\texttt{||} |
748 | 66 |
|
67 |
\item letters are uppercase and lowercase |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
68 |
|
748 | 69 |
\item symbols are letters plus the characters |
70 |
\texttt{.}, |
|
71 |
\texttt{\_}, |
|
72 |
\texttt{>}, |
|
73 |
\texttt{<}, |
|
74 |
\texttt{=}, |
|
75 |
\texttt{;}, |
|
850 | 76 |
\texttt{,} (comma), |
833 | 77 |
\texttt{$\backslash$} and |
748 | 78 |
\texttt{:} |
79 |
||
850 | 80 |
\item strings are enclosed by double quotes, like \texttt{"\ldots"}, and consisting of |
748 | 81 |
symbols, whitespaces and digits |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
82 |
\item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
83 |
\item there are semicolons \texttt{;} |
447
68769db65185
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
428
diff
changeset
|
84 |
\item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or |
845 | 85 |
\texttt{$\backslash$t} or \texttt{$\backslash$r} |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
86 |
\item identifiers are letters followed by underscores \texttt{\_\!\_}, letters |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
87 |
or digits |
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
88 |
\item numbers are \pcode{0}, \pcode{1}, \ldots and so on; give |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
89 |
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:
395
diff
changeset
|
90 |
with leading zeroes, such as \pcode{001} |
748 | 91 |
\item comments start with \texttt{//} and contain symbols, spaces and digits until the end of the line |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
92 |
\end{enumerate} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
\noindent |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
95 |
You can use the basic regular expressions |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
97 |
\[ |
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
98 |
\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:
216
diff
changeset
|
99 |
\] |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
101 |
\noindent |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
102 |
but also the following extended regular expressions |
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
103 |
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
104 |
\begin{center} |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
105 |
\begin{tabular}{ll} |
494 | 106 |
$[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:
216
diff
changeset
|
107 |
$r^+$ & one or more times $r$\\ |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
108 |
$r^?$ & optional $r$\\ |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
109 |
$r^{\{n\}}$ & n-times $r$\\ |
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
110 |
\end{tabular} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
111 |
\end{center} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
112 |
|
458 | 113 |
\noindent |
473 | 114 |
Later on you will also need the record regular expression: |
458 | 115 |
|
116 |
\begin{center} |
|
117 |
\begin{tabular}{ll} |
|
118 |
$REC(x:r)$ & record regular expression\\ |
|
119 |
\end{tabular} |
|
120 |
\end{center} |
|
121 |
||
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
122 |
\noindent Try to design your regular expressions to be as |
494 | 123 |
small as possible. For example you should use character sets |
124 |
for identifiers and numbers. Feel free to use the general |
|
125 |
character constructor \textit{CFUN} introduced in CW 1. |
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
126 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
127 |
\subsection*{Question 2} |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
128 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
129 |
Implement the Sulzmann \& Lu lexer from the lectures. For |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
130 |
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:
364
diff
changeset
|
131 |
(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:
333
diff
changeset
|
132 |
$inj$. These functions need to be appropriately extended for |
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
133 |
the extended regular expressions from Q1. Write down the |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
134 |
clauses for |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
135 |
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
136 |
\begin{center} |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
137 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
494 | 138 |
$mkeps([c_1,c_2,\ldots,c_n])$ & $\dn$ & $?$\\ |
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
139 |
$mkeps(r^+)$ & $\dn$ & $?$\\ |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
140 |
$mkeps(r^?)$ & $\dn$ & $?$\\ |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
141 |
$mkeps(r^{\{n\}})$ & $\dn$ & $?$\medskip\\ |
494 | 142 |
$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:
364
diff
changeset
|
143 |
$inj\, (r^+)\,c\,\ldots$ & $\dn$ & $?$\\ |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
144 |
$inj\, (r^?)\,c\,\ldots$ & $\dn$ & $?$\\ |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
145 |
$inj\, (r^{\{n\}})\,c\,\ldots$ & $\dn$ & $?$\\ |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
146 |
\end{tabular} |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
147 |
\end{center} |
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
148 |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
149 |
\noindent where $inj$ takes three arguments: a regular |
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
150 |
expression, a character and a value. Test your lexer code |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
151 |
with at least the two small examples below: |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
152 |
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
153 |
\begin{center} |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
154 |
\begin{tabular}{ll} |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
155 |
regex: & string:\smallskip\\ |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
156 |
$a^{\{3\}}$ & $aaa$\\ |
458 | 157 |
$(a + \ONE)^{\{3\}}$ & $aa$ |
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
158 |
\end{tabular} |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
159 |
\end{center} |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
160 |
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
161 |
|
598 | 162 |
\noindent Both strings should be successfully lexed by the |
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
163 |
respective regular expression, that means the lexer returns |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
164 |
in both examples a value. |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
165 |
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
166 |
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
167 |
Also add the record regular expression from the |
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
168 |
lectures to your lexer and implement a function, say |
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
169 |
\pcode{env}, that returns all assignments from a value (such |
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
170 |
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:
364
diff
changeset
|
171 |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
172 |
\noindent |
384
4629448c1bd9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
173 |
Finally give 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:
364
diff
changeset
|
174 |
string |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
175 |
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
176 |
\begin{center} |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
177 |
\code{"read n;"} |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
178 |
\end{center} |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
179 |
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
180 |
\noindent |
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
181 |
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:
216
diff
changeset
|
182 |
|
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
183 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
184 |
\subsection*{Question 3} |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
185 |
|
748 | 186 |
Extend your lexer from Q2 to also simplify regular expressions after |
187 |
each derivation step and rectify the computed values after each |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
188 |
injection. Use this lexer to tokenize the programs in |
748 | 189 |
Figures~\ref{fib} -- \ref{collatz}. You can find the programms also on |
190 |
KEATS. Give the tokens of these programs where whitespaces are |
|
191 |
filtered out. Make sure you can tokenise \textbf{exactly} these |
|
192 |
programs.\bigskip |
|
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
193 |
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
|
578 | 195 |
\begin{figure}[h] |
748 | 196 |
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/fib.while}} |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
197 |
\caption{Fibonacci program in the WHILE language.\label{fib}} |
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
198 |
\end{figure} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
|
578 | 200 |
\begin{figure}[h] |
748 | 201 |
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/loops.while}} |
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
202 |
\caption{The three-nested-loops program in the WHILE language. |
578 | 203 |
(Usually used for timing measurements.)\label{loop}} |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
204 |
\end{figure} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
205 |
|
659 | 206 |
\begin{figure}[h] |
748 | 207 |
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/factors.while}} |
659 | 208 |
\caption{A program that calculates factors for numbers in the WHILE |
209 |
language.\label{factors}} |
|
210 |
\end{figure} |
|
211 |
||
748 | 212 |
\begin{figure}[h] |
213 |
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/collatz2.while}} |
|
214 |
\caption{A program that calculates the Collatz series for numbers |
|
215 |
between 1 and 100.\label{collatz}} |
|
216 |
\end{figure} |
|
217 |
||
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
218 |
\end{document} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
219 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
220 |
%%% Local Variables: |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
221 |
%%% mode: latex |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
222 |
%%% TeX-master: t |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
223 |
%%% End: |