author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sat, 13 Sep 2014 04:30:25 +0100 | |
changeset 242 | 35104ee14f87 |
parent 165 | 66b699c80479 |
child 367 | 04127a5aad23 |
permissions | -rw-r--r-- |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.language.implicitConversions |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
import scala.language.reflectiveCalls |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
import scala.util._ |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
import scala.annotation.tailrec |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
sealed abstract class Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case object NULL extends Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case object EMPTY extends Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case class CHAR(c: Char) extends Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
case class STAR(r: Rexp) extends Rexp |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case Nil => EMPTY |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
case c::Nil => CHAR(c) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
implicit def RexpOps(r: Rexp) = new { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
def | (s: Rexp) = ALT(r, s) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
def % = STAR(r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
def ~ (s: Rexp) = SEQ(r, s) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
implicit def stringOps(s: String) = new { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
def | (r: Rexp) = ALT(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
def | (r: String) = ALT(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
def % = STAR(s) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
def ~ (r: Rexp) = SEQ(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
def ~ (r: String) = SEQ(s, r) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
def Range(s : List[Char]) : Rexp = s match { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
case Nil => NULL |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
case c::Nil => CHAR(c) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
case c::s => ALT(CHAR(c), Range(s)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
def RANGE(s: String) = Range(s.toList) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
val DIGIT = RANGE("0123456789") |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
val ID = SYM ~ (SYM | DIGIT).% |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
val NUM = PLUS(DIGIT) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
val SEMI: Rexp = ";" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
val WHITESPACE = PLUS(RANGE(" \n")) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
val RPAREN: Rexp = ")" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
val LPAREN: Rexp = "(" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
val BEGIN: Rexp = "{" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
val END: Rexp = "}" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
abstract class Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
case object T_WHITESPACE extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
case object T_SEMI extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
case object T_LPAREN extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
case object T_RPAREN extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
case object T_BEGIN extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
case object T_END extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
case class T_ID(s: String) extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
case class T_OP(s: String) extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
case class T_NUM(s: String) extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
case class T_KWD(s: String) extends Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
case class T_ERR(s: String) extends Token // special error token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
type TokenFun = String => Token |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
type LexRules = List[(Rexp, TokenFun)] |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
val lexing_rules: LexRules = |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
List((KEYWORD, (s) => T_KWD(s)), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
(ID, (s) => T_ID(s)), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
(OP, (s) => T_OP(s)), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
(NUM, (s) => T_NUM(s)), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
(SEMI, (s) => T_SEMI), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
(LPAREN, (s) => T_LPAREN), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
(RPAREN, (s) => T_RPAREN), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
(BEGIN, (s) => T_BEGIN), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
(END, (s) => T_END), |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
(WHITESPACE, (s) => T_WHITESPACE)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
def nullable (r: Rexp) : Boolean = r match { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
case NULL => false |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
case EMPTY => true |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
case CHAR(_) => false |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
case STAR(_) => true |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
def zeroable (r: Rexp) : Boolean = r match { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
case NULL => true |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
case EMPTY => false |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
case CHAR(_) => false |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
case ALT(r1, r2) => zeroable(r1) && zeroable(r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
case SEQ(r1, r2) => zeroable(r1) || zeroable(r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
case STAR(_) => false |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
def der (c: Char, r: Rexp) : Rexp = r match { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
case NULL => NULL |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
case EMPTY => NULL |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
case CHAR(d) => if (c == d) EMPTY else NULL |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
case SEQ(r1, r2) => |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
else SEQ(der(c, r1), r2) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
|
165
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
116 |
|
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
// calculates derivatives until all of them are zeroable |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
@tailrec |
165
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
120 |
def munch(s: List[Char], |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
121 |
pos: Int, |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
rs: LexRules, |
165
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
123 |
last: Option[(Int, TokenFun)]): Option[(Int, TokenFun)] = rs match { |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
124 |
case Nil => last |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
125 |
case rs if (s.length <= pos) => last |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
126 |
case rs => { |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
127 |
val ders = rs.map({case (r, tf) => (der(s(pos), r), tf)}) |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
128 |
val rs_nzero = ders.filterNot({case (r, _) => zeroable(r)}) |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
129 |
val rs_nulls = ders.filter({case (r, _) => nullable(r)}) |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
130 |
val new_last = if (rs_nulls != Nil) Some((pos, rs_nulls.head._2)) else last |
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
131 |
munch(s, 1 + pos, rs_nzero, new_last) |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
// iterates the munching function and returns a Token list |
165
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
136 |
def tokenize(s: String, rs: LexRules) : List[Token] = munch(s.toList, 0, rs, None) match { |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
case None if (s == "") => Nil |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
case None => List(T_ERR("Lexing error: $s")) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
case Some((n, tf)) => { |
165
66b699c80479
tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
164
diff
changeset
|
140 |
val (head, tail) = s.splitAt(n + 1) |
164
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
tf(head)::tokenize(tail, rs) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
} |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
144 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
145 |
val test_prog = """ |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
start := XXX; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
x := start; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
y := start; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
149 |
z := start; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
while 0 < x do { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
while 0 < y do { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
while 0 < z do { |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
z := z - 1 |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
}; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
z := start; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
y := y - 1 |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
}; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
y := start; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
x := x - 1 |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
}; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
write x; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
write y; |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
write z |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
""" |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
|
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
println(tokenize(test_prog, lexing_rules).mkString("\n")) |
6c1d214c39ef
added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |