progs/matcher/re3.sc
changeset 825 dca072e2bb7d
parent 782 a26a20acd1c2
child 826 b0352633bf48
equal deleted inserted replaced
824:284ac979f289 825:dca072e2bb7d
    33   case STAR(_) => true
    33   case STAR(_) => true
    34   case NTIMES(r, i) => if (i == 0) true else nullable(r)
    34   case NTIMES(r, i) => if (i == 0) true else nullable(r)
    35 }
    35 }
    36 
    36 
    37 // the derivative of a regular expression w.r.t. a character
    37 // the derivative of a regular expression w.r.t. a character
    38 def der (c: Char, r: Rexp) : Rexp = r match {
    38 def der(c: Char, r: Rexp) : Rexp = r match {
    39   case ZERO => ZERO
    39   case ZERO => ZERO
    40   case ONE => ZERO
    40   case ONE => ZERO
    41   case CHAR(d) => if (c == d) ONE else ZERO
    41   case CHAR(d) => if (c == d) ONE else ZERO
    42   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    42   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    43   case SEQ(r1, r2) => 
    43   case SEQ(r1, r2) => 
    95   (end - start)/(i * 1.0e9)
    95   (end - start)/(i * 1.0e9)
    96 }
    96 }
    97 
    97 
    98 
    98 
    99 //
    99 //
   100 @doc("Test (a?{n}) (a{n})")
   100 //@doc("Test (a?{n}) (a{n})")
   101 @main
   101 @main
   102 def test1() = {
   102 def test1() = {
   103   println("Test (a?{n}) (a{n})")
   103   println("Test (a?{n}) (a{n})")
   104 
   104 
   105   for (i <- 0 to 9000 by 1000) {
   105   for (i <- 0 to 9000 by 1000) {
   106     println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f")
   106     println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f")
   107   }
   107   }
   108 }  
   108 }  
   109 
   109 
   110 //
   110 //
   111 @doc("Test (a*)* b")
   111 //@doc("Test (a*)* b")
   112 @main
   112 @main
   113 def test2() = {
   113 def test2() = {
   114   println("Test (a*)* b")
   114   println("Test (a*)* b")
   115 
   115 
   116   for (i <- 0 to 6000000 by 500000) {
   116   for (i <- 0 to 6000000 by 500000) {