progs/app5.scala
changeset 261 24531cfaa36a
parent 93 4794759139ea
child 399 5c1fbb39c93e
equal deleted inserted replaced
260:65d1ea0e989f 261:24531cfaa36a
     4   case CHAR(_) => false
     4   case CHAR(_) => false
     5   case ALT(r1, r2) => nullable(r1) || nullable(r2)
     5   case ALT(r1, r2) => nullable(r1) || nullable(r2)
     6   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
     6   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
     7   case STAR(_) => true
     7   case STAR(_) => true
     8 }
     8 }
       
     9 
       
    10 def der (c: Char, r: Rexp) : Rexp = r match {
       
    11   case NULL => NULL
       
    12   case EMPTY => NULL
       
    13   case CHAR(d) => if (c == d) EMPTY else NULL
       
    14   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    15   case SEQ(r1, r2) => 
       
    16     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    17     else SEQ(der(c, r1), r2)
       
    18   case STAR(r) => SEQ(der(c, r), STAR(r))
       
    19 }
       
    20 
       
    21 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    22   case Nil => r
       
    23   case c::s => ders(s, der(c, r))
       
    24 }
       
    25 
       
    26 def matches(r: Rexp, s: String) : Boolean = 
       
    27   nullable(ders(s.toList, r))