author | Christian Urban <christian.urban@kcl.ac.uk> |
Thu, 19 Sep 2024 15:47:33 +0100 | |
changeset 961 | c0600f8b6427 |
parent 959 | 64ec1884d860 |
child 974 | 0cb4bf2469d1 |
permissions | -rw-r--r-- |
864 | 1 |
// CW3 |
2 |
||
961 | 3 |
//> using file project.scala |
4 |
//> using file lexer.sc |
|
5 |
import lexer.* |
|
864 | 6 |
|
919 | 7 |
case class ~[+A, +B](x: A, y: B) |
8 |
||
9 |
// parser combinators |
|
961 | 10 |
// |
11 |
type IsSeq[I] = I => Seq[?] |
|
919 | 12 |
|
961 | 13 |
abstract class Parser[I : IsSeq, T](using is: I => Seq[?]) { |
14 |
def parse(in: I): Set[(T, I)] |
|
919 | 15 |
|
16 |
def parse_all(in: I) : Set[T] = |
|
961 | 17 |
for ((hd, tl) <- parse(in); |
919 | 18 |
if is(tl).isEmpty) yield hd |
19 |
} |
|
864 | 20 |
|
961 | 21 |
// parser combinators |
22 |
||
919 | 23 |
// alternative parser |
961 | 24 |
class AltParser[I : IsSeq, T](p: => Parser[I, T], |
25 |
q: => Parser[I, T]) extends Parser[I, T] { |
|
26 |
def parse(in: I) = p.parse(in) ++ q.parse(in) |
|
919 | 27 |
} |
864 | 28 |
|
919 | 29 |
// sequence parser |
961 | 30 |
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], |
31 |
q: => Parser[I, S]) extends Parser[I, ~[T, S]] { |
|
32 |
def parse(in: I) = |
|
33 |
for ((hd1, tl1) <- p.parse(in); |
|
919 | 34 |
(hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2) |
864 | 35 |
} |
36 |
||
919 | 37 |
// map parser |
961 | 38 |
class MapParser[I : IsSeq, T, S](p: => Parser[I, T], |
39 |
f: T => S) extends Parser[I, S] { |
|
919 | 40 |
def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl) |
864 | 41 |
} |
42 |
||
919 | 43 |
|
961 | 44 |
|
919 | 45 |
/* |
46 |
// atomic parser for (particular) strings |
|
47 |
case class StrParser(s: String) extends Parser[String, String] { |
|
48 |
def parse(sb: String) = { |
|
49 |
val (prefix, suffix) = sb.splitAt(s.length) |
|
50 |
if (prefix == s) Set((prefix, suffix)) else Set() |
|
51 |
} |
|
864 | 52 |
} |
53 |
||
961 | 54 |
extension (sc: StringContext) |
919 | 55 |
def p(args: Any*) = StrParser(sc.s(args:_*)) |
56 |
*/ |
|
57 |
||
58 |
// more convenient syntax for parser combinators |
|
961 | 59 |
extension [I : IsSeq, T](p: Parser[I, T]) { |
60 |
def ||(q : => Parser[I, T]) = AltParser[I, T](p, q) |
|
61 |
def ~[S] (q : => Parser[I, S]) = SeqParser[I, T, S](p, q) |
|
62 |
def map[S](f: => T => S) = MapParser[I, T, S](p, f) |
|
864 | 63 |
} |
64 |
||
65 |
// New parser that takes as input a list of tokens |
|
919 | 66 |
case class TokenParser(t: Token) extends Parser[List[Token], Token] { |
67 |
def parse(in: List[Token]) = { |
|
68 |
// an example of an atomic parser for characters |
|
69 |
if (!in.isEmpty && in.head == t) Set((t, in.tail)) else Set() |
|
70 |
} |
|
961 | 71 |
} |
919 | 72 |
|
864 | 73 |
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] { |
74 |
def parse(tsb: List[Token]) = { |
|
75 |
val (prefix, suffix) = tsb.splitAt(ts.length) |
|
76 |
if (prefix == ts) Set((prefix, suffix)) else Set() |
|
77 |
} |
|
78 |
} |
|
79 |
||
961 | 80 |
// Implicit definitions to go from a token |
864 | 81 |
// or a list of tokens to a TokenListParser |
961 | 82 |
implicit def token2parser(t: Token) : Parser[List[Token], Token] = |
919 | 83 |
TokenParser(t) |
864 | 84 |
|
919 | 85 |
extension (t: Token) { |
961 | 86 |
def || (q : => Parser[List[Token], Token]) = AltParser[List[Token], Token](t, q) |
87 |
def ~[S](q : => Parser[List[Token], S]) = SeqParser[List[Token], Token, S](t, q) |
|
864 | 88 |
} |
89 |
||
90 |
||
91 |
// Abstract Syntax Trees |
|
92 |
abstract class Stmt |
|
93 |
abstract class AExp |
|
94 |
abstract class BExp |
|
95 |
||
96 |
type Block = List[Stmt] |
|
97 |
||
98 |
case object Skip extends Stmt |
|
99 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt |
|
100 |
case class While(b: BExp, bl: Block) extends Stmt |
|
101 |
case class Assign(s: String, a: AExp) extends Stmt |
|
102 |
case class Read(s: String) extends Stmt |
|
103 |
case class WriteId(s: String) extends Stmt // for printing values of variables |
|
104 |
case class WriteString(s: String) extends Stmt // for printing words |
|
105 |
||
106 |
case class Var(s: String) extends AExp |
|
107 |
case class Num(i: Int) extends AExp |
|
108 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp |
|
109 |
||
110 |
case object True extends BExp |
|
111 |
case object False extends BExp |
|
112 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
|
113 |
case class And(b1: BExp, b2: BExp) extends BExp |
|
114 |
case class Or(b1: BExp, b2: BExp) extends BExp |
|
115 |
||
116 |
case class IdParser() extends Parser[List[Token], String] { |
|
117 |
def parse(tsb: List[Token]) = tsb match { |
|
118 |
case T_ID(id) :: rest => Set((id, rest)) |
|
119 |
case _ => Set() |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
case class NumParser() extends Parser[List[Token], Int] { |
|
124 |
def parse(tsb: List[Token]) = tsb match { |
|
125 |
case T_NUM(n) :: rest => Set((n, rest)) |
|
126 |
case _ => Set() |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
case class StringParser() extends Parser[List[Token], String] { |
|
131 |
def parse(tsb: List[Token]) = tsb match { |
|
132 |
case T_STRING(s) :: rest => Set((s, rest)) |
|
133 |
case _ => Set() |
|
134 |
} |
|
135 |
} |
|
136 |
||
919 | 137 |
|
864 | 138 |
// WHILE Language Parsing |
961 | 139 |
lazy val AExp: Parser[List[Token], AExp] = |
919 | 140 |
(Te ~ T_OP("+") ~ AExp).map{ case x ~ _ ~ z => Aop("+", x, z): AExp } || |
141 |
(Te ~ T_OP("-") ~ AExp).map{ case x ~ _ ~ z => Aop("-", x, z): AExp } || Te |
|
961 | 142 |
lazy val Te: Parser[List[Token], AExp] = |
143 |
(Fa ~ T_OP("*") ~ Te).map{ case x ~ _ ~ z => Aop("*", x, z): AExp } || |
|
144 |
(Fa ~ T_OP("/") ~ Te).map{ case x ~ _ ~ z => Aop("/", x, z): AExp } || |
|
145 |
(Fa ~ T_OP("%") ~ Te).map{ case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa |
|
146 |
lazy val Fa: Parser[List[Token], AExp] = |
|
147 |
(T_PAREN("(") ~ AExp ~ T_PAREN(")")).map{ case _ ~ y ~ _ => y } || |
|
148 |
IdParser().map{Var(_)} || |
|
919 | 149 |
NumParser().map{Num(_)} |
864 | 150 |
|
961 | 151 |
lazy val BExp: Parser[List[Token], BExp] = |
152 |
(AExp ~ T_OP("==") ~ AExp).map{ case x ~ _ ~ z => Bop("==", x, z): BExp } || |
|
153 |
(AExp ~ T_OP("!=") ~ AExp).map{ case x ~ _ ~ z => Bop("!=", x, z): BExp } || |
|
154 |
(AExp ~ T_OP("<") ~ AExp).map{ case x ~ _ ~ z => Bop("<", x, z): BExp } || |
|
919 | 155 |
(AExp ~ T_OP(">") ~ AExp).map{ case x ~ _ ~ z => Bop(">", x, z): BExp } || |
156 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } || |
|
157 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } || |
|
961 | 158 |
(T_KEYWORD("true").map(_ => True: BExp )) || |
919 | 159 |
(T_KEYWORD("false").map(_ => False: BExp )) || |
160 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")")).map{ case _ ~ x ~ _ => x } |
|
864 | 161 |
|
162 |
lazy val Stmt: Parser[List[Token], Stmt] = |
|
919 | 163 |
T_KEYWORD("skip").map(_ => Skip: Stmt) || |
164 |
(IdParser() ~ T_OP(":=") ~ AExp).map{ case id ~ _ ~ z => Assign(id, z): Stmt } || |
|
165 |
(T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block).map{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } || |
|
166 |
(T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block).map{ case _ ~ y ~ _ ~ w => While(y, w) : Stmt } || |
|
167 |
(T_KEYWORD("read") ~ IdParser()).map{ case _ ~ id => Read(id): Stmt} || |
|
168 |
(T_KEYWORD("write") ~ IdParser()).map{ case _ ~ id => WriteId(id): Stmt} || |
|
961 | 169 |
(T_KEYWORD("write") ~ StringParser()).map{ case _ ~ s => WriteString(s): Stmt} || |
919 | 170 |
(T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} || |
171 |
(T_KEYWORD("write") ~ T_PAREN("(") ~ StringParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ s ~ _ => WriteString(s): Stmt} |
|
864 | 172 |
|
173 |
lazy val Stmts: Parser[List[Token], Block] = |
|
919 | 174 |
(Stmt ~ T_SEMI ~ Stmts).map{ case x ~ _ ~ z => x :: z : Block } || |
175 |
(Stmt.map(s => List(s) : Block)) |
|
864 | 176 |
|
177 |
lazy val Block: Parser[List[Token], Block] = |
|
919 | 178 |
(T_PAREN("{") ~ Stmts ~ T_PAREN("}")).map{ case x ~ y ~ z => y} || |
179 |
(Stmt.map(s => List(s))) |
|
864 | 180 |
|
181 |
// Testing with programs 2 & 3 |
|
182 |
||
905 | 183 |
//println("Fibonacci") |
184 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "fib.while")))) |
|
864 | 185 |
|
905 | 186 |
//println("Loops") |
187 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "loops.while")))) |
|
864 | 188 |
|
905 | 189 |
//println("Collatz") |
190 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while")))) |
|
864 | 191 |
|
192 |
||
193 |
// Interpreter |
|
194 |
||
195 |
// Environment to store values of variables |
|
196 |
type Env = Map[String, Int] |
|
197 |
||
198 |
def eval_aexp(a: AExp, env: Env) : Int = a match { |
|
199 |
case Num(i) => i |
|
200 |
case Var(s) => env(s) |
|
201 |
case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env) |
|
202 |
case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env) |
|
203 |
case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env) |
|
204 |
case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env) |
|
205 |
case Aop("%", a1, a2) => eval_aexp(a1, env) % eval_aexp(a2, env) |
|
206 |
} |
|
207 |
||
208 |
def eval_bexp(b: BExp, env: Env) : Boolean = b match { |
|
209 |
case True => true |
|
210 |
case False => false |
|
211 |
case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env) |
|
212 |
case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env)) |
|
213 |
case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env) |
|
214 |
case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env) |
|
215 |
case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env) |
|
216 |
case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env) |
|
217 |
} |
|
218 |
||
219 |
// Import needed to take int as input from the user |
|
220 |
import scala.io.StdIn.readInt |
|
221 |
||
222 |
def eval_stmt(s: Stmt, env: Env) : Env = s match { |
|
223 |
case Skip => env |
|
224 |
case Assign(x, a) => env + (x -> eval_aexp(a, env)) |
|
961 | 225 |
case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) |
226 |
case While(b, bl) => |
|
864 | 227 |
if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env)) |
228 |
else env |
|
229 |
||
230 |
case WriteId(x) => { print(env(x)) ; env } |
|
231 |
case WriteString(x) => { |
|
232 |
print(x.replaceAll("\"", "").replaceAll("""\\n""", "\n")) ; |
|
233 |
env |
|
234 |
} |
|
235 |
||
961 | 236 |
case Read(x) => { |
237 |
println("Enter an integer and press ENTER:") ; |
|
864 | 238 |
val n = readInt() ; // Note: Does not work when using the REPL |
961 | 239 |
eval_stmt(Assign(x, Num(n)), env) |
864 | 240 |
} |
241 |
} |
|
242 |
||
243 |
def eval_bl(bl: Block, env: Env) : Env = bl match { |
|
244 |
case Nil => env |
|
245 |
case s::bl => eval_bl(bl, eval_stmt(s, env)) |
|
246 |
} |
|
247 |
||
248 |
def eval(bl: Block) : Env = eval_bl(bl, Map()) |
|
249 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
250 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
251 |
//println(tokenise(os.read(os.pwd / "primes.while"))) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
252 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
253 |
//println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head)) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
254 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
255 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
256 |
|
961 | 257 |
//@main |
921 | 258 |
def run(file: String) = { |
905 | 259 |
val contents = os.read(os.pwd / file) |
260 |
println(s"Lex $file: ") |
|
261 |
println(tokenise(contents)) |
|
262 |
println(s"Parse $file: ") |
|
263 |
println(Stmts.parse_all(tokenise(contents)).head) |
|
264 |
println(s"Eval $file: ") |
|
265 |
println(eval(Stmts.parse_all(tokenise(contents)).head)) |
|
266 |
} |
|
864 | 267 |
|
961 | 268 |
//@main |
921 | 269 |
def test(file: String) = { |
270 |
val contents = os.read(os.pwd / file) |
|
271 |
println(s"Lex $file: ") |
|
272 |
println(tokenise(contents)) |
|
273 |
println(s"Parse $file: ") |
|
274 |
println(Stmts.parse_all(tokenise(contents)).head) |
|
275 |
} |
|
276 |
||
864 | 277 |
/* |
278 |
println("Loops eval") |
|
279 |
val start = System.nanoTime() |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
280 |
println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head)) |
864 | 281 |
val end = System.nanoTime() |
282 |
println("Time taken in seconds: ") |
|
283 |
println((end - start)/(1.0e9)) |
|
284 |
*/ |
|
961 | 285 |
|
286 |
test("primes.while") |
|
287 |
run("primes.while") |