864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// CW3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
import $file.lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
import lexer._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
920
|
6 |
case class ~[+A, +B](x: A, y: B)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
|
920
|
8 |
// parser combinators
|
|
9 |
|
|
10 |
abstract class Parser[I, T](using is: I => Seq[_]) {
|
|
11 |
def parse(in: I): Set[(T, I)]
|
|
12 |
|
|
13 |
def parse_all(in: I) : Set[T] =
|
|
14 |
for ((hd, tl) <- parse(in);
|
|
15 |
if is(tl).isEmpty) yield hd
|
|
16 |
}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
|
920
|
18 |
// alternative parser
|
|
19 |
class AltParser[I, T](p: => Parser[I, T],
|
|
20 |
q: => Parser[I, T])(using I => Seq[_]) extends Parser[I, T] {
|
|
21 |
def parse(in: I) = p.parse(in) ++ q.parse(in)
|
|
22 |
}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
920
|
24 |
// sequence parser
|
|
25 |
class SeqParser[I, T, S](p: => Parser[I, T],
|
|
26 |
q: => Parser[I, S])(using I => Seq[_]) extends Parser[I, ~[T, S]] {
|
|
27 |
def parse(in: I) =
|
|
28 |
for ((hd1, tl1) <- p.parse(in);
|
|
29 |
(hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
920
|
32 |
// map parser
|
|
33 |
class MapParser[I, T, S](p: => Parser[I, T],
|
|
34 |
f: T => S)(using I => Seq[_]) extends Parser[I, S] {
|
|
35 |
def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
|
|
36 |
}
|
|
37 |
|
|
38 |
// more convenient syntax for parser combinators
|
|
39 |
extension [I, T](p: Parser[I, T])(using I => Seq[_]) {
|
|
40 |
def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
|
|
41 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
|
|
42 |
def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
|
920
|
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
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
|
920
|
54 |
extension (sc: StringContext)
|
|
55 |
def p(args: Any*) = StrParser(sc.s(args:_*))
|
|
56 |
*/
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
|
920
|
58 |
case class TokenParser(t: Token) extends Parser[List[Token], Token] {
|
|
59 |
def parse(in: List[Token]) = {
|
|
60 |
// an example of an atomic parser for characters
|
|
61 |
if (!in.isEmpty && in.head == t) Set((t, in.tail)) else Set()
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
def parse(tsb: List[Token]) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
val (prefix, suffix) = tsb.splitAt(ts.length)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
if (prefix == ts) Set((prefix, suffix)) else Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
// Implicit definitions to go from a token
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
// or a list of tokens to a TokenListParser
|
920
|
74 |
implicit def token2parser(t: Token) : Parser[List[Token], Token] =
|
|
75 |
TokenParser(t)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
|
920
|
77 |
extension (t: Token) {
|
|
78 |
def || (q : => Parser[List[Token], Token]) =
|
|
79 |
new AltParser[List[Token], Token](t, q)
|
|
80 |
def ~[S](q : => Parser[List[Token], S]) =
|
|
81 |
new SeqParser[List[Token], Token, S](t, q)
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
// Abstract Syntax Trees
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
abstract class Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
abstract class AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
abstract class BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
type Block = List[Stmt]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
case object Skip extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case class While(b: BExp, bl: Block) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
case class Assign(s: String, a: AExp) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
case class Read(s: String) extends Stmt
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
case class WriteId(s: String) extends Stmt // for printing values of variables
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
case class WriteString(s: String) extends Stmt // for printing words
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
case class For(counter: String, lower: AExp, upper: AExp, code: Block) extends Stmt
|
920
|
101 |
case object Break extends Stmt
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
case class Var(s: String) extends AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
case class Num(i: Int) extends AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
case object True extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
case object False extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
case class And(b1: BExp, b2: BExp) extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
case class Or(b1: BExp, b2: BExp) extends BExp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
case class IdParser() extends Parser[List[Token], String] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
def parse(tsb: List[Token]) = tsb match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
case T_ID(id) :: rest => Set((id, rest))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
case _ => Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
case class NumParser() extends Parser[List[Token], Int] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
def parse(tsb: List[Token]) = tsb match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case T_NUM(n) :: rest => Set((n, rest))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
case _ => Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
case class StringParser() extends Parser[List[Token], String] {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
def parse(tsb: List[Token]) = tsb match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
case T_STRING(s) :: rest => Set((s, rest))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
case _ => Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
// WHILE Language Parsing
|
920
|
136 |
|
|
137 |
// WHILE Language Parsing
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
lazy val AExp: Parser[List[Token], AExp] =
|
920
|
139 |
(Te ~ T_OP("+") ~ AExp).map{ case x ~ _ ~ z => Aop("+", x, z): AExp } ||
|
|
140 |
(Te ~ T_OP("-") ~ AExp).map{ case x ~ _ ~ z => Aop("-", x, z): AExp } || Te
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
lazy val Te: Parser[List[Token], AExp] =
|
920
|
142 |
(Fa ~ T_OP("*") ~ Te).map{ case x ~ _ ~ z => Aop("*", x, z): 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 } || Fa
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
lazy val Fa: Parser[List[Token], AExp] =
|
920
|
146 |
(T_PAREN("(") ~ AExp ~ T_PAREN(")")).map{ case _ ~ y ~ _ => y } ||
|
|
147 |
IdParser().map{Var(_)} ||
|
|
148 |
NumParser().map{Num(_)}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
lazy val BExp: Parser[List[Token], BExp] =
|
920
|
151 |
(AExp ~ T_OP("==") ~ AExp).map{ case x ~ _ ~ z => Bop("==", x, z): 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 } ||
|
|
155 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
|
|
156 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
|
|
157 |
(T_KEYWORD("true").map(_ => True: BExp )) ||
|
|
158 |
(T_KEYWORD("false").map(_ => False: BExp )) ||
|
|
159 |
(T_PAREN("(") ~ BExp ~ T_PAREN(")")).map{ case _ ~ x ~ _ => x }
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
lazy val Stmt: Parser[List[Token], Stmt] =
|
920
|
162 |
T_KEYWORD("skip").map(_ => Skip: Stmt) ||
|
|
163 |
T_KEYWORD("break").map(_ => Break: 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("for") ~ IdParser() ~ T_OP(":=") ~ AExp ~T_KEYWORD("upto") ~ AExp ~ T_KEYWORD("do") ~ Block).map{
|
|
168 |
case _ ~ id ~ _ ~ low ~ _ ~ high ~ _ ~ bl => For(id, low, high, bl) : Stmt } ||
|
|
169 |
(T_KEYWORD("read") ~ IdParser()).map{ case _ ~ id => Read(id): Stmt} ||
|
|
170 |
(T_KEYWORD("write") ~ IdParser()).map{ case _ ~ id => WriteId(id): Stmt} ||
|
|
171 |
(T_KEYWORD("write") ~ StringParser()).map{ case _ ~ s => WriteString(s): Stmt} ||
|
|
172 |
(T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} ||
|
|
173 |
(T_KEYWORD("write") ~ T_PAREN("(") ~ StringParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ s ~ _ => WriteString(s): Stmt}
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
lazy val Stmts: Parser[List[Token], Block] =
|
920
|
176 |
(Stmt ~ T_SEMI ~ Stmts).map{ case x ~ _ ~ z => x :: z : Block } ||
|
|
177 |
(Stmt.map(s => List(s) : Block))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
lazy val Block: Parser[List[Token], Block] =
|
920
|
180 |
(T_PAREN("{") ~ Stmts ~ T_PAREN("}")).map{ case x ~ y ~ z => y} ||
|
|
181 |
(Stmt.map(s => List(s)))
|
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
|
920
|
183 |
|