main_testing3/re.scala
changeset 433 6af86ba1208f
parent 424 daf561a83ba6
child 455 557d18cce0f0
--- a/main_testing3/re.scala	Thu Nov 03 11:30:09 2022 +0000
+++ b/main_testing3/re.scala	Tue Nov 08 00:27:47 2022 +0000
@@ -1,5 +1,5 @@
 // Main Part 3 about Regular Expression Matching
-//=============================================
+//==============================================
 
 object M3 {
 
@@ -8,13 +8,15 @@
 case object ZERO extends Rexp
 case object ONE extends Rexp
 case class CHAR(c: Char) extends Rexp
-case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
-case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
-case class STAR(r: Rexp) extends Rexp             // star
+case class ALTs(rs: List[Rexp]) extends Rexp  // alternatives 
+case class SEQs(rs: List[Rexp]) extends Rexp  // sequences
+case class STAR(r: Rexp) extends Rexp         // star
 
 
-//the usual binary choice can be defined in terms of ALTs
+//the usual binary choice and binary sequence can be defined 
+//in terms of ALTs and SEQs
 def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
+def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
 
 // some convenience for typing in regular expressions
 import scala.language.implicitConversions    
@@ -41,83 +43,78 @@
   def ~ (r: String) = SEQ(s, r)
 }
 
-// (1) Complete the function nullable according to
-// the definition given in the coursework; this 
-// function checks whether a regular expression
-// can match the empty string and Returns a boolean
-// accordingly.
-
+// (1) 
 def nullable (r: Rexp) : Boolean = r match {
   case ZERO => false
   case ONE => true
   case CHAR(_) => false
   case ALTs(rs) => rs.exists(nullable)
-  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
+  case SEQs(rs) => rs.forall(nullable)
   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.
-
-def der (c: Char, r: Rexp) : Rexp = r match {
+// (2) 
+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 ALTs(rs) => ALTs(rs.map(der(c, _)))
-  case SEQ(r1, r2) => 
-    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
-    else SEQ(der(c, r1), r2)
+  case SEQs(Nil) => ZERO
+  case SEQs(r1::rs) => 
+    if (nullable(r1)) ALT(SEQs(der(c, r1)::rs), der(c, SEQs(rs)))
+    else SEQs(der(c, r1):: rs)
   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
 }
 
 
-// (3) Implement the flatten function flts. It
-// deletes 0s from a list of regular expressions
-// and also 'spills out', or flattens, nested 
-// ALTernativeS.
+// (3) 
+def denest(rs: List[Rexp]) : List[Rexp] = rs match {
+  case Nil => Nil
+  case ZERO::tl => denest(tl)
+  case ALTs(rs1)::rs2 => rs1 ::: denest(rs2)  
+  case r::rs => r :: denest(rs) 
+}
 
-def flts(rs: List[Rexp]) : List[Rexp] = rs match {
-  case Nil => Nil
-  case ZERO::tl => flts(tl)
-  case ALTs(rs1)::rs2 => rs1 ::: flts(rs2)  
-  case r::rs => r :: flts(rs) 
+// (4)
+def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = rs match {
+  case Nil => acc
+  case ZERO::rs => ZERO::Nil
+  case ONE::rs => flts(rs, acc)
+  case SEQs(rs1)::rs => flts(rs, acc ::: rs1)
+  case r::rs => flts(rs, acc :+ r) 
 }
 
-
+// (5)
+def ALTs_smart(rs: List[Rexp]) : Rexp = rs match {
+  case Nil => ZERO
+  case r::Nil => r  
+  case rs => ALTs(rs)
+}
 
-// (4) Complete the simp function according to
-// the specification given in the coursework; this
-// function simplifies a regular expression from
-// the inside out, like you would simplify arithmetic 
-// expressions; however it does not simplify inside 
-// STAR-regular expressions.
+def SEQs_smart(rs: List[Rexp]) : Rexp = rs match {
+  case Nil => ONE
+  case ZERO::nil => ZERO
+  case r::Nil => r
+  case rs => SEQs(rs) 
+}
 
+// (6) 
 
 def simp(r: Rexp) : Rexp = r match {
-  case ALTs(rs) => (flts(rs.map(simp)).distinct) match {
-    case Nil => ZERO
-    case r::Nil => r  
-    case rs => ALTs(rs)
-  }
-  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 ALTs(rs) => 
+    ALTs_smart(denest(rs.map(simp)).distinct)
+  case SEQs(rs) => 
+    SEQs_smart(flts(rs.map(simp)))
   case r => r
 }
 
-simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE))
+//println("Simp tests")
+//println(simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)))
+//println(simp(((CHAR('a') | ZERO) ~ ONE) | 
+//              (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))))
 
-// (5) Complete the two functions below; the first 
-// 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.
+
+// (7) 
 
 def ders (s: List[Char], r: Rexp) : Rexp = s match {
   case Nil => r
@@ -127,43 +124,40 @@
 // main matcher function
 def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
 
-// (6) Complete the size function for regular
-// expressions according to the specification 
-// given in the coursework.
-
+// (8) 
 
 def size(r: Rexp): Int = r match {
   case ZERO => 1
   case ONE => 1
   case CHAR(_) => 1
   case ALTs(rs) => 1 + rs.map(size).sum
-  case SEQ(r1, r2) => 1 + size(r1) + size (r2)
+  case SEQs(rs) => 1 + rs.map(size).sum
   case STAR(r1) => 1 + size(r1)
 }
 
 
 
 // some testing data
-
-//matcher(("a" ~ "b") ~ "c", "abc")  // => true
-//matcher(("a" ~ "b") ~ "c", "ab")   // => false
+/*
+println(matcher(("a" ~ "b") ~ "c", "abc"))  // => true
+println(matcher(("a" ~ "b") ~ "c", "ab"))   // => false
 
 // the supposedly 'evil' regular expression (a*)* b
-// val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
+val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 
-//println(matcher(EVIL, "a" * 1000 ++ "b"))   // => true
-//println(matcher(EVIL, "a" * 1000))          // => false
+println(matcher(EVIL, "a" * 1000 ++ "b"))   // => true
+println(matcher(EVIL, "a" * 1000))          // => false
 
 // size without simplifications
-//println(size(der('a', der('a', EVIL))))             // => 28
-//println(size(der('a', der('a', der('a', EVIL)))))   // => 58
+println(size(der('a', der('a', EVIL))))             // => 36
+println(size(der('a', der('a', der('a', EVIL)))))   // => 83
 
 // size with simplification
-//println(simp(der('a', der('a', EVIL))))          
-//println(simp(der('a', der('a', der('a', EVIL)))))
+println(simp(der('a', der('a', EVIL))))          
+println(simp(der('a', der('a', der('a', EVIL)))))
 
-//println(size(simp(der('a', der('a', EVIL)))))           // => 8
-//println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 8
+println(size(simp(der('a', der('a', EVIL)))))           // => 7
+println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 7
 
 // 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
@@ -171,7 +165,7 @@
 //
 // 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.
+// few seconds.
 
 def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
@@ -180,19 +174,20 @@
   "%.5f".format((end - start)/(i * 1.0e9))
 }
 
-//for (i <- 0 to 5000000 by 500000) {
-//  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
-//}
+for (i <- 0 to 5000000 by 500000) {
+  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
+}
 
 // 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 => SEQ(r, ONE | ONE)).drop(100).next()) == ONE)
 
 // the Iterator produces the rexp
 //
 //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
 //
-//    where SEQ is nested 50 times.
- 
+//    where SEQ is nested 100 times.
+*/ 
 
 
 }
+