--- a/progs/scala/re.scala Mon Sep 22 14:57:02 2014 +0100
+++ b/progs/scala/re.scala Thu Sep 25 14:41:06 2014 +0100
@@ -15,8 +15,8 @@
case object Void extends Val
case class Chr(c: Char) extends Val
case class Sequ(v1: Val, v2: Val) extends Val
-case class Left(n: Int, v: Val) extends Val
-case class Right(n: Int, v: Val) extends Val
+case class Left(v: Val) extends Val
+case class Right(v: Val) extends Val
case class Stars(vs: List[Val]) extends Val
case class Rec(x: String, v: Val) extends Val
@@ -89,8 +89,8 @@
def flatten(v: Val) : String = v match {
case Void => ""
case Chr(c) => c.toString
- case Left(n, v) => flatten(v)
- case Right(n, v) => flatten(v)
+ case Left(v) => flatten(v)
+ case Right(v) => flatten(v)
case Sequ(v1, v2) => flatten(v1) + flatten(v2)
case Stars(vs) => vs.map(flatten).mkString
case Rec(_, v) => flatten(v)
@@ -100,27 +100,18 @@
def env(v: Val) : List[(String, String)] = v match {
case Void => Nil
case Chr(c) => Nil
- case Left(n, v) => env(v)
- case Right(n, v) => env(v)
+ case Left(v) => env(v)
+ case Right(v) => env(v)
case Sequ(v1, v2) => env(v1) ::: env(v2)
case Stars(vs) => vs.flatMap(env)
case Rec(x, v) => (x, flatten(v))::env(v)
}
-def left_inc(v: Val) = v match {
- case Left(v, n) => Left(v, n + 1)
- case v => Left(v, 1)
-}
-
-def right_inc(v: Val) = v match {
- case Right(v, n) => Right(v, n + 1)
- case v => Right(v, 1)
-}
def mkeps(r: Rexp) : Val = r match {
case EMPTY => Void
case ALT(r1, r2) =>
- if (nullable(r1)) left_inc(mkeps(r1)) else right_inc(mkeps(r2))
+ if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
case STAR(r) => Stars(Nil)
case RECD(x, r) => Rec(x, mkeps(r))
@@ -131,8 +122,7 @@
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
- case (ALT(r1, r2), Left(v1), 1) => Left(inj(r1, c, v1), 1)
- case (ALT(r1, r2), Left(v1), n) => inc_left(inj(r1, c, Left(v1, n - 1)))
+ 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 (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
@@ -148,6 +138,12 @@
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
+val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).%
+env(lexing(r, "ba"))
+
+val r = "a" | "b"
+lexing(r,"a")
+
// Lexing Rules for a Small While Language
def PLUS(r: Rexp) = r ~ r.%