scala/app6.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 15 Jun 2013 09:11:11 -0400
changeset 92 e85600529ca5
parent 7 app6.scala@73cf4406b773
permissions -rw-r--r--
moved scala files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
def deriv (r: Rexp, c: Char) : Rexp = r match {
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
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
  case ALT(r1, r2) => ALT(deriv(r1, c), deriv(r2, c))
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  case SEQ(r1, r2) => 
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
    if (nullable(r1)) ALT(SEQ(deriv(r1, c), r2), deriv(r2, c))
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
    else SEQ(deriv(r1, c), r2)
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  case STAR(r) => SEQ(deriv(r, c), STAR(r))
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11