progs/app6.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 16 Oct 2015 14:27:20 +0100
changeset 354 86b2aeae3e98
parent 343 539b2e88f5b9
child 399 5c1fbb39c93e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     1
def simp(r: Rexp): Rexp = r match {
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     2
  case ALT(r1, r2) => {
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
     3
    (simp(r1), simp(r2)) match {
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
     4
      case (NULL, r2s) => r2s
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
     5
      case (r1s, NULL) => r1s
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
     6
      case (r1s, r2s) => if (r1s == r2s) r1s else ALT(r1s, r2s)
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     7
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     8
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     9
  case SEQ(r1, r2) => {
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    10
    (simp(r1), simp(r2)) match {
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    11
      case (NULL, _) => NULL
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    12
      case (_, NULL) => NULL
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    13
      case (EMPTY, r2s) => r2s
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    14
      case (r1s, EMPTY) => r1s
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    15
      case (r1s, r2s) => SEQ(r1s, r2s)
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    16
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    17
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    18
  case NTIMES(r, n) => NTIMES(simp(r), n)    
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    19
  case r => r
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    22
def ders (s: List[Char], r: Rexp) : Rexp = s match {
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    23
  case Nil => r
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    24
  case c::s => ders(s, simp(der(c, r)))
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    25
}
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    26