--- a/progs/re1.scala Tue Sep 20 12:25:09 2016 +0100
+++ b/progs/re1.scala Tue Sep 20 12:38:26 2016 +0100
@@ -1,6 +1,6 @@
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
@@ -9,8 +9,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)
@@ -19,9 +19,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))
@@ -38,18 +38,18 @@
// main matcher function
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
-//example from the homework
+//examples from the homework
val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
der('a', r)
der('b', r)
der('c', r)
//optional: one or zero times
-def OPT(r: Rexp) = ALT(r, EMPTY)
+def OPT(r: Rexp) = ALT(r, ONE)
//n-times
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
- case 0 => EMPTY
+ case 0 => ONE
case 1 => r
case n => SEQ(r, NTIMES(r, n - 1))
}
--- a/progs/re2.scala Tue Sep 20 12:25:09 2016 +0100
+++ b/progs/re2.scala Tue Sep 20 12:38:26 2016 +0100
@@ -1,8 +1,8 @@
// version with explicit n-times regular expression
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
@@ -10,8 +10,8 @@
case class NTIMES(r: Rexp, n: Int) extends Rexp //explicit constructor
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)
@@ -20,16 +20,16 @@
}
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))
else SEQ(der(c, r1), r2)
case STAR(r) => SEQ(der(c, r), STAR(r))
case NTIMES(r, i) =>
- if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
+ if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
}
def ders (s: List[Char], r: Rexp) : Rexp = s match {
@@ -41,7 +41,7 @@
//optional: one or zero times
-def OPT(r: Rexp) = ALT(r, EMPTY)
+def OPT(r: Rexp) = ALT(r, ONE)
//evil regular expressions
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))