diff -r b47e140bcccd -r 47a299e7010f progs/re3a.scala --- a/progs/re3a.scala Thu Jul 25 14:39:37 2019 +0100 +++ b/progs/re3a.scala Sun Jul 28 01:00:41 2019 +0100 @@ -9,7 +9,7 @@ case class NTIMES(r: Rexp, n: Int) extends Rexp case class UPNTIMES(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 UPNTIMES(r: Rexp, n: Int) => true } -// 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 @@ -58,20 +58,20 @@ } -// derivative w.r.t. a string (iterates der) +// the derivative w.r.t. a string (iterates der) def ders (s: List[Char], r: Rexp) : Rexp = s match { case Nil => r case c::s => ders(s, simp(der(c, r))) } -// main matcher function -def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) +// the main matcher function +def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) -//one or zero +// one or zero def OPT(r: Rexp) = ALT(r, ONE) -//evil regular expressions +// evil regular expressions def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) val EVIL3 = SEQ(STAR(ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('a')))), CHAR('b')) @@ -80,26 +80,18 @@ val start = System.nanoTime() for (j <- 1 to i) code val end = System.nanoTime() - (end - start)/(i * 1.0e9) + "%.5f".format((end - start) / (i * 1.0e9)) } -//test: (a?{n}) (a{n}) -for (i <- 1 to 8001 by 1000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) +// test: (a?{n}) (a{n}) +for (i <- 0 to 8000 by 1000) { + println(s"$i: ${time_needed(2, matches(EVIL1(i), "a" * i))}") } -for (i <- 1 to 8001 by 1000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) -} - -//test: (a*)* b -for (i <- 1 to 6000001 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) -} - -for (i <- 1 to 6000001 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) +// test: (a*)* b +for (i <- 0 to 6000000 by 500000) { + println(s"$i: ${time_needed(2, matches(EVIL2, "a" * i))}") } @@ -113,8 +105,8 @@ //test: (a|aa)* b /* -for (i <- 1 to 7001 by 500) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i ++ "c")))) +for (i <- 0 to 100 by 10) { + println(s"$i: ${time_needed(2, matches(EVIL3, "a" * i ++ "c"))}") } */