testing4/re.scala
changeset 236 e461b5325b5e
parent 229 5549016ab10f
child 249 1997cfcd6334
--- a/testing4/re.scala	Thu Dec 06 18:56:26 2018 +0000
+++ b/testing4/re.scala	Thu Dec 06 21:49:43 2018 +0000
@@ -1,23 +1,21 @@
 // Part 1 about Regular Expression Matching
 //==========================================
 
-//object CW9a {
-
 // Regular Expressions
 abstract class Rexp
 case object ZERO extends Rexp
 case object ONE extends Rexp
 case class CHAR(c: Char) extends Rexp
-case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
-case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
-case class STAR(r: Rexp) extends Rexp 
+case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
+case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
+case class STAR(r: Rexp) extends Rexp             // star
 
-// some convenience for typing in regular expressions
+ 
+// some convenience for typing regular expressions
 
 import scala.language.implicitConversions    
 import scala.language.reflectiveCalls 
 
-
 def charlist2rexp(s: List[Char]): Rexp = s match {
   case Nil => ONE
   case c::Nil => CHAR(c)
@@ -46,30 +44,38 @@
 // accordingly.
 
 def nullable (r: Rexp) : Boolean = r match {
-  case ZERO => false
-  case ONE => true
-  case CHAR(_) => false
-  case ALT(r1, r2) => nullable(r1) || nullable(r2)
-  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
-  case STAR(_) => true
+    case ZERO => false
+    case ONE => true
+    case CHAR(_) => false
+    case ALT(r1, r2) => nullable(r1) | nullable(r2)
+    case SEQ(r1, r2) => nullable(r1) & nullable(r2)
+    case STAR(_) => true
 }
 
+
+
 // (2) Complete the function der according to
 // the definition given in the coursework; this
 // function calculates the derivative of a 
 // regular expression w.r.t. a character.
 
+//TODO: debug
+//TODO: understand this more. 
+// first test runs
+// test 2 fails
+// test 3 runs
+// test 4 runs
 def der (c: Char, r: Rexp) : Rexp = r match {
-  case ZERO => ZERO
-  case ONE => ZERO
-  case CHAR(d) => if (c == d) ONE else ZERO
-  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
-  case SEQ(r1, r2) => 
-    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
-    else SEQ(der(c, r1), r2)
-  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
+    //TODO: debug
+    case ZERO => ZERO
+    case ONE => ZERO
+    case CHAR(r1) => if (c == r1) ONE else ZERO
+    case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
+    case SEQ(r1, r2) => if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) else SEQ(der(c, r1), r2)
+    case STAR(r1) => SEQ(der(c, r1), STAR(r1))
 }
 
+
 // (3) Complete the simp function according to
 // the specification given in the coursework; this
 // function simplifies a regular expression from
@@ -78,19 +84,21 @@
 // STAR-regular expressions.
 
 def simp(r: Rexp) : Rexp = r match {
-  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
-    case (ZERO, r2s) => r2s
-    case (r1s, ZERO) => r1s
-    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
-  }
-  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
-    case (ZERO, _) => ZERO
-    case (_, ZERO) => ZERO
-    case (ONE, r2s) => r2s
-    case (r1s, ONE) => r1s
-    case (r1s, r2s) => SEQ(r1s, r2s)
-  }
-  case r => r
+    case STAR(_) => r
+    case SEQ(r1, r2) => (simp(r1), simp(r2)) match { // potential failure
+        case (_, ZERO) => ZERO
+        case (ZERO, _) => ZERO
+        case (r1, ONE) => simp(r1)
+        case (ONE, r2) => simp(r2)
+        case (r1, r2) => SEQ(r1, r2)
+    }
+    case ALT(r1, r2) => (simp(r1), simp(r2)) match {
+        case (r1, ZERO) => simp(r1)
+        case (ZERO, r1) => simp(r1)
+        case (r1, r2) if r1 == r2 => simp(r1)
+        case (r1, r2) => ALT(r1, r2)
+    }
+    case r => r
 }
 
 
