progs/scala/re.scala
changeset 48 652861c9473f
parent 41 4f8a9ed0f26d
child 49 c616ec6b1e3c
--- a/progs/scala/re.scala	Fri Dec 26 21:17:55 2014 +0000
+++ b/progs/scala/re.scala	Mon Jan 19 09:55:58 2015 +0000
@@ -54,6 +54,17 @@
   case RECD(_, r) => 1 + size(r)
 }
 
+// extracts a set of candidate values from a "non-starred" regular expression
+def values(r: Rexp) : Set[Val] = r match {
+  case NULL => Set()
+  case EMPTY => Set(Void)
+  case CHAR(c) => Set(Chr(c))
+  case ALT(r1, r2) => values(r1) ++ values(r2)
+  case SEQ(r1, r2) => for (v1 <- values(r1); v2 <- values(r2)) yield Sequ(v1, v2)
+  case STAR(r) => Set(Void) ++ values(r)   // to do more would cause the set to be infinite
+  case RECD(_, r) => values(r)
+}
+
 
 // nullable function: tests whether the regular 
 // expression can recognise the empty string
@@ -108,6 +119,7 @@
   case Rec(x, v) => (x, flatten(v))::env(v)
 }
 
+// injection part
 def mkeps(r: Rexp) : Val = r match {
   case EMPTY => Void
   case ALT(r1, r2) => 
@@ -249,6 +261,11 @@
   result
 }
 
+val r = ("a" | "ab") ~ ("bcd" | "c")
+println(lexing(r, "abcd"))
+println(values(r).mkString("\n"))
+println(values(r).map(flatten).mkString("\n"))
+
 // Two Simple Tests
 //===================
 println("prog0 test")