progs/re2.scala
changeset 424 1129024b26d5
parent 422 5deefcc8cffa
child 434 8664ff87cd77
equal deleted inserted replaced
423:e3acf2bf3895 424:1129024b26d5
     1 // version with explicit n-times regular expression
     1 // version with explicit n-times regular expression
     2 
     2 
     3 abstract class Rexp 
     3 abstract class Rexp 
     4 case object NULL extends Rexp
     4 case object ZERO extends Rexp
     5 case object EMPTY extends Rexp
     5 case object ONE extends Rexp
     6 case class CHAR(c: Char) extends Rexp
     6 case class CHAR(c: Char) extends Rexp
     7 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
     7 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
     8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
     8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
     9 case class STAR(r: Rexp) extends Rexp 
     9 case class STAR(r: Rexp) extends Rexp 
    10 case class NTIMES(r: Rexp, n: Int) extends Rexp //explicit constructor
    10 case class NTIMES(r: Rexp, n: Int) extends Rexp //explicit constructor
    11 
    11 
    12 def nullable (r: Rexp) : Boolean = r match {
    12 def nullable (r: Rexp) : Boolean = r match {
    13   case NULL => false
    13   case ZERO => false
    14   case EMPTY => true
    14   case ONE => true
    15   case CHAR(_) => false
    15   case CHAR(_) => false
    16   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    16   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    17   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
    17   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
    18   case STAR(_) => true
    18   case STAR(_) => true
    19   case NTIMES(r, i) => if (i == 0) true else nullable(r)
    19   case NTIMES(r, i) => if (i == 0) true else nullable(r)
    20 }
    20 }
    21 
    21 
    22 def der (c: Char, r: Rexp) : Rexp = r match {
    22 def der (c: Char, r: Rexp) : Rexp = r match {
    23   case NULL => NULL
    23   case ZERO => ZERO
    24   case EMPTY => NULL
    24   case ONE => ZERO
    25   case CHAR(d) => if (c == d) EMPTY else NULL
    25   case CHAR(d) => if (c == d) ONE else ZERO
    26   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    26   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    27   case SEQ(r1, r2) => 
    27   case SEQ(r1, r2) => 
    28     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    28     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    29     else SEQ(der(c, r1), r2)
    29     else SEQ(der(c, r1), r2)
    30   case STAR(r) => SEQ(der(c, r), STAR(r))
    30   case STAR(r) => SEQ(der(c, r), STAR(r))
    31   case NTIMES(r, i) => 
    31   case NTIMES(r, i) => 
    32     if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
    32     if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
    33 }
    33 }
    34 
    34 
    35 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    35 def ders (s: List[Char], r: Rexp) : Rexp = s match {
    36   case Nil => r
    36   case Nil => r
    37   case c::s => ders(s, der(c, r))
    37   case c::s => ders(s, der(c, r))
    39 
    39 
    40 def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
    40 def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
    41 
    41 
    42 
    42 
    43 //optional: one or zero times
    43 //optional: one or zero times
    44 def OPT(r: Rexp) = ALT(r, EMPTY)
    44 def OPT(r: Rexp) = ALT(r, ONE)
    45 
    45 
    46 //evil regular expressions
    46 //evil regular expressions
    47 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
    47 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
    48 val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b'))
    48 val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b'))
    49 
    49