Attic/re4.scala
changeset 742 b5b5583a3a08
parent 638 0367aa7c764b
equal deleted inserted replaced
741:e66bd5c563eb 742:b5b5583a3a08
       
     1 // A version which attempts to move whole strings, not
       
     2 // just characters, under derivatives whenever possible
       
     3    
       
     4 abstract class Rexp
       
     5 case object ZERO extends Rexp
       
     6 case object ONE extends Rexp
       
     7 case class CHAR(c: Char) extends Rexp
       
     8 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
       
     9 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
       
    10 case class STAR(r: Rexp) extends Rexp 
       
    11 case class NTIMES(r: Rexp, n: Int) extends Rexp 
       
    12 
       
    13 // the nullable function: tests whether the regular 
       
    14 // expression can recognise the empty string
       
    15 def nullable (r: Rexp) : Boolean = r match {
       
    16   case ZERO => false
       
    17   case ONE => true
       
    18   case CHAR(_) => false
       
    19   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    20   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    21   case STAR(_) => true
       
    22   case NTIMES(r, i) => if (i == 0) true else nullable(r)
       
    23 }
       
    24 
       
    25 // the derivative of a regular expression w.r.t. a character
       
    26 def der (c: Char, r: Rexp) : Rexp = r match {
       
    27   case ZERO => ZERO
       
    28   case ONE => ZERO
       
    29   case CHAR(d) => if (c == d) ONE else ZERO
       
    30   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    31   case SEQ(r1, r2) => 
       
    32     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    33     else SEQ(der(c, r1), r2)
       
    34   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
       
    35   case NTIMES(r1, i) => 
       
    36     if (i == 0) ZERO else SEQ(der(c, r1), NTIMES(r1, i - 1))
       
    37 }
       
    38 
       
    39 def simp(r: Rexp) : Rexp = r match {
       
    40   case ALT(r1, r2) => (simp(r1), simp(r2)) match {
       
    41     case (ZERO, r2s) => r2s
       
    42     case (r1s, ZERO) => r1s
       
    43     case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
       
    44   }
       
    45   case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
       
    46     case (ZERO, _) => ZERO
       
    47     case (_, ZERO) => ZERO
       
    48     case (ONE, r2s) => r2s
       
    49     case (r1s, ONE) => r1s
       
    50     case (r1s, r2s) => SEQ(r1s, r2s)
       
    51   }
       
    52   case r => r
       
    53 }
       
    54 
       
    55 // an example
       
    56 val r = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z'))
       
    57 der('x', r)
       
    58 der('y', der('x', r))
       
    59 der('z', der('y', der('x', r)))
       
    60 simp(der('z', der('y', der('x', r))))
       
    61 
       
    62 // *new*
       
    63 // the derivative w.r.t. a string (iterates der)
       
    64 def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match {
       
    65   case (Nil, r) => r
       
    66   case (s, ZERO) => ZERO
       
    67   case (s, ONE) => if (s == Nil) ONE else ZERO
       
    68   case (s, CHAR(c)) => if (s == List(c)) ONE else 
       
    69                        if (s == Nil) CHAR(c) else ZERO
       
    70   case (s, ALT(r1, r2)) => ALT(ders2(s, r1), ders2(s, r2))
       
    71   case (c::s, r) => ders2(s, simp(der(c, r)))
       
    72 }
       
    73 
       
    74 // the main matcher function
       
    75 def matcher(r: Rexp, s: String) : Boolean = 
       
    76   nullable(ders2(s.toList, r))
       
    77 
       
    78 
       
    79 // one or zero
       
    80 def OPT(r: Rexp) = ALT(r, ONE)
       
    81 
       
    82 
       
    83 // Test Cases
       
    84 
       
    85 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
       
    86 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
       
    87 
       
    88 // for measuring time
       
    89 def time_needed[T](i: Int, code: => T) = {
       
    90   val start = System.nanoTime()
       
    91   for (j <- 1 to i) code
       
    92   val end = System.nanoTime()
       
    93   (end - start) / (i * 1.0e9)
       
    94 }
       
    95 
       
    96 
       
    97 // test: (a?{n}) (a{n})
       
    98 for (i <- 0 to 11000 by 1000) {
       
    99   println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f")
       
   100 }
       
   101 
       
   102 
       
   103 // test: (a*)* b
       
   104 for (i <- 0 to 7000000 by 500000) {
       
   105   println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
       
   106 }
       
   107 
       
   108 
       
   109 
       
   110 // the size of a regular expressions - for testing purposes 
       
   111 def size(r: Rexp) : Int = r match {
       
   112   case ZERO => 1
       
   113   case ONE => 1
       
   114   case CHAR(_) => 1
       
   115   case ALT(r1, r2) => 1 + size(r1) + size(r2)
       
   116   case SEQ(r1, r2) => 1 + size(r1) + size(r2)
       
   117   case STAR(r) => 1 + size(r)
       
   118   case NTIMES(r, _) => 1 + size(r)
       
   119 }
       
   120 
       
   121 
       
   122 // string of a regular expressions - for testing purposes 
       
   123 def string(r: Rexp) : String = r match {
       
   124   case ZERO => "0"
       
   125   case ONE => "1"
       
   126   case CHAR(c) => c.toString 
       
   127   case ALT(r1, r2) => s"(${string(r1)} + ${string(r2)})"
       
   128   case SEQ(CHAR(c), CHAR(d)) => s"${c}${d}"
       
   129   case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
       
   130   case STAR(r) => s"(${string(r)})*"
       
   131   case NTIMES(r, n) =>  s"(${string(r)}){${n}}"
       
   132 }
       
   133 
       
   134 
       
   135 // test: ("a" | "aa")*
       
   136 val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
       
   137 
       
   138 // test: ("" | "a" | "aa")*
       
   139 val EVIL4 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
       
   140 
       
   141 val t1  = ders2("a".toList, EVIL3)
       
   142 val t1s = simp(t1) 
       
   143 val t2  = ders2("aa".toList, t1s)
       
   144 val t2s = simp(t2)
       
   145 val t3  = ders2("aaa".toList, t2s)
       
   146 val t3s = simp(t3)
       
   147 val t4  = ders2("aaaa".toList, t3s)
       
   148 val t4s = simp(t4)
       
   149 println(string(t1) + "    " + size(t1))
       
   150 println("s " + string(t1s) + "    " + size(t1s))
       
   151 println(string(t2) + "    " + size(t2))
       
   152 println("s " + string(t2s) + "    " + size(t2s))
       
   153 println(string(t3) + "    " + size(t3))
       
   154 println("s " + string(t3s) + "    " + size(t3s))
       
   155 println(string(t4) + "    " + size(t4))
       
   156 println("s " + string(t4s) + "    " + size(t4s))