author | Christian Urban <christian.urban@kcl.ac.uk> |
Wed, 16 Oct 2024 13:14:13 +0100 | |
changeset 968 | d8d8911a3d6f |
parent 956 | ae9782e62bdd |
permissions | -rw-r--r-- |
630 | 1 |
% !TEX program = xelatex |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\documentclass{article} |
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
3 |
\usepackage{../style} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
214
diff
changeset
|
4 |
\usepackage{../langs} |
950 | 5 |
\usepackage[normalem]{ulem} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
9 |
\section*{Coursework 3} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
722 | 11 |
|
12 |
||
750 | 13 |
\noindent This coursework is worth 10\% and is due on \cwTHREE{} at |
877 | 14 |
16:00. You are asked to implement a parser for the WHILE language and |
968 | 15 |
also an interpreter. The parser needs to use parser combinators. You |
16 |
can do the implementation in any programming language you like, but |
|
17 |
you need to submit the source code with which you answered the |
|
18 |
questions, otherwise a mark of 0\% will be awarded. If you use Scala |
|
19 |
in your code, a good place to start is the file \texttt{comb1.sc} and |
|
20 |
\texttt{comb2.sc} uploaded to KEATS. Feel free to use the ``hack'' |
|
21 |
explained during the lectures. This might make your grammar |
|
22 |
simpler. However, make sure you understand the code involved in the |
|
23 |
``hack'' because if you just do ``mix-and-match'' you will receive |
|
24 |
strange errors. The main function that will be tested is called |
|
25 |
\texttt{eval} and \texttt{Stmts.parse\_all}. The latter expects a list |
|
26 |
of tokens as input and generates an AST. The former expects an AST and |
|
27 |
``runs'' the program. The marks will be distributed such that 6 marks |
|
28 |
are given for the correct grammar (and parsers); 4 marks for the correct |
|
29 |
\texttt{eval} function. You should use the lexer from CW2 for the |
|
30 |
parser - you potentially need to make additions for CW2. |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
|
750 | 32 |
\subsection*{Disclaimer\alert} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
33 |
|
750 | 34 |
It should be understood that the work you submit represents your own |
35 |
effort. You have not copied from anyone else. An exception is the |
|
36 |
Scala code I showed during the lectures or uploaded to KEATS, which |
|
37 |
you can both use. You can also use your own code from the CW~1 and |
|
886 | 38 |
CW~2. But do not |
39 |
be tempted to ask Github Copilot for help or do any other |
|
40 |
shenanigans like this! |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
|
968 | 42 |
\subsection*{Syntax Error in Template File cw03.sc\alert} |
43 |
||
44 |
Apologies, there is a small syntax error in the template file where a variable |
|
45 |
needs to be called \texttt{tks} instead of \texttt{tk}. The code |
|
46 |
in question is at the end of \texttt{cw03.sc} and should be like |
|
47 |
this (see lines 5, 6 and 8): |
|
48 |
||
49 |
\begin{lstlisting}[language=Scala,numbers=left] |
|
50 |
@main |
|
51 |
def test(file: String) = { |
|
52 |
val contents = os.read(os.pwd / "examples" / file) |
|
53 |
println(s"Lex $file: ") |
|
54 |
val tks = tokenise(contents) |
|
55 |
println(tks.mkString(",")) |
|
56 |
println(s"Parse $file: ") |
|
57 |
val ast = Stmts.parse_all(tks).head |
|
58 |
println(ast) |
|
59 |
println(s"Eval $file: ") |
|
60 |
println(eval(ast)) |
|
61 |
} |
|
62 |
\end{lstlisting} |
|
63 |
||
64 |
||
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
65 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
66 |
\subsection*{Question 1} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
68 |
Design a grammar for the WHILE language and give the grammar |
544 | 69 |
rules. The main categories of non-terminals should be: |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
70 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
71 |
\begin{itemize} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
72 |
\item arithmetic expressions (with the operations from the |
682 | 73 |
previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*}, |
74 |
\pcode{/} and \pcode{\%}) |
|
75 |
\item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>}, |
|
686 | 76 |
\code{>=}, \code{<=}, |
682 | 77 |
\code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false}) |
78 |
\item single statements (that is \pcode{skip}, assignments, \pcode{if}s, |
|
79 |
\pcode{while}-loops, \pcode{read} and \pcode{write}) |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
80 |
\item compound statements separated by semicolons |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
81 |
\item blocks which are enclosed in curly parentheses |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
82 |
\end{itemize} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
|
683 | 84 |
\noindent |
85 |
Make sure the grammar is not left-recursive. |
|
86 |
||
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
87 |
\subsection*{Question 2} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
88 |
|
684 | 89 |
You should implement a parser for the WHILE language using parser |
968 | 90 |
combinators. Be careful that the parser takes as input a list of |
91 |
\emph{tokens} generated by the tokenizer from the previous |
|
684 | 92 |
coursework. For this you might want to filter out whitespaces and |
93 |
comments. Your parser should be able to handle the WHILE programs in |
|
968 | 94 |
the \texttt{examples} directory. The output of the parser is an |
95 |
abstract syntax tree (AST). A (possibly incomplete) datatype for ASTs |
|
96 |
of the WHILE language is shown in Figure~\ref{trees}. |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
97 |
|
750 | 98 |
\begin{figure}[p] |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
99 |
\begin{lstlisting}[language=Scala] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
100 |
abstract class Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
101 |
abstract class AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
102 |
abstract class BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
103 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
104 |
type Block = List[Stmt] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
105 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
106 |
case object Skip extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
107 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
108 |
case class While(b: BExp, bl: Block) extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
109 |
case class Assign(s: String, a: AExp) extends Stmt |
750 | 110 |
case class Read(s: String) extends Stmt |
111 |
case class WriteVar(s: String) extends Stmt |
|
112 |
case class WriteStr(s: String) extends Stmt |
|
113 |
// for printing variables and strings |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
114 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
115 |
case class Var(s: String) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
116 |
case class Num(i: Int) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
117 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
118 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
119 |
case object True extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
120 |
case object False extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
121 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
682 | 122 |
case class Lop(o: String, b1: BExp, b2: BExp) extends BExp |
750 | 123 |
// logical operations: and, or |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
124 |
\end{lstlisting} |
901 | 125 |
\caption{The datatype for abstract syntax trees in Scala.\label{trees}} |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
126 |
\end{figure} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
127 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
128 |
\subsection*{Question 3} |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
129 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
130 |
Implement an interpreter for the WHILE language you designed |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
131 |
and parsed in Question 1 and 2. This interpreter should take |
901 | 132 |
as input an AST. However be careful because, programs |
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
133 |
contain variables and variable assignments. This means |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
134 |
you need to maintain a kind of memory, or environment, |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
135 |
where you can look up a value of a variable and also |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
136 |
store a new value if it is assigned. Therefore an |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
137 |
evaluation function (interpreter) needs to look roughly as |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
138 |
follows |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
139 |
|
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
140 |
\begin{lstlisting}[numbers=none] |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
141 |
eval_stmt(stmt, env) |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
142 |
\end{lstlisting} |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
143 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
144 |
\noindent |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
145 |
where \pcode{stmt} corresponds to the parse tree |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
146 |
of the program and \pcode{env} is an environment |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
147 |
acting as a store for variable values. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
148 |
Consider the Fibonacci program in Figure~\ref{fib}. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
149 |
At the beginning of the program this store will be |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
150 |
empty, but needs to be extended in line 3 and 4 where |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
151 |
the variables \pcode{minus1} and \pcode{minus2} |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
152 |
are assigned values. These values need to be reassigned in |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
153 |
lines 7 and 8. The program should be interpreted |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
154 |
according to straightforward rules: for example an |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
155 |
if-statement will ``run'' the if-branch if the boolean |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
156 |
evaluates to \pcode{true}, otherwise the else-branch. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
157 |
Loops should be run as long as the boolean is \pcode{true}. |
950 | 158 |
Note also that some programs contain a read-statement, |
159 |
which means you need to read and integer from the commandline |
|
160 |
and store the value in the corresponding variable. |
|
968 | 161 |
Programs you should be able to run are given in the |
162 |
\texttt{examples} directory. The output |
|
950 | 163 |
of the \texttt{primes.while} should look as follows: |
684 | 164 |
|
950 | 165 |
\begin{figure}[h] |
166 |
{\small |
|
167 |
\begin{lstlisting}[numbers=none] |
|
168 |
2 |
|
169 |
3 |
|
170 |
5 |
|
171 |
7 |
|
172 |
11 |
|
173 |
13 |
|
174 |
17 |
|
175 |
19 |
|
176 |
23 |
|
177 |
29 |
|
178 |
31 |
|
179 |
37 |
|
180 |
41 |
|
181 |
43 |
|
182 |
47 |
|
183 |
53 |
|
184 |
59 |
|
185 |
61 |
|
186 |
67 |
|
187 |
71 |
|
188 |
73 |
|
189 |
79 |
|
190 |
83 |
|
191 |
89 |
|
192 |
97 |
|
193 |
Map(end -> 100, n -> 100, f -> 4, tmp -> 1) |
|
194 |
\end{lstlisting}} |
|
195 |
\caption{Sample output for the file \texttt{primes.while}.\label{fib}} |
|
196 |
\end{figure} |
|
684 | 197 |
|
943 | 198 |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
\end{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
%%% Local Variables: |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
202 |
%%% mode: latex |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
203 |
%%% TeX-master: t |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
204 |
%%% End: |