diff -r 53f08d873e09 -r 7af2eea19646 solutions/cw2/lexer.sc --- a/solutions/cw2/lexer.sc Sun Sep 17 19:12:57 2023 +0100 +++ b/solutions/cw2/lexer.sc Tue Sep 19 09:54:41 2023 +0100 @@ -1,5 +1,7 @@ -import scala.language.implicitConversions -import scala.language.reflectiveCalls +// CW 2 +//====== + + // Rexp abstract class Rexp @@ -27,7 +29,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) @@ -207,17 +209,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 | DIGIT | SYM | PARENS | " " | "\\n").% ~ "\"" val ID : Rexp = LET ~ (LET | "_" | DIGIT).% val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%) -val COMMENT : Rexp = "//" ~ (SYM | " " | DIGIT).% ~ ("\n" | "\r\n") +val EOL : Rexp = "\n" | "\r\n" +val COMMENT : Rexp = "//" ~ (LET | DIGIT | SYM | " ").% ~ EOL val WHILE_REGS = (("k" $ KEYWORD) | ("o" $ OP) | @@ -227,7 +230,7 @@ ("w" $ WHITESPACE) | ("i" $ ID) | ("n" $ NUM) | - ("c" $ COMMENT)).% + ("c" $ COMMENT)).% def esc(raw: String): String = { import scala.reflect.runtime.universe._ @@ -263,6 +266,7 @@ // Q2 Tests + lex_simp(NTIMES("a", 3), "aaa".toList) lex_simp(NTIMES(("a" | ONE), 3), "aa".toList) @@ -312,7 +316,7 @@ println(tokenise(prog3)) -println("MY TESTS") +// More tests println(lex_simp("x" $ OPTIONAL("a"), "a".toList)) println(lex_simp("x" $ OPTIONAL("a"), "".toList))