@@ -98,58 +106,60 @@
 // calculates the derivative w.r.t. a string; the second
 // is the regular expression matcher taking a regular
 // expression and a string and checks whether the
-// string matches the regular expression.
+// string matches the regular expression
 
 def ders (s: List[Char], r: Rexp) : Rexp = s match {
-  case Nil => r
-  case c::s => ders(s, simp(der(c, r)))
+    case Nil => r
+    case c::cs => ders(cs, simp(der(c, r)))
 }
 
-// main matcher function
-def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
+def matcher(r: Rexp, s: String): Boolean = {
+    nullable(ders(s.toList, r))
+}
+
 
 // (5) Complete the size function for regular
 // expressions according to the specification 
 // given in the coursework.
 
-
 def size(r: Rexp): Int = r match {
-  case ZERO => 1
-  case ONE => 1
-  case CHAR(_) => 1
-  case ALT(r1, r2) => 1 + size(r1) + size (r2)
-  case SEQ(r1, r2) => 1 + size(r1) + size (r2)
-  case STAR(r1) => 1 + size(r1)
+    case ZERO => 1
+    case ONE => 1
+    case CHAR(_) => 1
+    case SEQ(r1, r2) => 1 + size(r1) + size(r2)
+    case ALT(r1, r2) => 1 + size(r1) + size(r2)
+    case STAR(r1) => 1 + size(r1)
 }
 
 
+// some testing data
 
-// some testing data
-/*
-matcher(("a" ~ "b") ~ "c", "abc")  // => true
-matcher(("a" ~ "b") ~ "c", "ab")   // => false
+//matcher(("a" ~ "b") ~ "c", "abc")  // => true
+//matcher(("a" ~ "b") ~ "c", "ab")   // => false
 
 // the supposedly 'evil' regular expression (a*)* b
 val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 
-matcher(EVIL, "a" * 1000 ++ "b")   // => true
-matcher(EVIL, "a" * 1000)          // => false
+//matcher(EVIL, "a" * 1000 ++ "b")   // => true
+//matcher(EVIL, "a" * 1000)          // => false
 
 // size without simplifications
-size(der('a', der('a', EVIL)))             // => 28
-size(der('a', der('a', der('a', EVIL))))   // => 58
+//size(der('a', der('a', EVIL)))             // => 28
+//size(der('a', der('a', der('a', EVIL))))   // => 58
+
+
 
 // size with simplification
-size(simp(der('a', der('a', EVIL))))           // => 8
-size(simp(der('a', der('a', der('a', EVIL))))) // => 8
+//size(simp(der('a', der('a', EVIL))))           // => 8
+//size(simp(der('a', der('a', der('a', EVIL))))) // => 8
 
 // Python needs around 30 seconds for matching 28 a's with EVIL. 
 // Java 9 and later increase this to an "astonishing" 40000 a's in
-// around 30 seconds.
+// 30 seconds.
 //
-// Lets see how long it takes to match strings with 
-// 5 Million a's...it should be in the range of a 
-// couple of seconds.
+// Lets see how long it really takes to match strings with 
+// 5 Million a's...it should be in the range of a couple
+// of seconds.
 
 def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
@@ -158,19 +168,17 @@
   (end - start)/(i * 1.0e9)
 }
 
-for (i <- 0 to 5000000 by 500000) {
-  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
-}
+//for (i <- 0 to 5000000 by 500000) {
+//  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
+//}
 
 // another "power" test case 
-simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next) == ONE
+println(simp(Iterator.iterate(ONE:Rexp)(r => ALT(r, r)).drop(40).next))
 
 // the Iterator produces the rexp
 //
 //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
 //
-//    where SEQ is nested 100 times.
- 
-*/
+//    where SEQ is nested 50 times.
 
-//}
+