--- a/progs/app6.scala Sun Sep 28 18:07:58 2014 +0100
+++ b/progs/app6.scala Sun Sep 28 22:30:25 2014 +0100
@@ -1,16 +1,30 @@
-def der (r: Rexp, c: Char) : Rexp = r match {
- case NULL => NULL
- case EMPTY => NULL
- case CHAR(d) => if (c == d) EMPTY else NULL
- case ALT(r1, r2) => ALT(der(r1, c), der(r2, c))
- case SEQ(r1, r2) =>
- if (nullable(r1)) ALT(SEQ(der(r1, c), r2), der(r2, c))
- else SEQ(der(r1, c), r2)
- case STAR(r) => SEQ(der(r, c), STAR(r))
+def simp(r: Rexp): Rexp = r match {
+ case ALT(r1, r2) => {
+ val r1s = simp(r1)
+ val r2s = simp(r2)
+ (r1s, r2s) match {
+ case (NULL, _) => r2s
+ case (_, NULL) => r1s
+ case _ => if (r1s == r2s) r1s else ALT(r1s, r2s)
+ }
+ }
+ case SEQ(r1, r2) => {
+ val r1s = simp(r1)
+ val r2s = simp(r2)
+ (r1s, r2s) match {
+ case (NULL, _) => NULL
+ case (_, NULL) => NULL
+ case (EMPTY, _) => r2s
+ case (_, EMPTY) => r1s
+ case _ => SEQ(r1s, r2s)
+ }
+ }
+ case NTIMES(r, n) => NTIMES(simp(r), n)
+ case r => r
}
def ders (s: List[Char], r: Rexp) : Rexp = s match {
case Nil => r
- case c::s => ders(s, der(c, r))
+ case c::s => ders(s, simp(der(c, r)))
}