diff -r 3e71efb25ce9 -r 43c116860e47 progs/re4.scala --- a/progs/re4.scala Wed Oct 02 00:10:52 2013 +0100 +++ b/progs/re4.scala Wed Oct 02 01:02:50 2013 +0100 @@ -1,4 +1,5 @@ import scala.annotation.tailrec + abstract class Rexp { def simp : Rexp = this } @@ -12,7 +13,7 @@ case (r, NULL) => r case (r, EMPTY) => if (nullable(r)) r else ALT(r, EMPTY) case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY) - case (r1, r2) => ALT(r1, r2) + case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2) } } case class SEQ(r1: Rexp, r2: Rexp) extends Rexp { @@ -24,8 +25,21 @@ case (r1, r2) => SEQ(r1, r2) } } -case class STAR(r: Rexp) extends Rexp -case class NTIMES(r: Rexp, n: Int) extends Rexp +case class STAR(r: Rexp) extends Rexp { + override def simp = r.simp match { + case NULL => EMPTY + case EMPTY => EMPTY + case r => STAR(r) + } +} +case class NTIMES(r: Rexp, n: Int) extends Rexp { + override def simp = if (n == 0) EMPTY else + r.simp match { + case NULL => NULL + case EMPTY => EMPTY + case r => NTIMES(r, n) + } +} // some convenience for typing in regular expressions def charlist2rexp(s : List[Char]) : Rexp = s match { @@ -45,7 +59,7 @@ case ALT(r1, r2) => nullable(r1) || nullable(r2) case SEQ(r1, r2) => nullable(r1) && nullable(r2) case STAR(_) => true - case NTIMES(r, i) => if (i == 0) false else nullable(r) + case NTIMES(r, i) => if (i == 0) true else nullable(r) } // derivative of a regular expression w.r.t. a character @@ -73,18 +87,10 @@ def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) - //one or zero def OPT(r: Rexp) = ALT(r, EMPTY) -//n-times -/*def NTIMES(r: Rexp, n: Int) : Rexp = n match { - case 0 => NULL - case 1 => r - case n => SEQ(r, NTIMES(r, n - 1)) -}*/ - -def RTEST(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n)) +def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n)) def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() @@ -94,8 +100,8 @@ } -for (i <- 1 to 13001 by 500) { - println(i + " " + time_needed(1, matcher(RTEST(i), "a" * i))) +for (i <- 1 to 12001 by 500) { + println(i + " " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i)))) }