regexp3.scala
changeset 39 e5fb17c02508
parent 29 774007c4b1b3
equal deleted inserted replaced
38:cad34315db1b 39:e5fb17c02508
    46 
    46 
    47 
    47 
    48 // derivative of a regular expression w.r.t. a character
    48 // derivative of a regular expression w.r.t. a character
    49 def der (c: Char, r: Rexp) : Rexp = r match {
    49 def der (c: Char, r: Rexp) : Rexp = r match {
    50   case NULL => NULL
    50   case NULL => NULL
    51   case EMPTY => NULL
    51   case EMPTY => NULL  case CHAR(d) => if (c == d) EMPTY else NULL
    52   case CHAR(d) => if (c == d) EMPTY else NULL
       
    53   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    52   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    54   case SEQ(r1, r2) => 
    53   case SEQ(r1, r2) => 
    55     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    54     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    56     else SEQ(der(c, r1), r2)
    55     else SEQ(der(c, r1), r2)
    57   case STAR(r) => SEQ(der(c, r), STAR(r))
    56   case STAR(r) => SEQ(der(c, r), STAR(r))