progs/matcher/re2.sc
changeset 825 dca072e2bb7d
parent 769 f9686b22db7e
child 826 b0352633bf48
equal deleted inserted replaced
824:284ac979f289 825:dca072e2bb7d
    30   case STAR(_) => true
    30   case STAR(_) => true
    31   case NTIMES(r, n) => if (n == 0) true else nullable(r)
    31   case NTIMES(r, n) => if (n == 0) true else nullable(r)
    32 }
    32 }
    33 
    33 
    34 
    34 
    35 def der (c: Char, r: Rexp) : Rexp = r match {
    35 def der(c: Char, r: Rexp) : Rexp = r match {
    36   case ZERO => ZERO
    36   case ZERO => ZERO
    37   case ONE => ZERO
    37   case ONE => ZERO
    38   case CHAR(d) => if (c == d) ONE else ZERO
    38   case CHAR(d) => if (c == d) ONE else ZERO
    39   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    39   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    40   case SEQ(r1, r2) => 
    40   case SEQ(r1, r2) => 
    43   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
    43   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
    44   case NTIMES(r, n) => 
    44   case NTIMES(r, n) => 
    45     if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
    45     if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
    46 }
    46 }
    47 
    47 
    48 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    48 def ders(s: List[Char], r: Rexp) : Rexp = s match {
    49   case Nil => r
    49   case Nil => r
    50   case c::s => ders(s, der(c, r))
    50   case c::s => ders(s, der(c, r))
    51 }
    51 }
    52 
    52 
    53 def matcher(r: Rexp, s: String) : Boolean = 
    53 def matcher(r: Rexp, s: String) : Boolean =