progs/app6.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 12 Oct 2014 19:39:55 +0100
changeset 272 1446bc47a294
parent 262 ee4304bc6350
child 343 539b2e88f5b9
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) => {
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     3
    val r1s = simp(r1)
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     4
    val r2s = simp(r2)
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     5
    (r1s, r2s) match {
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     6
      case (NULL, _) => r2s
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     7
      case (_, NULL) => r1s
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     8
      case _ => if (r1s == r2s) r1s else ALT(r1s, r2s)
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     9
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    10
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    11
  case SEQ(r1, r2) => {
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    12
    val r1s = simp(r1)
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    13
    val r2s = simp(r2)
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    14
    (r1s, r2s) match {
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    15
      case (NULL, _) => NULL
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    16
      case (_, NULL) => NULL
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    17
      case (EMPTY, _) => r2s
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    18
      case (_, EMPTY) => r1s
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    19
      case _ => SEQ(r1s, r2s)
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    20
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    21
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    22
  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
    23
  case r => r
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    26
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
    27
  case Nil => r
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    28
  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
    29
}
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    30