testing4/re.scala
changeset 249 1997cfcd6334
parent 236 e461b5325b5e
child 300 72688efdf17c
equal deleted inserted replaced
248:1616d06a0893 249:1997cfcd6334
     1 // Part 1 about Regular Expression Matching
     1 // Part 1 about Regular Expression Matching
     2 //==========================================
     2 //==========================================
       
     3 
       
     4 //object CW9a {
     3 
     5 
     4 // Regular Expressions
     6 // Regular Expressions
     5 abstract class Rexp
     7 abstract class Rexp
     6 case object ZERO extends Rexp
     8 case object ZERO extends Rexp
     7 case object ONE extends Rexp
     9 case object ONE extends Rexp
     8 case class CHAR(c: Char) extends Rexp
    10 case class CHAR(c: Char) extends Rexp
     9 case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
    11 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
    10 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
    11 case class STAR(r: Rexp) extends Rexp             // star
    13 case class STAR(r: Rexp) extends Rexp 
    12 
    14 
    13  
    15 // some convenience for typing in regular expressions
    14 // some convenience for typing regular expressions
       
    15 
    16 
    16 import scala.language.implicitConversions    
    17 import scala.language.implicitConversions    
    17 import scala.language.reflectiveCalls 
    18 import scala.language.reflectiveCalls 
       
    19 
    18 
    20 
    19 def charlist2rexp(s: List[Char]): Rexp = s match {
    21 def charlist2rexp(s: List[Char]): Rexp = s match {
    20   case Nil => ONE
    22   case Nil => ONE
    21   case c::Nil => CHAR(c)
    23   case c::Nil => CHAR(c)
    22   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    24   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    42 // function checks whether a regular expression
    44 // function checks whether a regular expression
    43 // can match the empty string and Returns a boolean
    45 // can match the empty string and Returns a boolean
    44 // accordingly.
    46 // accordingly.
    45 
    47 
    46 def nullable (r: Rexp) : Boolean = r match {
    48 def nullable (r: Rexp) : Boolean = r match {
    47     case ZERO => false
    49   case ZERO => false
    48     case ONE => true
    50   case ONE => true
    49     case CHAR(_) => false
    51   case CHAR(_) => false
    50     case ALT(r1, r2) => nullable(r1) | nullable(r2)
    52   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    51     case SEQ(r1, r2) => nullable(r1) & nullable(r2)
    53   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
    52     case STAR(_) => true
    54   case STAR(_) => true
    53 }
    55 }
    54 
       
    55 
       
    56 
    56 
    57 // (2) Complete the function der according to
    57 // (2) Complete the function der according to
    58 // the definition given in the coursework; this
    58 // the definition given in the coursework; this
    59 // function calculates the derivative of a 
    59 // function calculates the derivative of a 
    60 // regular expression w.r.t. a character.
    60 // regular expression w.r.t. a character.
    61 
    61 
    62 //TODO: debug
       
    63 //TODO: understand this more. 
       
    64 // first test runs
       
    65 // test 2 fails
       
    66 // test 3 runs
       
    67 // test 4 runs
       
    68 def der (c: Char, r: Rexp) : Rexp = r match {
    62 def der (c: Char, r: Rexp) : Rexp = r match {
    69     //TODO: debug
    63   case ZERO => ZERO
    70     case ZERO => ZERO
    64   case ONE => ZERO
    71     case ONE => ZERO
    65   case CHAR(d) => if (c == d) ONE else ZERO
    72     case CHAR(r1) => if (c == r1) ONE else ZERO
    66   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    73     case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    67   case SEQ(r1, r2) => 
    74     case SEQ(r1, r2) => if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) else SEQ(der(c, r1), r2)
    68     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    75     case STAR(r1) => SEQ(der(c, r1), STAR(r1))
    69     else SEQ(der(c, r1), r2)
       
    70   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
    76 }
    71 }
    77 
       
    78 
    72 
    79 // (3) Complete the simp function according to
    73 // (3) Complete the simp function according to
    80 // the specification given in the coursework; this
    74 // the specification given in the coursework; this
    81 // function simplifies a regular expression from
    75 // function simplifies a regular expression from
    82 // the inside out, like you would simplify arithmetic 
    76 // the inside out, like you would simplify arithmetic 
    83 // expressions; however it does not simplify inside 
    77 // expressions; however it does not simplify inside 
    84 // STAR-regular expressions.
    78 // STAR-regular expressions.
    85 
    79 
    86 def simp(r: Rexp) : Rexp = r match {
    80 def simp(r: Rexp) : Rexp = r match {
    87     case STAR(_) => r
    81   case ALT(r1, r2) => (simp(r1), simp(r2)) match {
    88     case SEQ(r1, r2) => (simp(r1), simp(r2)) match { // potential failure
    82     case (ZERO, r2s) => r2s
    89         case (_, ZERO) => ZERO
    83     case (r1s, ZERO) => r1s
    90         case (ZERO, _) => ZERO
    84     case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
    91         case (r1, ONE) => simp(r1)
    85   }
    92         case (ONE, r2) => simp(r2)
    86   case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
    93         case (r1, r2) => SEQ(r1, r2)
    87     case (ZERO, _) => ZERO
    94     }
    88     case (_, ZERO) => ZERO
    95     case ALT(r1, r2) => (simp(r1), simp(r2)) match {
    89     case (ONE, r2s) => r2s
    96         case (r1, ZERO) => simp(r1)
    90     case (r1s, ONE) => r1s
    97         case (ZERO, r1) => simp(r1)
    91     case (r1s, r2s) => SEQ(r1s, r2s)
    98         case (r1, r2) if r1 == r2 => simp(r1)
    92   }
    99         case (r1, r2) => ALT(r1, r2)
    93   case r => r
   100     }
       
   101     case r => r
       
   102 }
    94 }
   103 
    95 
   104 
    96 
   105 // (4) Complete the two functions below; the first 
    97 // (4) Complete the two functions below; the first 
   106 // calculates the derivative w.r.t. a string; the second
    98 // calculates the derivative w.r.t. a string; the second
   107 // is the regular expression matcher taking a regular
    99 // is the regular expression matcher taking a regular
   108 // expression and a string and checks whether the
   100 // expression and a string and checks whether the
   109 // string matches the regular expression
   101 // string matches the regular expression.
   110 
   102 
   111 def ders (s: List[Char], r: Rexp) : Rexp = s match {
   103 def ders (s: List[Char], r: Rexp) : Rexp = s match {
   112     case Nil => r
   104   case Nil => r
   113     case c::cs => ders(cs, simp(der(c, r)))
   105   case c::s => ders(s, simp(der(c, r)))
   114 }
   106 }
   115 
   107 
   116 def matcher(r: Rexp, s: String): Boolean = {
   108 // main matcher function
   117     nullable(ders(s.toList, r))
   109 def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
   118 }
       
   119 
       
   120 
   110 
   121 // (5) Complete the size function for regular
   111 // (5) Complete the size function for regular
   122 // expressions according to the specification 
   112 // expressions according to the specification 
   123 // given in the coursework.
   113 // given in the coursework.
   124 
   114 
       
   115 
   125 def size(r: Rexp): Int = r match {
   116 def size(r: Rexp): Int = r match {
   126     case ZERO => 1
   117   case ZERO => 1
   127     case ONE => 1
   118   case ONE => 1
   128     case CHAR(_) => 1
   119   case CHAR(_) => 1
   129     case SEQ(r1, r2) => 1 + size(r1) + size(r2)
   120   case ALT(r1, r2) => 1 + size(r1) + size (r2)
   130     case ALT(r1, r2) => 1 + size(r1) + size(r2)
   121   case SEQ(r1, r2) => 1 + size(r1) + size (r2)
   131     case STAR(r1) => 1 + size(r1)
   122   case STAR(r1) => 1 + size(r1)
   132 }
   123 }
   133 
   124 
   134 
   125 
       
   126 
   135 // some testing data
   127 // some testing data
   136 
   128 /*
   137 //matcher(("a" ~ "b") ~ "c", "abc")  // => true
   129 matcher(("a" ~ "b") ~ "c", "abc")  // => true
   138 //matcher(("a" ~ "b") ~ "c", "ab")   // => false
   130 matcher(("a" ~ "b") ~ "c", "ab")   // => false
   139 
   131 
   140 // the supposedly 'evil' regular expression (a*)* b
   132 // the supposedly 'evil' regular expression (a*)* b
   141 val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
   133 val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
   142 
   134 
   143 //matcher(EVIL, "a" * 1000 ++ "b")   // => true
   135 matcher(EVIL, "a" * 1000 ++ "b")   // => true
   144 //matcher(EVIL, "a" * 1000)          // => false
   136 matcher(EVIL, "a" * 1000)          // => false
   145 
   137 
   146 // size without simplifications
   138 // size without simplifications
   147 //size(der('a', der('a', EVIL)))             // => 28
   139 size(der('a', der('a', EVIL)))             // => 28
   148 //size(der('a', der('a', der('a', EVIL))))   // => 58
   140 size(der('a', der('a', der('a', EVIL))))   // => 58
   149 
       
   150 
       
   151 
   141 
   152 // size with simplification
   142 // size with simplification
   153 //size(simp(der('a', der('a', EVIL))))           // => 8
   143 size(simp(der('a', der('a', EVIL))))           // => 8
   154 //size(simp(der('a', der('a', der('a', EVIL))))) // => 8
   144 size(simp(der('a', der('a', der('a', EVIL))))) // => 8
   155 
   145 
   156 // Python needs around 30 seconds for matching 28 a's with EVIL. 
   146 // Python needs around 30 seconds for matching 28 a's with EVIL. 
   157 // Java 9 and later increase this to an "astonishing" 40000 a's in
   147 // Java 9 and later increase this to an "astonishing" 40000 a's in
   158 // 30 seconds.
   148 // around 30 seconds.
   159 //
   149 //
   160 // Lets see how long it really takes to match strings with 
   150 // Lets see how long it takes to match strings with 
   161 // 5 Million a's...it should be in the range of a couple
   151 // 5 Million a's...it should be in the range of a 
   162 // of seconds.
   152 // couple of seconds.
   163 
   153 
   164 def time_needed[T](i: Int, code: => T) = {
   154 def time_needed[T](i: Int, code: => T) = {
   165   val start = System.nanoTime()
   155   val start = System.nanoTime()
   166   for (j <- 1 to i) code
   156   for (j <- 1 to i) code
   167   val end = System.nanoTime()
   157   val end = System.nanoTime()
   168   (end - start)/(i * 1.0e9)
   158   (end - start)/(i * 1.0e9)
   169 }
   159 }
   170 
   160 
   171 //for (i <- 0 to 5000000 by 500000) {
   161 for (i <- 0 to 5000000 by 500000) {
   172 //  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
   162   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
   173 //}
   163 }
   174 
   164 
   175 // another "power" test case 
   165 // another "power" test case 
   176 println(simp(Iterator.iterate(ONE:Rexp)(r => ALT(r, r)).drop(40).next))
   166 simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next) == ONE
   177 
   167 
   178 // the Iterator produces the rexp
   168 // the Iterator produces the rexp
   179 //
   169 //
   180 //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
   170 //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
   181 //
   171 //
   182 //    where SEQ is nested 50 times.
   172 //    where SEQ is nested 100 times.
       
   173  
       
   174 */
   183 
   175 
   184 
   176 //}