diff -r af5cb349fa9e -r 8e63f9745f46 progs/re4.scala --- a/progs/re4.scala Thu Jul 25 14:39:37 2019 +0100 +++ b/progs/re4.scala Sun Jul 28 01:00:41 2019 +0100 @@ -1,4 +1,4 @@ -// Version which attempts to move whole strings, not +// A version which attempts to move whole strings, not // just characters, under derivatives whenever possible abstract class Rexp @@ -10,7 +10,7 @@ case class STAR(r: Rexp) extends Rexp case class NTIMES(r: Rexp, n: Int) extends Rexp -// nullable function: tests whether the regular +// the nullable function: tests whether the regular // expression can recognise the empty string def nullable (r: Rexp) : Boolean = r match { case ZERO => false @@ -22,7 +22,7 @@ case NTIMES(r, i) => if (i == 0) true else nullable(r) } -// derivative of a regular expression w.r.t. a character +// the derivative of a regular expression w.r.t. a character def der (c: Char, r: Rexp) : Rexp = r match { case ZERO => ZERO case ONE => ZERO @@ -52,7 +52,7 @@ case r => r } -//example +// an example val r = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z')) der('x', r) der('y', der('x', r)) @@ -60,7 +60,7 @@ simp(der('z', der('y', der('x', r)))) // *new* -// derivative w.r.t. a string (iterates der) +// the derivative w.r.t. a string (iterates der) def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match { case (Nil, r) => r case (s, ZERO) => ZERO @@ -71,11 +71,11 @@ case (c::s, r) => ders2(s, simp(der(c, r))) } -// main matcher function -def matcher(r: Rexp, s: String) : Boolean = nullable(ders2(s.toList, r)) +// the main matcher function +def matches(r: Rexp, s: String) : Boolean = nullable(ders2(s.toList, r)) -//one or zero +// one or zero def OPT(r: Rexp) = ALT(r, ONE) @@ -84,35 +84,29 @@ def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) - +// for measuring time def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() for (j <- 1 to i) code val end = System.nanoTime() - (end - start)/(i * 1.0e9) -} - -//test: (a?{n}) (a{n}) -for (i <- 1 to 7000001 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) + "%.5f".format((end - start) / (i * 1.0e9)) } -for (i <- 1 to 7000001 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) + +// test: (a?{n}) (a{n}) +for (i <- 0 to 7000000 by 500000) { + println(s"$i: ${time_needed(2, matches(EVIL1(i), "a" * i))}") } -//test: (a*)* b + +// test: (a*)* b for (i <- 1 to 7000001 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) -} - -for (i <- 1 to 7000001 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) + println(s"$i: ${time_needed(2, matches(EVIL2, "a" * i))}") } -// size of a regular expressions - for testing purposes +// the size of a regular expressions - for testing purposes def size(r: Rexp) : Int = r match { case ZERO => 1 case ONE => 1 @@ -140,7 +134,7 @@ // test: ("a" | "aa")* val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))) -//test: ("" | "a" | "aa")* +// test: ("" | "a" | "aa")* val EVIL3 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))) val t1 = ders2("a".toList, EVIL3)