|
1 // CW3 |
|
2 |
|
3 import $file.lexer |
|
4 import lexer._ |
|
5 |
|
6 |
|
7 case class ~[+A, +B](_1: A, _2: B) |
|
8 type IsSeq[A] = A => Seq[_] |
|
9 |
|
10 abstract class Parser[I : IsSeq, T] { |
|
11 def parse(ts: I): Set[(T, I)] |
|
12 |
|
13 def parse_all(ts: I) : Set[T] = |
|
14 for ((head, tail) <- parse(ts); if tail.isEmpty) yield head |
|
15 } |
|
16 |
|
17 class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] { |
|
18 def parse(sb: I) = |
|
19 for ((head1, tail1) <- p.parse(sb); |
|
20 (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2) |
|
21 } |
|
22 |
|
23 class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] { |
|
24 def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
|
25 } |
|
26 |
|
27 class FunParser[I : IsSeq, T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] { |
|
28 def parse(sb: I) = |
|
29 for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
|
30 } |
|
31 |
|
32 // New parser that takes as input a list of tokens |
|
33 case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] { |
|
34 def parse(tsb: List[Token]) = { |
|
35 val (prefix, suffix) = tsb.splitAt(ts.length) |
|
36 if (prefix == ts) Set((prefix, suffix)) else Set() |
|
37 } |
|
38 } |
|
39 |
|
40 // Implicit definitions to go from a token |
|
41 // or a list of tokens to a TokenListParser |
|
42 implicit def token2parser(t: Token) = TokenListParser(List(t)) |
|
43 implicit def tokenList2parser(ts: List[Token]) = TokenListParser(ts) |
|
44 |
|
45 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new { |
|
46 def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
|
47 def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
|
48 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
|
49 } |
|
50 |
|
51 implicit def TokenOps(t: Token) = new { |
|
52 def || (q : => Parser[List[Token], List[Token]]) = new AltParser[List[Token], List[Token]](List(t), q) |
|
53 def || (qs : List[Token]) = new AltParser[List[Token], List[Token]](List(t), qs) |
|
54 def ==>[S] (f: => List[Token] => S) = new FunParser[List[Token], List[Token], S](List(t), f) |
|
55 def ~[S](q : => Parser[List[Token], S]) = |
|
56 new SeqParser[List[Token], List[Token], S](List(t), q) |
|
57 def ~ (qs : List[Token]) = |
|
58 new SeqParser[List[Token], List[Token], List[Token]](List(t), qs) |
|
59 } |
|
60 |
|
61 implicit def TokenListOps(ts: List[Token]) = new { |
|
62 def || (q : => Parser[List[Token], List[Token]]) = new AltParser[List[Token], List[Token]](ts, q) |
|
63 def || (qs : List[Token]) = new AltParser[List[Token], List[Token]](ts, qs) |
|
64 def ==>[S] (f: => List[Token] => S) = new FunParser[List[Token], List[Token], S](ts, f) |
|
65 def ~[S](q : => Parser[List[Token], S]) = |
|
66 new SeqParser[List[Token], List[Token], S](ts, q) |
|
67 def ~ (qs : List[Token]) = |
|
68 new SeqParser[List[Token], List[Token], List[Token]](ts, qs) |
|
69 } |
|
70 |
|
71 // Abstract Syntax Trees |
|
72 abstract class Stmt |
|
73 abstract class AExp |
|
74 abstract class BExp |
|
75 |
|
76 type Block = List[Stmt] |
|
77 |
|
78 case object Skip extends Stmt |
|
79 case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt |
|
80 case class While(b: BExp, bl: Block) extends Stmt |
|
81 case class Assign(s: String, a: AExp) extends Stmt |
|
82 case class Read(s: String) extends Stmt |
|
83 case class WriteId(s: String) extends Stmt // for printing values of variables |
|
84 case class WriteString(s: String) extends Stmt // for printing words |
|
85 case class For(counter: String, lower: AExp, upper: AExp, code: Block) extends Stmt |
|
86 |
|
87 |
|
88 case class Var(s: String) extends AExp |
|
89 case class Num(i: Int) extends AExp |
|
90 case class Aop(o: String, a1: AExp, a2: AExp) extends AExp |
|
91 |
|
92 case object True extends BExp |
|
93 case object False extends BExp |
|
94 case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
|
95 case class And(b1: BExp, b2: BExp) extends BExp |
|
96 case class Or(b1: BExp, b2: BExp) extends BExp |
|
97 |
|
98 case class IdParser() extends Parser[List[Token], String] { |
|
99 def parse(tsb: List[Token]) = tsb match { |
|
100 case T_ID(id) :: rest => Set((id, rest)) |
|
101 case _ => Set() |
|
102 } |
|
103 } |
|
104 |
|
105 case class NumParser() extends Parser[List[Token], Int] { |
|
106 def parse(tsb: List[Token]) = tsb match { |
|
107 case T_NUM(n) :: rest => Set((n, rest)) |
|
108 case _ => Set() |
|
109 } |
|
110 } |
|
111 |
|
112 case class StringParser() extends Parser[List[Token], String] { |
|
113 def parse(tsb: List[Token]) = tsb match { |
|
114 case T_STRING(s) :: rest => Set((s, rest)) |
|
115 case _ => Set() |
|
116 } |
|
117 } |
|
118 |
|
119 // WHILE Language Parsing |
|
120 lazy val AExp: Parser[List[Token], AExp] = |
|
121 (Te ~ T_OP("+") ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z): AExp } || |
|
122 (Te ~ T_OP("-") ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z): AExp } || Te |
|
123 lazy val Te: Parser[List[Token], AExp] = |
|
124 (Fa ~ T_OP("*") ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z): AExp } || |
|
125 (Fa ~ T_OP("/") ~ Te) ==> { case x ~ _ ~ z => Aop("/", x, z): AExp } || |
|
126 (Fa ~ T_OP("%") ~ Te) ==> { case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa |
|
127 lazy val Fa: Parser[List[Token], AExp] = |
|
128 (T_PAREN("(") ~ AExp ~ T_PAREN(")")) ==> { case _ ~ y ~ _ => y } || |
|
129 IdParser() ==> Var || |
|
130 NumParser() ==> Num |
|
131 |
|
132 lazy val BExp: Parser[List[Token], BExp] = |
|
133 (AExp ~ T_OP("==") ~ AExp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || |
|
134 (AExp ~ T_OP("!=") ~ AExp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || |
|
135 (AExp ~ T_OP("<") ~ AExp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || |
|
136 (AExp ~ T_OP(">") ~ AExp) ==> { case x ~ _ ~ z => Bop(">", x, z): BExp } || |
|
137 (T_PAREN("(") ~ BExp ~ List(T_PAREN(")"), T_OP("&&")) ~ BExp) ==> { case _ ~ y ~ _ ~ v => And(y, v): BExp } || |
|
138 (T_PAREN("(") ~ BExp ~ List(T_PAREN(")"), T_OP("||")) ~ BExp) ==> { case _ ~ y ~ _ ~ v => Or(y, v): BExp } || |
|
139 (T_KEYWORD("true") ==> (_ => True: BExp )) || |
|
140 (T_KEYWORD("false") ==> (_ => False: BExp )) || |
|
141 (T_PAREN("(") ~ BExp ~ T_PAREN(")")) ==> { case _ ~ x ~ _ => x } |
|
142 |
|
143 lazy val Stmt: Parser[List[Token], Stmt] = |
|
144 T_KEYWORD("skip") ==> (_ => Skip: Stmt) || |
|
145 (IdParser() ~ T_OP(":=") ~ AExp) ==> { case id ~ _ ~ z => Assign(id, z): Stmt } || |
|
146 (T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block) ==> { case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } || |
|
147 (T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) : Stmt } || |
|
148 (T_KEYWORD("read") ~ IdParser()) ==> { case _ ~ id => Read(id): Stmt} || |
|
149 (T_KEYWORD("write") ~ IdParser()) ==> { case _ ~ id => WriteId(id): Stmt} || |
|
150 (T_KEYWORD("write") ~ StringParser()) ==> { case _ ~ s => WriteString(s): Stmt} || |
|
151 (T_KEYWORD("for") ~ IdParser() ~ T_OP(":=") ~ AExp ~ T_KEYWORD("upto") ~ AExp ~ T_KEYWORD("do") ~ Block) ==> { |
|
152 case _ ~ id ~ _ ~ lower ~ _ ~ upper ~ _ ~ blck => For(id, lower, upper, blck): Stmt |
|
153 } |
|
154 |
|
155 lazy val Stmts: Parser[List[Token], Block] = |
|
156 (Stmt ~ T_SEMI ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } || |
|
157 (Stmt ==> (s => List(s) : Block)) |
|
158 |
|
159 lazy val Block: Parser[List[Token], Block] = |
|
160 (T_PAREN("{") ~ Stmts ~ T_PAREN("}")) ==> { case x ~ y ~ z => y} || |
|
161 (Stmt ==> (s => List(s))) |
|
162 |