--- a/solutions/cw3/lexer.sc Fri Sep 15 10:49:33 2023 +0100
+++ b/solutions/cw3/lexer.sc Sun Sep 17 19:12:57 2023 +0100
@@ -37,13 +37,13 @@
implicit def string2rexp(s : String) : Rexp =
charlist2rexp(s.toList)
-implicit def RexpOps(r: Rexp) = new {
+extension (r: Rexp) {
def | (s: Rexp) = ALT(r, s)
def % = STAR(r)
def ~ (s: Rexp) = SEQ(r, s)
}
-implicit def stringOps(s: String) = new {
+extension (s: String) {
def | (r: Rexp) = ALT(s, r)
def | (r: String) = ALT(s, r)
def % = STAR(s)
@@ -82,7 +82,7 @@
case RECD(_, r1) => der(c, r1)
case RANGE(s) => if (s.contains(c)) ONE else ZERO
case PLUS(r1) => SEQ(der(c, r1), STAR(r1))
- case OPTIONAL(r1) => ALT(der(c, r1), ZERO)
+ case OPTIONAL(r1) => der(c, r1)
case NTIMES(r, i) =>
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
}
@@ -119,7 +119,7 @@
case RECD(x, r) => Rec(x, mkeps(r))
case PLUS(r) => Stars(List(mkeps(r))) // the first copy must match the empty string
- case OPTIONAL(r) => Right(Empty)
+ case OPTIONAL(r) => if (nullable(r)) Stars(List(mkeps(r))) else Stars(Nil)
case NTIMES(r, i) => Stars(List.fill(i)(mkeps(r)))
}
@@ -136,7 +136,7 @@
case (RANGE(_), Empty) => Chr(c)
case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
- case (OPTIONAL(r), Left(v1)) => Left(inj(r, c, v1))
+ case (OPTIONAL(r), Left(v1)) => Stars(List(inj(r, c, v1)))
case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
}
@@ -201,18 +201,18 @@
// Language specific code
val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip"
val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||"
-val LET: Rexp = RANGE(('A' to 'Z').toSet ++ ('a' to 'z'))
-val SYM : Rexp = (LET | RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':')))
+val LET: Rexp = RANGE(('A' to 'Z').toSet ++ ('a' to 'z').toSet)
+val SYM : Rexp = RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':', ')', '('))
val PARENS : Rexp = "(" | "{" | ")" | "}"
val SEMI : Rexp = ";"
val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
val DIGIT : Rexp = RANGE(('0' to '9').toSet)
val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
-val STRING : Rexp = "\"" ~ (SYM | " " | "\\n" | DIGIT).% ~ "\""
+val STRING : Rexp = "\"" ~ (LET | SYM | DIGIT | " " | "\\n").% ~ "\""
val ID : Rexp = LET ~ (LET | "_" | DIGIT).%
val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%)
val EOL : Rexp = "\n" | "\r\n"
-val COMMENT : Rexp = "//" ~ (SYM | PARENS | " " | DIGIT).% ~ EOL
+val COMMENT : Rexp = "//" ~ (LET | SYM | PARENS | " " | DIGIT).% ~ EOL
val WHILE_REGS = (("k" $ KEYWORD) |
("o" $ OP) |
@@ -224,7 +224,18 @@
("n" $ NUM) |
("c" $ COMMENT)).%
-// Token
+
+
+def esc(raw: String): String = {
+ import scala.reflect.runtime.universe._
+ Literal(Constant(raw)).toString
+}
+
+def escape(tks: List[(String, String)]) =
+ tks.map{ case (s1, s2) => (s1, esc(s2))}
+
+
+// Tokens
abstract class Token extends Serializable
case class T_KEYWORD(s: String) extends Token
case class T_OP(s: String) extends Token
@@ -247,21 +258,3 @@
// Tokenise
def tokenise(s: String) : List[Token] =
lexing_simp(WHILE_REGS, s).collect(token)
-
-
-val fact = """
-write "Input n please";
-read n;
-write "The factors of n are";
-f := 2;
-while n != 1 do {
- while (n / f) * f == n do {
- write f;
- n := n / f
- };
- f := f + 1
-}
-"""
-
-//println(tokenise(fact))
-