--- a/solutions/cw2/lexer.sc Thu Nov 10 23:49:29 2022 +0000
+++ b/solutions/cw2/lexer.sc Tue Nov 15 11:34:33 2022 +0000
@@ -196,6 +196,11 @@
}
}
+def ders_simp(cs: List[Char], r: Rexp) : Rexp = cs match {
+ case Nil => r
+ case c::cs => ders_simp(cs, simp(der(c, r))._1)
+}
+
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
// Language specific code
@@ -221,7 +226,7 @@
("w" $ WHITESPACE) |
("i" $ ID) |
("n" $ NUM) |
- ("c" $ COMMENT)).%
+ ("c" $ COMMENT)).%
def esc(raw: String): String = {
import scala.reflect.runtime.universe._
@@ -346,7 +351,10 @@
case ALT(r1, r2) => 1 + size(r1) + size (r2)
case SEQ(r1, r2) => 1 + size(r1) + size (r2)
case STAR(r1) => 1 + size(r1)
+ case PLUS(r1) => 1 + size(r1)
case NTIMES(r1, n) => 1 + size(r1)
+ case RECD(_, r1) => 1 + size(r1)
+ case RANGE(_) => 1
}
val reg = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
@@ -355,3 +363,10 @@
size(simp(der('a', reg))._1) // 8
size(simp(der('a', der('a', reg)))._1) // 8
+
+
+// python tests
+println(size(WHILE_REGS))
+println(size(ders_simp("r".toList, WHILE_REGS)))
+println(size(ID))
+println(size(ders_simp("read".toList, ID)))