solution/cw3/lexer.sc
changeset 869 81ee93b87258
parent 864 b5b1bc0a603b
--- a/solution/cw3/lexer.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw3/lexer.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -202,16 +202,17 @@
 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 SYM : Rexp = (LET | RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':')))
 val PARENS : Rexp = "(" | "{" | ")" | "}"
 val SEMI : Rexp = ";"
-val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t"
+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 ID : Rexp = LET ~ (LET | "_" | DIGIT).%
 val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%)
-val COMMENT : Rexp = "//" ~ (SYM | " " | DIGIT).% ~ "\n" 
+val EOL : Rexp = "\n" | "\r\n"
+val COMMENT : Rexp = "//" ~ (SYM | PARENS | " " | DIGIT).% ~ EOL 
 
 val WHILE_REGS = (("k" $ KEYWORD) | 
                   ("o" $ OP) | 
@@ -248,3 +249,18 @@
   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))
+