progs/scala/re-simp.scala
changeset 158 4e00dd2398ac
parent 157 1fe44fb6d0a4
child 166 cab1ae6f339a
equal deleted inserted replaced
157:1fe44fb6d0a4 158:4e00dd2398ac
    67   case STAR(r) => SEQ(der(c, r), STAR(r))
    67   case STAR(r) => SEQ(der(c, r), STAR(r))
    68   case RECD(_, r1) => der(c, r1)
    68   case RECD(_, r1) => der(c, r1)
    69 }
    69 }
    70 
    70 
    71 // derivative w.r.t. a string (iterates der)
    71 // derivative w.r.t. a string (iterates der)
       
    72 @tailrec
    72 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    73 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    73   case Nil => r
    74   case Nil => r
    74   case c::s => ders(s, der(c, r))
    75   case c::s => ders(s, der(c, r))
    75 }
    76 }
    76 
    77 
   189 
   190 
   190 
   191 
   191 // Some Tests
   192 // Some Tests
   192 //============
   193 //============
   193 
   194 
   194 def time[T](code: => T) = {
   195 def time_needed[T](i: Int, code: => T) = {
   195   val start = System.nanoTime()
   196   val start = System.nanoTime()
   196   val result = code
   197   for (j <- 1 to i) code
   197   val end = System.nanoTime()
   198   val end = System.nanoTime()
   198   println((end - start)/1.0e9)
   199   (end - start)/(i * 1.0e9)
   199   result
   200 }
   200 }
   201 
   201 
   202 
   202 val r0 = ("a" | "ab") ~ ("b" | "")
   203 val r0 = ("a" | "ab") ~ ("b" | "")
   203 println(lexing(r0, "ab"))
   204 println(lexing(r0, "ab"))
   204 println(lexing_simp(r0, "ab"))
   205 println(lexing_simp(r0, "ab"))
   205 
   206 
   264 
   265 
   265 val prog1 = """read  n; write (n)"""
   266 val prog1 = """read  n; write (n)"""
   266 println(env(lexing_simp(WHILE_REGS, prog1)))
   267 println(env(lexing_simp(WHILE_REGS, prog1)))
   267 
   268 
   268 
   269 
   269 // Big Test
   270 // Bigger Test
   270 //==========
   271 //=============
   271 val prog2 = """
   272 val prog2 = """
   272 i := 2;
   273 i := 2;
   273 max := 100;
   274 max := 100;
   274 while i < max do {
   275 while i < max do {
   275   isprime := 1;
   276   isprime := 1;
   309   print(i.toString + ":  ")
   310   print(i.toString + ":  ")
   310   time(lexing_simp(WHILE_REGS, prog2 * i))
   311   time(lexing_simp(WHILE_REGS, prog2 * i))
   311 }
   312 }
   312 */
   313 */
   313 
   314 
       
   315 // Sulzmann's tests
       
   316 //==================
       
   317 
       
   318 val sulzmann = ("a" | "b" | "ab").%
       
   319 
       
   320 println(lexing_simp(sulzmann, "a" * 10))
       
   321 
       
   322 for (i <- 1 to 4501 by 500) {
       
   323   println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "a" * i))))
       
   324 }
       
   325 
       
   326 for (i <- 1 to 2001 by 500) {
       
   327   println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "ab" * i))))
       
   328 }