--- a/progs/scala/re.scala Sun Oct 26 16:42:28 2014 +0000
+++ b/progs/scala/re.scala Tue Oct 28 15:36:14 2014 +0000
@@ -54,6 +54,7 @@
case RECD(_, r) => 1 + size(r)
}
+
// nullable function: tests whether the regular
// expression can recognise the empty string
def nullable (r: Rexp) : Boolean = r match {
@@ -107,7 +108,6 @@
case Rec(x, v) => (x, flatten(v))::env(v)
}
-
def mkeps(r: Rexp) : Val = r match {
case EMPTY => Void
case ALT(r1, r2) =>
@@ -117,6 +117,7 @@
case RECD(x, r) => Rec(x, mkeps(r))
}
+
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
@@ -124,11 +125,10 @@
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
- case (CHAR(d), Void) => Chr(d)
+ case (CHAR(d), Void) => Chr(c)
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
}
-
// main lexing function (produces a value)
def lex(r: Rexp, s: List[Char]) : Val = s match {
case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
@@ -138,11 +138,66 @@
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
-val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).%
-env(lexing(r, "ba"))
+
+// some "rectification" functions for simplification
+def F_ID(v: Val): Val = v
+def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
+def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
+def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
+ case Right(v) => Right(f2(v))
+ case Left(v) => Left(f1(v))
+}
+def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
+ case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
+}
+def F_SEQ_Void1(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(Void), f2(v))
+def F_SEQ_Void2(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(v), f2(Void))
+def F_RECD(f: Val => Val) = (v:Val) => v match {
+ case Rec(x, v) => Rec(x, f(v))
+}
+def F_ERROR(v: Val): Val = throw new Exception("error")
-val r = "a" | "b"
-lexing(r,"a")
+// simplification of regular expressions returning also an
+// rectification function; no simplification under STAR
+def simp(r: Rexp): (Rexp, Val => Val) = r match {
+ case ALT(r1, r2) => {
+ val (r1s, f1s) = simp(r1)
+ val (r2s, f2s) = simp(r2)
+ (r1s, r2s) match {
+ case (NULL, _) => (r2s, F_RIGHT(f2s))
+ case (_, NULL) => (r1s, F_LEFT(f1s))
+ case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
+ else (ALT (r1s, r2s), F_ALT(f1s, f2s))
+ }
+ }
+ case SEQ(r1, r2) => {
+ val (r1s, f1s) = simp(r1)
+ val (r2s, f2s) = simp(r2)
+ (r1s, r2s) match {
+ case (NULL, _) => (NULL, F_ERROR)
+ case (_, NULL) => (NULL, F_ERROR)
+ case (EMPTY, _) => (r2s, F_SEQ_Void1(f1s, f2s))
+ case (_, EMPTY) => (r1s, F_SEQ_Void2(f1s, f2s))
+ case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
+ }
+ }
+ case RECD(x, r1) => {
+ val (r1s, f1s) = simp(r1)
+ (RECD(x, r1s), F_RECD(f1s))
+ }
+ case r => (r, F_ID)
+}
+
+def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
+ case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
+ case c::cs => {
+ val (r_simp, f_simp) = simp(der(c, r))
+ inj(r, c, f_simp(lex_simp(r_simp, cs)))
+ }
+}
+
+def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
+
// Lexing Rules for a Small While Language
@@ -193,12 +248,12 @@
}
val prog0 = """read n"""
-env (lexing_simp(WHILE_REGS, prog0))
+env (lexing(WHILE_REGS, prog0))
println("Next test")
/*
val prog1 = """read n; write (n)"""
-env (lexing_simp(WHILE_REGS, prog1))
+env (lexing(WHILE_REGS, prog1))
val prog2 = """
i := 2;
@@ -222,6 +277,6 @@
for (i <- 1 to 100 by 10) {
print(i.toString + ": ")
- time(lexing_simp(WHILE_REGS, prog2 * i))
+ time(lexing(WHILE_REGS, prog2 * i))
}
*/