lex_blex_Frankensteined.scala
changeset 92 aaa2f2b52baf
parent 59 8ff7b7508824
child 93 d486c12deeab
--- a/lex_blex_Frankensteined.scala	Mon Nov 11 17:37:24 2019 +0000
+++ b/lex_blex_Frankensteined.scala	Wed Nov 27 14:15:00 2019 +0000
@@ -185,6 +185,43 @@
     //case PLUS(r) => SEQ(der(c, r), STAR(r))
   }
 
+  def ders (s: List[Char], r: Rexp) : Rexp = s match {
+    case Nil => r
+    case c::s => ders(s, der(c, r))
+  }
+
+def der_seqo(r:Rexp, s: List[Char],acc: List[Rexp]) : List[Rexp] = s match{
+    case Nil => acc ::: List(r)
+    case c::cs => der_seqo(der(c, r), cs, acc ::: List(r))
+  }
+  def der_seq_revo(r:Rexp, s: List[Char], acc: List[Rexp]): List[Rexp] = s match{
+    case Nil => r::acc
+    case c::cs =>der_seq_revo(r, cs, ders(s, r) :: acc  )
+  }
+  def re_closeo(l1: List[Rexp], l2: List[Rexp], re_init: Rexp): Rexp = l1 match {
+    case Nil => re_init
+    case c::cs => if(nullable(c)) re_closeo(cs, l2.tail, ALT(re_init,  l2.head)  ) 
+    else re_closeo(cs, l2.tail, re_init)
+  }
+  //HERE
+  def closed_string_dero(r1: Rexp, r2: Rexp, s: List[Char]): Rexp = {
+    val l1 = der_seqo(r1, s, Nil)
+    val l2 = der_seq_revo(r2, s, Nil)
+    val Re = re_closeo((l1.reverse).tail, l2.tail, SEQ(l1.last, l2.head))
+    Re
+  }
+  //derivative w.r.t string
+def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match {
+  case (Nil, r) => r
+  case (s, ZERO) => ZERO
+  case (s, ONE) => if (s == Nil) ONE else ZERO
+  case (s, CHAR(c)) => if (s == List(c)) ONE else 
+                       if (s == Nil) CHAR(c) else ZERO
+  case (s, ALTS(List(r1, r2))) => ALT(ders2(s, r1), ders2(s, r2))
+  case (s, SEQ(r1, r2)) => closed_string_dero(r1, r2, s)
+  case (c::cs, STAR(r)) => closed_string_dero(der(c, r), STAR(r), cs)
+}
+
   def flatten(v: Val) : String = v match {
     case Empty => ""
     case Chr(c) => c.toString
@@ -365,12 +402,6 @@
     case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
   }
 
-
-  def ders (s: List[Char], r: Rexp) : Rexp = s match {
-    case Nil => r
-    case c::s => ders(s, der(c, r))
-  }
-
   // derivative w.r.t. a string (iterates bder)
   @tailrec
   def bders (s: List[Char], r: ARexp) : ARexp = s match {