progs/re1.scala
changeset 440 e14cd32ad497
parent 434 8664ff87cd77
child 453 36e5752fa191
equal deleted inserted replaced
439:7611ace6a93b 440:e14cd32ad497
    24   case CHAR(d) => if (c == d) ONE else ZERO
    24   case CHAR(d) => if (c == d) ONE else ZERO
    25   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    25   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    26   case SEQ(r1, r2) => 
    26   case SEQ(r1, r2) => 
    27     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    27     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    28     else SEQ(der(c, r1), r2)
    28     else SEQ(der(c, r1), r2)
    29   case STAR(r) => SEQ(der(c, r), STAR(r))
    29   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
    30 }
    30 }
    31 
    31 
    32 // derivative w.r.t. a string (iterates der)
    32 // derivative w.r.t. a string (iterates der)
    33 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    33 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    34   case Nil => r
    34   case Nil => r
    56 
    56 
    57 // the evil regular expression  a?{n} a{n}
    57 // the evil regular expression  a?{n} a{n}
    58 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
    58 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
    59 
    59 
    60 // the evil regular expression (a*)*b
    60 // the evil regular expression (a*)*b
    61 val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b'))
    61 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
    62 
    62 
    63 //for measuring time
    63 //for measuring time
    64 def time_needed[T](i: Int, code: => T) = {
    64 def time_needed[T](i: Int, code: => T) = {
    65   val start = System.nanoTime()
    65   val start = System.nanoTime()
    66   for (j <- 1 to i) code
    66   for (j <- 1 to i) code
    76 for (i <- 1 to 20) {
    76 for (i <- 1 to 20) {
    77   println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
    77   println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
    78 }
    78 }
    79 
    79 
    80 //test: (a*)* b
    80 //test: (a*)* b
    81 for (i <- 1 to 6502 by 500) {
    81 for (i <- 1 to 20) {
    82   println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
    82   println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
    83 }
    83 }
    84 
    84 
    85 for (i <- 1 to 6502 by 500) {
    85 for (i <- 1 to 20) {
    86   println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
    86   println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
    87 }
    87 }
    88 
    88