progs/re4.scala
changeset 550 71fc4a7a7039
parent 546 6589afc6789b
child 623 47a299e7010f
--- a/progs/re4.scala	Sat May 05 10:31:00 2018 +0100
+++ b/progs/re4.scala	Fri Jun 01 15:28:37 2018 +0100
@@ -112,3 +112,50 @@
 
 
 
+// size of a regular expressions - for testing purposes 
+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(r) => 1 + size(r)
+  case NTIMES(r, _) => 1 + size(r)
+}
+
+
+// string of a regular expressions - for testing purposes 
+def string(r: Rexp) : String = r match {
+  case ZERO => "0"
+  case ONE => "1"
+  case CHAR(c) => c.toString 
+  case ALT(r1, r2) => s"(${string(r1)} + ${string(r2)})"
+  case SEQ(CHAR(c), CHAR(d)) => s"${c}${d}"
+  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
+  case STAR(r) => s"(${string(r)})*"
+  case NTIMES(r, n) =>  s"(${string(r)}){${n}}"
+}
+
+
+// test: ("a" | "aa")*
+val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
+
+//test: ("" | "a" | "aa")*
+val EVIL3 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
+
+val t1  = ders2("a".toList, EVIL3)
+val t1s = simp(t1) 
+val t2  = ders2("aa".toList, t1s)
+val t2s = simp(t2)
+val t3  = ders2("aaa".toList, t2s)
+val t3s = simp(t3)
+val t4  = ders2("aaaa".toList, t3s)
+val t4s = simp(t4)
+println(string(t1) + "    " + size(t1))
+println("s " + string(t1s) + "    " + size(t1s))
+println(string(t2) + "    " + size(t2))
+println("s " + string(t2s) + "    " + size(t2s))
+println(string(t3) + "    " + size(t3))
+println("s " + string(t3s) + "    " + size(t3s))
+println(string(t4) + "    " + size(t4))
+println("s " + string(t4s) + "    " + size(t4s))