| author | Christian Urban <christian.urban@kcl.ac.uk> |
| Mon, 20 Oct 2025 22:18:21 +0200 | |
| changeset 1013 | 1a23d87d1700 |
| parent 961 | c0600f8b6427 |
| permissions | -rw-r--r-- |
| 920 | 1 |
// Lexer from CW2 |
2 |
//================ |
|
3 |
||
| 961 | 4 |
//> using toolkit 0.4.0 |
5 |
//> using file project.sc |
|
6 |
import project.* |
|
| 864 | 7 |
|
8 |
// Rexp |
|
9 |
abstract class Rexp |
|
10 |
case object ZERO extends Rexp |
|
11 |
case object ONE extends Rexp |
|
12 |
case class CHAR(c: Char) extends Rexp |
|
| 961 | 13 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
14 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
15 |
case class STAR(r: Rexp) extends Rexp |
|
| 864 | 16 |
case class RECD(x: String, r: Rexp) extends Rexp |
17 |
||
18 |
case class RANGE(s: Set[Char]) extends Rexp |
|
19 |
case class PLUS(r: Rexp) extends Rexp |
|
20 |
case class OPTIONAL(r: Rexp) extends Rexp |
|
| 961 | 21 |
case class NTIMES(r: Rexp, n: Int) extends Rexp |
| 864 | 22 |
|
23 |
// Values |
|
24 |
abstract class Val |
|
25 |
case object Empty extends Val |
|
26 |
case class Chr(c: Char) extends Val |
|
27 |
case class Sequ(v1: Val, v2: Val) extends Val |
|
28 |
case class Left(v: Val) extends Val |
|
29 |
case class Right(v: Val) extends Val |
|
30 |
case class Stars(vs: List[Val]) extends Val |
|
31 |
case class Rec(x: String, v: Val) extends Val |
|
32 |
||
33 |
||
| 920 | 34 |
// Convenience for typing |
| 864 | 35 |
def charlist2rexp(s : List[Char]): Rexp = s match {
|
36 |
case Nil => ONE |
|
37 |
case c::Nil => CHAR(c) |
|
38 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
39 |
} |
|
40 |
||
| 961 | 41 |
implicit def string2rexp(s : String) : Rexp = |
| 864 | 42 |
charlist2rexp(s.toList) |
43 |
||
| 919 | 44 |
extension (r: Rexp) {
|
| 864 | 45 |
def | (s: Rexp) = ALT(r, s) |
46 |
def % = STAR(r) |
|
47 |
def ~ (s: Rexp) = SEQ(r, s) |
|
48 |
} |
|
49 |
||
| 919 | 50 |
extension (s: String) {
|
| 864 | 51 |
def | (r: Rexp) = ALT(s, r) |
52 |
def | (r: String) = ALT(s, r) |
|
53 |
def % = STAR(s) |
|
54 |
def ~ (r: Rexp) = SEQ(s, r) |
|
55 |
def ~ (r: String) = SEQ(s, r) |
|
| 961 | 56 |
infix def $ (r: Rexp) = RECD(s, r) |
| 864 | 57 |
} |
58 |
||
59 |
// nullable |
|
60 |
def nullable(r: Rexp) : Boolean = r match {
|
|
61 |
case ZERO => false |
|
62 |
case ONE => true |
|
63 |
case CHAR(_) => false |
|
64 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
65 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
66 |
case STAR(_) => true |
|
67 |
||
68 |
case RECD(_, r1) => nullable(r1) |
|
69 |
case RANGE(_) => false |
|
70 |
case PLUS(r1) => nullable(r1) |
|
71 |
case OPTIONAL(_) => true |
|
72 |
case NTIMES(r1, i) => if (i == 0) true else nullable(r1) |
|
73 |
} |
|
74 |
||
75 |
// der |
|
76 |
def der(c: Char, r: Rexp) : Rexp = r match {
|
|
77 |
case ZERO => ZERO |
|
78 |
case ONE => ZERO |
|
79 |
case CHAR(d) => if (c == d) ONE else ZERO |
|
80 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
| 961 | 81 |
case SEQ(r1, r2) => |
| 864 | 82 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
83 |
else SEQ(der(c, r1), r2) |
|
84 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
85 |
||
86 |
case RECD(_, r1) => der(c, r1) |
|
| 961 | 87 |
case RANGE(s) => if (s.contains(c)) ONE else ZERO |
| 864 | 88 |
case PLUS(r1) => SEQ(der(c, r1), STAR(r1)) |
| 919 | 89 |
case OPTIONAL(r1) => der(c, r1) |
| 961 | 90 |
case NTIMES(r, i) => |
| 864 | 91 |
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1)) |
92 |
} |
|
93 |
||
94 |
// Flatten |
|
95 |
def flatten(v: Val) : String = v match {
|
|
96 |
case Empty => "" |
|
97 |
case Chr(c) => c.toString |
|
98 |
case Left(v) => flatten(v) |
|
99 |
case Right(v) => flatten(v) |
|
100 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
101 |
case Stars(vs) => vs.map(flatten).mkString |
|
102 |
case Rec(_, v) => flatten(v) |
|
103 |
} |
|
104 |
||
105 |
// Env |
|
106 |
def env(v: Val) : List[(String, String)] = v match {
|
|
107 |
case Empty => Nil |
|
108 |
case Chr(c) => Nil |
|
109 |
case Left(v) => env(v) |
|
110 |
case Right(v) => env(v) |
|
111 |
case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
112 |
case Stars(vs) => vs.flatMap(env) |
|
113 |
case Rec(x, v) => (x, flatten(v))::env(v) |
|
114 |
} |
|
115 |
||
116 |
// Mkeps |
|
117 |
def mkeps(r: Rexp) : Val = r match {
|
|
118 |
case ONE => Empty |
|
| 961 | 119 |
case ALT(r1, r2) => |
| 864 | 120 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
121 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
122 |
case STAR(r) => Stars(Nil) |
|
123 |
case RECD(x, r) => Rec(x, mkeps(r)) |
|
124 |
||
125 |
case PLUS(r) => Stars(List(mkeps(r))) // the first copy must match the empty string |
|
| 919 | 126 |
case OPTIONAL(r) => if (nullable(r)) Stars(List(mkeps(r))) else Stars(Nil) |
| 864 | 127 |
case NTIMES(r, i) => Stars(List.fill(i)(mkeps(r))) |
128 |
} |
|
129 |
||
130 |
// Inj |
|
131 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
|
|
132 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
133 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
134 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
135 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
136 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
137 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
| 961 | 138 |
case (CHAR(d), Empty) => Chr(c) |
| 864 | 139 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
140 |
||
141 |
case (RANGE(_), Empty) => Chr(c) |
|
142 |
case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
| 920 | 143 |
case (OPTIONAL(r), v1) => Stars(List(inj(r, c, v1))) |
| 864 | 144 |
case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
145 |
} |
|
146 |
||
147 |
// Rectification functions |
|
148 |
def F_ID(v: Val): Val = v |
|
149 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
150 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
151 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
|
|
152 |
case Right(v) => Right(f2(v)) |
|
153 |
case Left(v) => Left(f1(v)) |
|
154 |
} |
|
155 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
|
|
156 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
157 |
} |
|
| 961 | 158 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
| 864 | 159 |
(v:Val) => Sequ(f1(Empty), f2(v)) |
| 961 | 160 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
| 864 | 161 |
(v:Val) => Sequ(f1(v), f2(Empty)) |
162 |
def F_RECD(f: Val => Val) = (v:Val) => v match {
|
|
163 |
case Rec(x, v) => Rec(x, f(v)) |
|
164 |
} |
|
165 |
def F_ERROR(v: Val): Val = throw new Exception("error")
|
|
166 |
||
167 |
// Simp |
|
168 |
def simp(r: Rexp): (Rexp, Val => Val) = r match {
|
|
169 |
case ALT(r1, r2) => {
|
|
170 |
val (r1s, f1s) = simp(r1) |
|
171 |
val (r2s, f2s) = simp(r2) |
|
172 |
(r1s, r2s) match {
|
|
173 |
case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
174 |
case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
175 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
| 961 | 176 |
else (ALT (r1s, r2s), F_ALT(f1s, f2s)) |
| 864 | 177 |
} |
178 |
} |
|
179 |
case SEQ(r1, r2) => {
|
|
180 |
val (r1s, f1s) = simp(r1) |
|
181 |
val (r2s, f2s) = simp(r2) |
|
182 |
(r1s, r2s) match {
|
|
183 |
case (ZERO, _) => (ZERO, F_ERROR) |
|
184 |
case (_, ZERO) => (ZERO, F_ERROR) |
|
185 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
186 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
187 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
188 |
} |
|
189 |
} |
|
190 |
case r => (r, F_ID) |
|
191 |
} |
|
192 |
||
193 |
// Lex |
|
194 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
|
|
| 961 | 195 |
case Nil => if (nullable(r)) mkeps(r) else |
196 |
{ throw new Exception("lexing error") }
|
|
| 864 | 197 |
case c::cs => {
|
198 |
val (r_simp, f_simp) = simp(der(c, r)) |
|
199 |
inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList)) |
|
204 |
||
205 |
// Language specific code |
|
| 961 | 206 |
val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip" |
| 864 | 207 |
val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||" |
| 919 | 208 |
val LET: Rexp = RANGE(('A' to 'Z').toSet ++ ('a' to 'z').toSet)
|
209 |
val SYM : Rexp = RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':', ')', '('))
|
|
| 864 | 210 |
val PARENS : Rexp = "(" | "{" | ")" | "}"
|
211 |
val SEMI : Rexp = ";" |
|
|
869
81ee93b87258
changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents:
864
diff
changeset
|
212 |
val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
|
| 864 | 213 |
val DIGIT : Rexp = RANGE(('0' to '9').toSet)
|
214 |
val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
|
|
| 919 | 215 |
val STRING : Rexp = "\"" ~ (LET | SYM | DIGIT | " " | "\\n").% ~ "\"" |
| 864 | 216 |
val ID : Rexp = LET ~ (LET | "_" | DIGIT).% |
217 |
val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%) |
|
|
869
81ee93b87258
changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents:
864
diff
changeset
|
218 |
val EOL : Rexp = "\n" | "\r\n" |
| 961 | 219 |
val COMMENT : Rexp = "//" ~ (LET | SYM | PARENS | " " | DIGIT).% ~ EOL |
| 864 | 220 |
|
| 961 | 221 |
val WHILE_REGS = (("k" $ KEYWORD) |
|
222 |
("o" $ OP) |
|
|
| 864 | 223 |
("str" $ STRING) |
|
224 |
("p" $ PARENS) |
|
|
| 961 | 225 |
("s" $ SEMI) |
|
226 |
("w" $ WHITESPACE) |
|
|
227 |
("i" $ ID) |
|
|
| 864 | 228 |
("n" $ NUM) |
|
| 961 | 229 |
("c" $ COMMENT)).%
|
230 |
||
231 |
||
| 864 | 232 |
|
| 919 | 233 |
|
234 |
||
| 961 | 235 |
def escapedChar(ch: Char): String = ch match {
|
236 |
case '\b' => "\\b" |
|
237 |
case '\t' => "\\t" |
|
238 |
case '\n' => "\\n" |
|
239 |
case '\f' => "\\f" |
|
240 |
case '\r' => "\\r" |
|
241 |
case '"' => "\\\"" |
|
242 |
case '\'' => "\\\'" |
|
243 |
case '\\' => "\\\\" |
|
244 |
case _ => if (ch.isControl) "\\0" + Integer.toOctalString(ch.toInt) |
|
245 |
else String.valueOf(ch) |
|
246 |
} |
|
247 |
||
248 |
def esc(s: String): String = "\"" + escapeImpl(s) + "\"" |
|
249 |
def escapeImpl(s: String): String = s.flatMap(escapedChar) |
|
250 |
||
251 |
/* |
|
| 919 | 252 |
def esc(raw: String): String = {
|
253 |
import scala.reflect.runtime.universe._ |
|
254 |
Literal(Constant(raw)).toString |
|
255 |
} |
|
| 961 | 256 |
*/ |
| 919 | 257 |
|
258 |
def escape(tks: List[(String, String)]) = |
|
| 961 | 259 |
tks.map{ case (s1, s2) => (s1, s2)}
|
| 919 | 260 |
|
261 |
||
262 |
// Tokens |
|
| 961 | 263 |
abstract class Token extends Serializable |
| 864 | 264 |
case class T_KEYWORD(s: String) extends Token |
265 |
case class T_OP(s: String) extends Token |
|
266 |
case class T_STRING(s: String) extends Token |
|
267 |
case class T_PAREN(s: String) extends Token |
|
268 |
case object T_SEMI extends Token |
|
269 |
case class T_ID(s: String) extends Token |
|
270 |
case class T_NUM(n: Int) extends Token |
|
271 |
||
272 |
val token : PartialFunction[(String, String), Token] = {
|
|
273 |
case ("k", s) => T_KEYWORD(s)
|
|
274 |
case ("o", s) => T_OP(s)
|
|
275 |
case ("str", s) => T_STRING(s)
|
|
276 |
case ("p", s) => T_PAREN(s)
|
|
277 |
case ("s", _) => T_SEMI
|
|
278 |
case ("i", s) => T_ID(s)
|
|
279 |
case ("n", s) => T_NUM(s.toInt)
|
|
280 |
} |
|
281 |
||
282 |
// Tokenise |
|
| 961 | 283 |
def tokenise(s: String) = //: List[Token] = |
284 |
escape(lexing_simp(WHILE_REGS, s)).collect(token) |
|
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
920
diff
changeset
|
285 |
|
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
920
diff
changeset
|
286 |
|
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
920
diff
changeset
|
287 |
|
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
920
diff
changeset
|
288 |
|
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
920
diff
changeset
|
289 |
println(tokenise(os.read(os.pwd / "primes.while"))) |