--- a/solutions/cw1/matcher.sc Fri Sep 15 10:49:33 2023 +0100
+++ b/solutions/cw1/matcher.sc Sun Sep 17 19:12:57 2023 +0100
@@ -1,114 +1,17 @@
-// CW1
-abstract class 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
-case class STAR(r: Rexp) extends Rexp
-
-// extended Rexps
-case class RANGE(s: Set[Char]) extends Rexp
-case class PLUS(r: Rexp) extends Rexp
-case class OPTIONAL(r: Rexp) extends Rexp
-case class NTIMES(r: Rexp, n: Int) extends Rexp
-case class UPTO(r: Rexp, n: Int) extends Rexp
-case class FROM(r: Rexp, n: Int) extends Rexp
-case class BETWEEN(r: Rexp, n: Int, m: Int) extends Rexp
-case class NOT(r: Rexp) extends Rexp
-
-// functions
-case class CFUN(f: Char => Boolean) extends Rexp
-
-// abbreviations
-def FCHAR(c: Char) = CFUN((a: Char) => a == c)
-def FSET(s: Set[Char]) = CFUN((a: Char) => s.contains(a))
-val FALL = CFUN(_ => true)
-
-def nullable (r: Rexp) : Boolean = r match {
- 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)
- case STAR(_) => true
-
- case RANGE(_) => false
- case PLUS(r1) => nullable(r1)
- case OPTIONAL(_) => true
- case NTIMES(r1, i) => if (i == 0) true else nullable(r1)
- case UPTO(_, _) => true
- case FROM(r1, i) => if (i == 0) true else nullable(r1)
- case BETWEEN(r1, i, _) => if (i == 0) true else nullable(r1)
- case NOT(r1) => !nullable(r1)
-
- case CFUN(f) => false
-}
-
+println("===========================")
-def der (c: Char, r: Rexp) : Rexp = r match {
- 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(r1) => SEQ(der(c, r1), STAR(r1))
-
- case RANGE(s) =>
- if (s.contains(c)) ONE else ZERO
- case PLUS(r1) => SEQ(der(c, r1), STAR(r1))
- case OPTIONAL(r1) => der(c, r1)
- case NTIMES(r, i) =>
- if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
- case UPTO(r1, i) =>
- if (i == 0) ZERO else SEQ(der(c, r1), UPTO(r1, i - 1))
- case FROM(r1, i) =>
- if (i == 0) SEQ(der(c, r1), FROM(r1, 0)) else
- SEQ(der(c, r1), FROM(r1, i - 1))
- case BETWEEN(r1, i, j) =>
- if (i == 0) {
- if (j == 0) ZERO else SEQ(der(c, r1), BETWEEN(r1, 0, j - 1))
- } else SEQ(der(c, r1), BETWEEN(r1, i - 1, j - 1))
- case NOT(r1) => NOT(der(c, r1))
+//import $file.cw1
+//import cw1._
+import CW1._
- case CFUN(f) => if (f(c)) ONE else ZERO
-}
-
-
-def simp(r: Rexp) : Rexp = r match {
- case ALT(r1, r2) => (simp(r1), simp(r2)) match {
- case (ZERO, r2s) => r2s
- case (r1s, ZERO) => r1s
- case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
- }
- case SEQ(r1, r2) => (simp(r1), simp(r2)) match {
- case (ZERO, _) => ZERO
- case (_, ZERO) => ZERO
- case (ONE, r2s) => r2s
- case (r1s, ONE) => r1s
- case (r1s, r2s) => SEQ(r1s, r2s)
- }
- case r => r
-}
-
-def ders(s: List[Char], r: Rexp) : Rexp = s match {
- case Nil => r
- case c::s => ders(s, simp(der(c, r)))
-}
-
-def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
-
-
-
+/*
val Regex31 = NTIMES(CHAR('a'),3)
val Regex32 = NTIMES(OPTIONAL(CHAR('a')),3)
val Regex33 = UPTO(CHAR('a'),3)
val Regex34 = UPTO(OPTIONAL(CHAR('a')),3)
-val Regex35 = BETWEEN(CHAR('a'),3,5)
-val Regex36 = BETWEEN(OPTIONAL(CHAR('a')),3,5)
+val Regex35 = NMTIMES(CHAR('a'),3,5)
+val Regex36 = NMTIMES(OPTIONAL(CHAR('a')),3,5)
val string31 = ""
val string32 = "a"
val string33 = "aa"
@@ -323,3 +226,4 @@
matcher(PLUS(PLUS(Q7r1)), Q7str7)
matcher(PLUS(PLUS(Q7r2)), Q7str7)
+*/
\ No newline at end of file