diff -r 04346d82fe01 -r 0b63c0edfb09 progs/cw1.scala --- a/progs/cw1.scala Wed Nov 13 20:07:26 2013 +0000 +++ b/progs/cw1.scala Thu Nov 14 20:08:39 2013 +0000 @@ -57,16 +57,20 @@ case r => NTIMES(r, n) } } -case class NMTIMES(r: Rexp, n: Int, m: Int) extends Rexp { - if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.") - +case class NUPTOM(r: Rexp, n: Int, m: Int) extends Rexp { override def simp = if (m == 0) EMPTY else r.simp match { case NULL => NULL case EMPTY => EMPTY - case r => NMTIMES(r, n, m) + case r => NUPTOM(r, n, m) } } +def NMTIMES(r: Rexp, n: Int, m: Int) = { + if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.") + else NUPTOM(r, n, m - n) +} + + case class NOT(r: Rexp) extends Rexp { override def simp = NOT(r.simp) } @@ -85,7 +89,7 @@ case STAR(_) => true case PLUS(r) => nullable(r) case NTIMES(r, i) => if (i == 0) true else nullable(r) - case NMTIMES(r, i, j) => if (i == 0) true else nullable(r) + case NUPTOM(r, i, j) => if (i == 0) true else nullable(r) case RANGE(_) => false case NOT(r) => !(nullable(r)) case OPT(_) => true @@ -101,13 +105,13 @@ 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 PLUS(r) => der(c, SEQ(r, STAR(r))) + case PLUS(r) => SEQ(der(c, r), STAR(r)) case NTIMES(r, i) => if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1))) - case NMTIMES(r, i, j) => - if (j == 0) NULL - if (i == 0) der(c, NTIMES(r, j)) - else der(c, SEQ(r, NMTIMES(r, i - 1, j - 1))) + case NUPTOM(r, i, j) => + if (i == 0 && j == 0) NULL else + if (i == 0) ALT(der(c, NTIMES(r, j)), der(c, NUPTOM(r, 0, j - 1))) + else der(c, SEQ(r, NUPTOM(r, i - 1, j))) case RANGE(cs) => if (cs contains c) EMPTY else NULL case NOT(r) => NOT(der (c, r)) case OPT(r) => der(c, r) @@ -188,3 +192,22 @@ println(matcher(EVIL2, "aaa" * 40)) println(matcher(EVIL2, "aaa" * 43 + "aa")) println(matcher(EVIL2, "aaa" * 45 + "a")) + +println("TEST") +val test = NMTIMES(RANGE(LOWERCASE + "."), 2, 6) + +println(matcher(test,"ab")) +println(matcher(test,"abcdef")) +println(matcher(test,"abc")) +println(matcher(test,"....")) +println(matcher(test,"asdfg")) +println(matcher(test,"abcdefg")) + +println(test) +println(ders_simp("a".toList, test)) +println(ders_simp("aa".toList, test)) +println(ders_simp("aaa".toList, test)) +println(ders_simp("aaaa".toList, test)) +println(ders_simp("aaaaa".toList, test)) +println(ders_simp("aaaaaa".toList, test)) +println(ders_simp("aaaaaaa".toList, test))