diff -r c9027db225cc -r 6a43ea9305ba progs/scala/re2.scala --- a/progs/scala/re2.scala Fri Mar 18 15:03:54 2016 +0000 +++ b/progs/scala/re2.scala Sat Mar 19 23:27:29 2016 +0000 @@ -3,8 +3,8 @@ import scala.annotation.tailrec abstract class Rexp -case object NULL extends Rexp -case object EMPTY extends Rexp +case object ZERO extends Rexp +case object ONE extends Rexp case class CHAR(c: Char) extends Rexp case class ALT(r1: Rexp, r2: Rexp) extends Rexp case class SEQ(r1: Rexp, r2: Rexp) extends Rexp @@ -12,7 +12,7 @@ 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 Left(v: Val) extends Val @@ -22,7 +22,7 @@ // some convenience for typing in regular expressions def charlist2rexp(s : List[Char]): Rexp = s match { - case Nil => EMPTY + case Nil => ONE case c::Nil => CHAR(c) case c::s => SEQ(CHAR(c), charlist2rexp(s)) } @@ -45,8 +45,8 @@ // size of a regular expressions - for testing purposes def size(r: Rexp) : Int = r match { - case NULL => 1 - case EMPTY => 1 + case ZERO => 1 + case ONE => 1 case CHAR(_) => 1 case ALT(r1, r2) => 1 + size(r1) + size(r2) case SEQ(r1, r2) => 1 + size(r1) + size(r2) @@ -58,8 +58,8 @@ // nullable function: tests whether the regular // expression can recognise the empty string def nullable (r: Rexp) : Boolean = r match { - case NULL => false - case EMPTY => true + case ZERO => false + case ONE => true case CHAR(_) => false case ALT(r1, r2) => nullable(r1) || nullable(r2) case SEQ(r1, r2) => nullable(r1) && nullable(r2) @@ -69,9 +69,9 @@ // derivative of a regular expression w.r.t. a character def der (c: Char, r: Rexp) : Rexp = r match { - case NULL => NULL - case EMPTY => NULL - case CHAR(d) => if (c == d) EMPTY else NULL + case ZERO => ZERO + case ONE => ZERO + case CHAR(d) => if (c == d) ONE else ZERO case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) case SEQ(r1, r2) => if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) @@ -88,7 +88,7 @@ // 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) @@ -99,7 +99,7 @@ // 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) @@ -108,19 +108,6 @@ case Rec(x, v) => (x, flatten(v))::env(v) } -def mkeps_all(r: Rexp) : Set[Val] = r match { - case EMPTY => Set(Void) - case ALT(r1, r2) => (nullable(r1), nullable(r2)) match { - case (true, true) => mkeps_all(r1).map(Left) ++ mkeps_all(r2).map(Right) - case (true, false) => mkeps_all(r1).map(Left) - case (false, true) => mkeps_all(r2).map(Right) - } - case SEQ(r1, r2) => for (v1 <- mkeps_all(r1); - v2 <- mkeps_all(r2)) yield Sequ(v1, v2) - case STAR(r) => Set(Stars(Nil), Stars(List(mkeps(r)))) - case RECD(x, r) => for (v <- mkeps_all(r)) yield Rec(x, v) -} - 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) @@ -128,12 +115,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(c) + case (CHAR(d), Empty) => Chr(c) case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) } -def inj_all(r: Rexp, c: Char, vs: Set[Val]) : Set[Val] = - for (v <- vs) yield inj(r, c, v) // main lexing function (produces a value) def lex(r: Rexp, s: List[Char]) : Val = s match {