--- a/progs/cw1.scala Wed Nov 27 08:49:51 2013 +0000
+++ b/progs/cw1.scala Wed Nov 27 21:36:30 2013 +0000
@@ -95,6 +95,21 @@
case OPT(_) => true
}
+def zeroable (r: Rexp) : Boolean = r match {
+ case NULL => true
+ case EMPTY => false
+ case CHAR(_) => false
+ case ALT(r1, r2) => zeroable(r1) && zeroable(r2)
+ case SEQ(r1, r2) => zeroable(r1) || zeroable(r2)
+ case STAR(_) => false
+ case PLUS(r) => zeroable(r)
+ case NTIMES(r, i) => if (i == 0) false else zeroable(r)
+ case NUPTOM(r, i, j) => if (i == 0) false else zeroable(r)
+ case RANGE(_) => false
+ case NOT(r) => !(zeroable(r))
+ case OPT(_) => false
+}
+
// derivative of a regular expression w.r.t. a character
def der (c: Char, r: Rexp) : Rexp = r match {
case NULL => NULL
@@ -212,3 +227,36 @@
println(ders_simp("aaaaa".toList, test))
println(ders_simp("aaaaaa".toList, test))
println(ders_simp("aaaaaaa".toList, test))
+
+def TEST(s: String, r: Rexp) = {
+ println("Rexp |" + s + "|")
+ println("Derivative:\n" + ders_simp(s.toList, r))
+ println("Is Nullable: " + nullable(ders_simp(s.toList, r)))
+ println("Is Zeroable: " + zeroable(ders_simp(s.toList, r)))
+ Console.readLine
+}
+
+
+val ALL2 = "a" | "f"
+val COMMENT2 = ("/*" ~ NOT(ALL2.% ~ "*/" ~ ALL2.%) ~ "*/")
+
+println("1) TEST TEST")
+TEST("", COMMENT2)
+TEST("/", COMMENT2)
+TEST("/*", COMMENT2)
+TEST("/*f", COMMENT2)
+TEST("/*f*", COMMENT2)
+TEST("/*f*/", COMMENT2)
+TEST("/*f*/ ", COMMENT2)
+
+val ALL3 = "a" | "f"
+val COMMENT3 = ("/" ~ NOT(ALL3.% ~ "&" ~ ALL3.%) ~ "&")
+
+println("2) TEST TEST")
+TEST("", COMMENT3)
+TEST("/", COMMENT3)
+TEST("/", COMMENT3)
+TEST("/f", COMMENT3)
+TEST("/f&", COMMENT3)
+TEST("/f& ", COMMENT3)
+