--- a/solutions/cw4/lexer.sc Sun Sep 17 19:12:57 2023 +0100
+++ b/solutions/cw4/lexer.sc Tue Sep 19 09:54:41 2023 +0100
@@ -1,5 +1,5 @@
-import scala.language.implicitConversions
-import scala.language.reflectiveCalls
+// Lexer from CW2 with additions to the rules
+//============================================
// Rexp
abstract class Rexp
@@ -27,7 +27,7 @@
case class Rec(x: String, v: Val) extends Val
-// Convenience typing
+// Convenience for typing
def charlist2rexp(s : List[Char]): Rexp = s match {
case Nil => ONE
case c::Nil => CHAR(c)
@@ -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), v1) => Stars(List(inj(r, c, v1)))
case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
}
@@ -199,20 +199,21 @@
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
// Language specific code
-val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip"
+// Language specific code
+val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "upto" | "true" | "false" | "read" | "write" | "skip" | "break"
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,6 +225,8 @@
("n" $ NUM) |
("c" $ COMMENT)).%
+
+
// Token
abstract class Token extends Serializable
case class T_KEYWORD(s: String) extends Token