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