progs/app6.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 22 Nov 2013 16:56:51 +0000
changeset 198 f54972b0f641
parent 117 25999de692b2
child 262 ee4304bc6350
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     1
def der (r: Rexp, c: Char) : Rexp = r match {
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
  case NULL => NULL
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
  case EMPTY => NULL
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
  case CHAR(d) => if (c == d) EMPTY else NULL
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     5
  case ALT(r1, r2) => ALT(der(r1, c), der(r2, c))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  case SEQ(r1, r2) => 
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     7
    if (nullable(r1)) ALT(SEQ(der(r1, c), r2), der(r2, c))
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     8
    else SEQ(der(r1, c), r2)
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     9
  case STAR(r) => SEQ(der(r, c), STAR(r))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    12
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
    13
  case Nil => r
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    14
  case c::s => ders(s, der(c, r))
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    15
}
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    16