--- a/progs/re4.scala Wed Sep 25 11:24:34 2019 +0100
+++ b/progs/re4.scala Wed Sep 25 23:56:36 2019 +0100
@@ -33,7 +33,7 @@
else SEQ(der(c, r1), r2)
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
case NTIMES(r1, i) =>
- if (i == 0) ZERO else der(c, SEQ(r1, NTIMES(r1, i - 1)))
+ if (i == 0) ZERO else SEQ(der(c, r1), NTIMES(r1, i - 1))
}
def simp(r: Rexp) : Rexp = r match {
@@ -72,7 +72,8 @@
}
// the main matcher function
-def matches(r: Rexp, s: String) : Boolean = nullable(ders2(s.toList, r))
+def matcher(r: Rexp, s: String) : Boolean =
+ nullable(ders2(s.toList, r))
// one or zero
@@ -89,19 +90,19 @@
val start = System.nanoTime()
for (j <- 1 to i) code
val end = System.nanoTime()
- "%.5f".format((end - start) / (i * 1.0e9))
+ (end - start) / (i * 1.0e9)
}
// test: (a?{n}) (a{n})
for (i <- 0 to 7000000 by 500000) {
- println(s"$i: ${time_needed(2, matches(EVIL1(i), "a" * i))}")
+ println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f")
}
// test: (a*)* b
-for (i <- 1 to 7000001 by 500000) {
- println(s"$i: ${time_needed(2, matches(EVIL2, "a" * i))}")
+for (i <- 0 to 7000000 by 500000) {
+ println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
}
@@ -135,7 +136,7 @@
val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
// test: ("" | "a" | "aa")*
-val EVIL3 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
+val EVIL4 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
val t1 = ders2("a".toList, EVIL3)
val t1s = simp(t1)