--- a/progs/token.scala Tue Sep 20 12:40:57 2016 +0100
+++ b/progs/token.scala Tue Sep 20 12:47:46 2016 +0100
@@ -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
@@ -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))
}
@@ -46,8 +46,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)
@@ -57,9 +57,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))
@@ -98,7 +98,7 @@
// injection part
def mkeps(r: Rexp) : Val = r match {
- case EMPTY => Empty
+ case ONE => Empty
case ALT(r1, r2) =>
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
case SEQ(r1, r2) => Seq(mkeps(r1), mkeps(r2))
@@ -126,7 +126,7 @@
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
-lexing(("ab" | "ab") ~ ("b" | EMPTY), "ab")
+lexing(("ab" | "ab") ~ ("b" | ONE), "ab")
// some "rectification" functions for simplification
def F_ID(v: Val): Val = v
@@ -155,8 +155,8 @@
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 (ZERO, _) => (r2s, F_RIGHT(f2s))
+ case (_, ZERO) => (r1s, F_LEFT(f1s))
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
else (ALT (r1s, r2s), F_ALT(f1s, f2s))
}
@@ -165,10 +165,10 @@
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_Empty1(f1s, f2s))
- case (_, EMPTY) => (r1s, F_SEQ_Empty2(f1s, f2s))
+ case (ZERO, _) => (ZERO, F_ERROR)
+ case (_, ZERO) => (ZERO, F_ERROR)
+ case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
+ case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
}
}