--- a/progs/scala/re.scala Sat Jan 31 18:21:03 2015 +0000
+++ b/progs/scala/re.scala Mon Feb 09 12:13:10 2015 +0000
@@ -43,6 +43,27 @@
def $ (r: Rexp) = RECD(s, r)
}
+def pretty(r: Rexp) : String = r match {
+ case NULL => "0"
+ case EMPTY => "e"
+ case CHAR(c) => c.toString
+ case ALT(r1, r2) => "(" ++ pretty(r1) ++ " | " + pretty(r2) ++ ")"
+ case SEQ(r1, r2) => pretty(r1) ++ pretty(r2)
+ case STAR(r) => "(" ++ pretty(r) ++ ")*"
+ case RECD(x, r) => "(" ++ x ++ " : " ++ pretty(r) ++ ")"
+}
+
+def vpretty(v: Val) : String = v match {
+ case Void => "()"
+ case Chr(c) => c.toString
+ case Left(v) => "Left(" ++ vpretty(v) ++ ")"
+ case Right(v) => "Right(" ++ vpretty(v) ++ ")"
+ case Sequ(v1, v2) => vpretty(v1) ++ " ~ " ++ vpretty(v2)
+ case Stars(vs) => vs.flatMap(vpretty).mkString("[", ",", "]")
+ case Rec(x, v) => "(" ++ x ++ ":" ++ vpretty(v) ++ ")"
+}
+
+
// size of a regular expressions - for testing purposes
def size(r: Rexp) : Int = r match {
case NULL => 1
@@ -59,7 +80,8 @@
case NULL => Set()
case EMPTY => Set(Void)
case CHAR(c) => Set(Chr(c))
- case ALT(r1, r2) => values(r1) ++ values(r2)
+ case ALT(r1, r2) => (for (v1 <- values(r1)) yield Left(v1)) ++
+ (for (v2 <- values(r2)) yield Right(v2))
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)
@@ -271,6 +293,18 @@
println(values(r2).mkString("\n"))
println(values(r2).toList.map(flatten).mkString("\n"))
+//Some experiments
+//================
+
+val reg0 = ("" | "a") ~ ("ab" | "b")
+val reg1 = der('a', reg0)
+val reg2 = der('b', reg1)
+println(List(reg0, reg1, reg2).map(pretty).mkString("\n"))
+println(lexing(reg0, "ab"))
+
+val val0 = values(reg0)
+val val1 = values(reg1)
+val val2 = values(reg2)
// Two Simple Tests