--- a/progs/token.scala Fri Oct 16 08:56:25 2015 +0100
+++ b/progs/token.scala Fri Oct 16 14:27:20 2015 +0100
@@ -12,9 +12,9 @@
case class RECD(x: String, r: Rexp) extends Rexp
abstract class Val
-case object Void extends Val
+case object Empty extends Val
case class Chr(c: Char) extends Val
-case class Sequ(v1: Val, v2: Val) extends Val
+case class Seq(v1: Val, v2: 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
@@ -76,45 +76,45 @@
// extracts a string from value
def flatten(v: Val) : String = v match {
- case Void => ""
+ case Empty => ""
case Chr(c) => c.toString
case Left(v) => flatten(v)
case Right(v) => flatten(v)
- case Sequ(v1, v2) => flatten(v1) + flatten(v2)
+ case Seq(v1, v2) => flatten(v1) + flatten(v2)
case Stars(vs) => vs.map(flatten).mkString
case Rec(_, v) => flatten(v)
}
// extracts an environment from a value
def env(v: Val) : List[(String, String)] = v match {
- case Void => Nil
+ case Empty => Nil
case Chr(c) => Nil
case Left(v) => env(v)
case Right(v) => env(v)
- case Sequ(v1, v2) => env(v1) ::: env(v2)
+ case Seq(v1, v2) => env(v1) ::: env(v2)
case Stars(vs) => vs.flatMap(env)
case Rec(x, v) => (x, flatten(v))::env(v)
}
// injection part
def mkeps(r: Rexp) : Val = r match {
- case EMPTY => Void
+ case EMPTY => Empty
case ALT(r1, r2) =>
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
- case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
+ case SEQ(r1, r2) => Seq(mkeps(r1), mkeps(r2))
case STAR(r) => Stars(Nil)
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)
- 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 (STAR(r), Seq(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
+ case (SEQ(r1, r2), Seq(v1, v2)) => Seq(inj(r1, c, v1), v2)
+ case (SEQ(r1, r2), Left(Seq(v1, v2))) => Seq(inj(r1, c, v1), v2)
+ case (SEQ(r1, r2), Right(v2)) => Seq(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(c)
+ case (CHAR(d), Empty) => Chr(c)
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
}
@@ -126,6 +126,8 @@
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
+lexing(("ab" | "ab") ~ ("b" | EMPTY), "ab")
+
// some "rectification" functions for simplification
def F_ID(v: Val): Val = v
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
@@ -135,10 +137,12 @@
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))
+ case Seq(v1, v2) => Seq(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_SEQ_Empty1(f1: Val => Val, f2: Val => Val) =
+ (v:Val) => Seq(f1(Empty), f2(v))
+def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) =
+ (v:Val) => Seq(f1(v), f2(Empty))
def F_RECD(f: Val => Val) = (v:Val) => v match {
case Rec(x, v) => Rec(x, f(v))
}
@@ -163,8 +167,8 @@
(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 (EMPTY, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
+ case (_, EMPTY) => (r1s, F_SEQ_Empty2(f1s, f2s))
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
}
}
@@ -185,7 +189,7 @@
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
-lexing_simp(("a" + "ab") | ("b" + ""), "ab")
+lexing_simp(("a" | "ab") ~ ("b" | ""), "ab")
// Lexing Rules for a Small While Language