main_solution3/re.scala
changeset 396 3ffe978a5664
parent 347 4de31fdc0d67
child 400 e48ea8300b2d
equal deleted inserted replaced
395:017f621f5835 396:3ffe978a5664
     1 // Core Part about Regular Expression Matching
     1 // Core Part about Regular Expression Matching
     2 //=============================================
     2 //=============================================
     3 
     3 
     4 object CW8c {
     4 object M3 {
     5 
     5 
     6 // Regular Expressions
     6 // Regular Expressions
     7 abstract class Rexp
     7 abstract class Rexp
     8 case object ZERO extends Rexp
     8 case object ZERO extends Rexp
     9 case object ONE extends Rexp
     9 case object ONE extends Rexp
    10 case class CHAR(c: Char) extends Rexp
    10 case class CHAR(c: Char) extends Rexp
    11 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
    11 case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
    13 case class STAR(r: Rexp) extends Rexp 
    13 case class STAR(r: Rexp) extends Rexp             // star
    14 
    14 
    15 // some convenience for typing in regular expressions
    15 // some convenience for typing in regular expressions
       
    16 
       
    17 
       
    18 //the usual binary choice can be defined in terms of ALTs
       
    19 def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
       
    20 
    16 
    21 
    17 import scala.language.implicitConversions    
    22 import scala.language.implicitConversions    
    18 import scala.language.reflectiveCalls 
    23 import scala.language.reflectiveCalls 
    19 
    24 
    20 
    25 
    47 
    52 
    48 def nullable (r: Rexp) : Boolean = r match {
    53 def nullable (r: Rexp) : Boolean = r match {
    49   case ZERO => false
    54   case ZERO => false
    50   case ONE => true
    55   case ONE => true
    51   case CHAR(_) => false
    56   case CHAR(_) => false
    52   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    57   case ALTs(rs) => rs.exists(nullable)
    53   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
    58   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
    54   case STAR(_) => true
    59   case STAR(_) => true
    55 }
    60 }
    56 
    61 
    57 // (2) Complete the function der according to
    62 // (2) Complete the function der according to
    61 
    66 
    62 def der (c: Char, r: Rexp) : Rexp = r match {
    67 def der (c: Char, r: Rexp) : Rexp = r match {
    63   case ZERO => ZERO
    68   case ZERO => ZERO
    64   case ONE => ZERO
    69   case ONE => ZERO
    65   case CHAR(d) => if (c == d) ONE else ZERO
    70   case CHAR(d) => if (c == d) ONE else ZERO
    66   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
    71   case ALTs(rs) => ALTs(rs.map(der(c, _)))
    67   case SEQ(r1, r2) => 
    72   case SEQ(r1, r2) => 
    68     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    73     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
    69     else SEQ(der(c, r1), r2)
    74     else SEQ(der(c, r1), r2)
    70   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
    75   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
       
    76 }
       
    77 
       
    78 
       
    79 
       
    80 def flts(rs: List[Rexp]) : List[Rexp] = rs match {
       
    81   case Nil => Nil
       
    82   case ZERO::tl => flts(tl)
       
    83   case ALTs(rs1)::rs2 => rs1 ::: flts(rs2)  
       
    84   case r::rs => r :: flts(rs) 
    71 }
    85 }
    72 
    86 
    73 // (3) Complete the simp function according to
    87 // (3) Complete the simp function according to
    74 // the specification given in the coursework; this
    88 // the specification given in the coursework; this
    75 // function simplifies a regular expression from
    89 // function simplifies a regular expression from
    76 // the inside out, like you would simplify arithmetic 
    90 // the inside out, like you would simplify arithmetic 
    77 // expressions; however it does not simplify inside 
    91 // expressions; however it does not simplify inside 
    78 // STAR-regular expressions.
    92 // STAR-regular expressions.
    79 
    93 
       
    94 
    80 def simp(r: Rexp) : Rexp = r match {
    95 def simp(r: Rexp) : Rexp = r match {
    81   case ALT(r1, r2) => (simp(r1), simp(r2)) match {
    96   case ALTs(rs) => (flts(rs.map(simp)).distinct) match {
    82     case (ZERO, r2s) => r2s
    97     case Nil => ZERO
    83     case (r1s, ZERO) => r1s
    98     case r::Nil => r  
    84     case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
    99     case rs => ALTs(rs)
    85   }
   100   }
    86   case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
   101   case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
    87     case (ZERO, _) => ZERO
   102     case (ZERO, _) => ZERO
    88     case (_, ZERO) => ZERO
   103     case (_, ZERO) => ZERO
    89     case (ONE, r2s) => r2s
   104     case (ONE, r2s) => r2s
   115 
   130 
   116 def size(r: Rexp): Int = r match {
   131 def size(r: Rexp): Int = r match {
   117   case ZERO => 1
   132   case ZERO => 1
   118   case ONE => 1
   133   case ONE => 1
   119   case CHAR(_) => 1
   134   case CHAR(_) => 1
   120   case ALT(r1, r2) => 1 + size(r1) + size (r2)
   135   case ALTs(rs) => 1 + rs.map(size).sum
   121   case SEQ(r1, r2) => 1 + size(r1) + size (r2)
   136   case SEQ(r1, r2) => 1 + size(r1) + size (r2)
   122   case STAR(r1) => 1 + size(r1)
   137   case STAR(r1) => 1 + size(r1)
   123 }
   138 }
   124 
   139 
   125 
   140 
   130 //matcher(("a" ~ "b") ~ "c", "ab")   // => false
   145 //matcher(("a" ~ "b") ~ "c", "ab")   // => false
   131 
   146 
   132 // the supposedly 'evil' regular expression (a*)* b
   147 // the supposedly 'evil' regular expression (a*)* b
   133 val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
   148 val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
   134 
   149 
   135 //matcher(EVIL, "a" * 1000 ++ "b")   // => true
   150 //println(matcher(EVIL, "a" * 1000 ++ "b"))   // => true
   136 //matcher(EVIL, "a" * 1000)          // => false
   151 //println(matcher(EVIL, "a" * 1000))          // => false
   137 
   152 
   138 // size without simplifications
   153 // size without simplifications
   139 //size(der('a', der('a', EVIL)))             // => 28
   154 //println(size(der('a', der('a', EVIL))))             // => 28
   140 //size(der('a', der('a', der('a', EVIL))))   // => 58
   155 //println(size(der('a', der('a', der('a', EVIL)))))   // => 58
   141 
   156 
   142 // size with simplification
   157 // size with simplification
   143 //size(simp(der('a', der('a', EVIL))))           // => 8
   158 //println(simp(der('a', der('a', EVIL))))          // => 8
   144 //size(simp(der('a', der('a', der('a', EVIL))))) // => 8
   159 //println(simp(der('a', der('a', der('a', EVIL)))))// => 8
       
   160 
       
   161 //println(size(simp(der('a', der('a', EVIL)))))           // => 8
       
   162 //println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 8
   145 
   163 
   146 // Python needs around 30 seconds for matching 28 a's with EVIL. 
   164 // Python needs around 30 seconds for matching 28 a's with EVIL. 
   147 // Java 9 and later increase this to an "astonishing" 40000 a's in
   165 // Java 9 and later increase this to an "astonishing" 40000 a's in
   148 // around 30 seconds.
   166 // around 30 seconds.
   149 //
   167 //