testing4/re.scala
changeset 347 4de31fdc0d67
parent 346 663c2a9108d1
child 348 b5b6ed38c2f2
equal deleted inserted replaced
346:663c2a9108d1 347:4de31fdc0d67
     1 // Core Part about Regular Expression Matching
       
     2 //=============================================
       
     3 
       
     4 object CW9c {
       
     5 
       
     6 // Regular Expressions
       
     7 abstract class Rexp
       
     8 case object ZERO extends Rexp
       
     9 case object ONE extends Rexp
       
    10 case class CHAR(c: Char) extends Rexp
       
    11 case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
       
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
       
    13 case class STAR(r: Rexp) extends Rexp             // star
       
    14 
       
    15 
       
    16 // some convenience for typing regular expressions
       
    17 
       
    18 import scala.language.implicitConversions    
       
    19 import scala.language.reflectiveCalls 
       
    20 
       
    21 def charlist2rexp(s: List[Char]): Rexp = s match {
       
    22   case Nil => ONE
       
    23   case c::Nil => CHAR(c)
       
    24   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    25 }
       
    26 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
       
    27 
       
    28 implicit def RexpOps (r: Rexp) = new {
       
    29   def | (s: Rexp) = ALT(r, s)
       
    30   def % = STAR(r)
       
    31   def ~ (s: Rexp) = SEQ(r, s)
       
    32 }
       
    33 
       
    34 implicit def stringOps (s: String) = new {
       
    35   def | (r: Rexp) = ALT(s, r)
       
    36   def | (r: String) = ALT(s, r)
       
    37   def % = STAR(s)
       
    38   def ~ (r: Rexp) = SEQ(s, r)
       
    39   def ~ (r: String) = SEQ(s, r)
       
    40 }
       
    41 
       
    42 // (5) Complete the function nullable according to
       
    43 // the definition given in the coursework; this 
       
    44 // function checks whether a regular expression
       
    45 // can match the empty string and Returns a boolean
       
    46 // accordingly.
       
    47 
       
    48 def nullable (r: Rexp) : Boolean = {
       
    49 	r match {
       
    50 		case ZERO => false
       
    51 		case ONE => true
       
    52 		case CHAR(c) => false
       
    53 		case ALT(r1, r2) => (nullable(r1) || nullable(r2))
       
    54 		case SEQ(r1, r2) => (nullable(r1) && nullable(r2))
       
    55 		case STAR(r) => true
       
    56 	}
       
    57 }
       
    58 
       
    59 // (6) Complete the function der according to
       
    60 // the definition given in the coursework; this
       
    61 // function calculates the derivative of a 
       
    62 // regular expression w.r.t. a character.
       
    63 
       
    64 def der (c: Char, r: Rexp) : Rexp = {
       
    65 	r match {
       
    66 		case ZERO => ZERO
       
    67 		case ONE => ZERO
       
    68 		case CHAR(d) => if(d == c) ONE else ZERO
       
    69 		case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    70 		case SEQ(r1, r2) => if(nullable(r1)) {
       
    71 								(ALT(SEQ(der(c, r1), r2), der(c, r2)))
       
    72 							} else {
       
    73 								SEQ(der(c, r1), r2)
       
    74 							}
       
    75 		case STAR(r) => SEQ(der(c, r), STAR(r))
       
    76 	}
       
    77 }
       
    78 
       
    79 
       
    80 // (7) Complete the simp function according to
       
    81 // the specification given in the coursework; this
       
    82 // function simplifies a regular expression from
       
    83 // the inside out, like you would simplify arithmetic 
       
    84 // expressions; however it does not simplify inside 
       
    85 // STAR-regular expressions.
       
    86 
       
    87 def simp(r: Rexp) : Rexp = {
       
    88 	r match {
       
    89 		case STAR(r) => STAR(r) // does not process r star
       
    90 		case SEQ(r1, r2) => {
       
    91 			val x = (simp(r1), simp(r2))
       
    92 			if(x._1 == ZERO) ZERO else
       
    93 			if(x._2 == ZERO) ZERO else
       
    94 			if(x._1 == ONE) simp(x._2) else 
       
    95 			if(x._2 == ONE) simp(x._1) else
       
    96 			if(x._1 == x._2) simp(x._2) else
       
    97 			SEQ(simp(x._1), simp(x._2))
       
    98 		}
       
    99 		case ALT(r1, r2) => {
       
   100 			val x = (simp(r1), simp(r2))
       
   101 			if(x._1 == ZERO) simp(x._2) else
       
   102 			if(x._2 == ZERO) simp(x._1) else
       
   103 			if(x._1 == x._2) simp(x._2) else
       
   104 			ALT(simp(x._1), simp(x._2))
       
   105 		}
       
   106 		case r => r // if single regex, return it
       
   107 	}
       
   108 }
       
   109 
       
   110 
       
   111 // (8) Complete the two functions below; the first 
       
   112 // calculates the derivative w.r.t. a string; the second
       
   113 // is the regular expression matcher taking a regular
       
   114 // expression and a string and checks whether the
       
   115 // string matches the regular expression
       
   116 
       
   117 def ders (s: List[Char], r: Rexp) : Rexp = {
       
   118 	s match {
       
   119 		case Nil => r
       
   120 		case c :: cs => ders(cs, simp(der(c,r)))
       
   121 	}
       
   122 }
       
   123 
       
   124 def matcher(r: Rexp, s: String): Boolean = {
       
   125 	val listOfCharacters = s.toList
       
   126 	val result = ders(listOfCharacters, r)
       
   127 	nullable(result)
       
   128 }
       
   129 
       
   130 
       
   131 // (9) Complete the size function for regular
       
   132 // expressions according to the specification 
       
   133 // given in the coursework.
       
   134 
       
   135 def size(r: Rexp): Int = {
       
   136 	r match {
       
   137 		case ZERO => 1
       
   138 		case ONE => 1
       
   139 		case CHAR(c) => 1
       
   140 		case ALT(r1, r2) => 1 + size(r1) + size(r2)
       
   141 		case SEQ(r1, r2) => 1 + size(r1) + size(r2)
       
   142 		case STAR(r) => 1 + size(r)
       
   143 	}
       
   144 }
       
   145 
       
   146 // some testing data
       
   147 
       
   148 /*
       
   149 matcher(("a" ~ "b") ~ "c", "abc")  // => true
       
   150 matcher(("a" ~ "b") ~ "c", "ab")   // => false
       
   151 
       
   152 // the supposedly 'evil' regular expression (a*)* b
       
   153 // val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
       
   154 
       
   155 matcher(EVIL, "a" * 1000 ++ "b")   // => true
       
   156 matcher(EVIL, "a" * 1000)          // => false
       
   157 
       
   158 // size without simplifications
       
   159 size(der('a', der('a', EVIL)))             // => 28
       
   160 size(der('a', der('a', der('a', EVIL))))   // => 58
       
   161 
       
   162 // size with simplification
       
   163 size(simp(der('a', der('a', EVIL))))           // => 8
       
   164 size(simp(der('a', der('a', der('a', EVIL))))) // => 8
       
   165 
       
   166 // Python needs around 30 seconds for matching 28 a's with EVIL. 
       
   167 // Java 9 and later increase this to an "astonishing" 40000 a's in
       
   168 // 30 seconds.
       
   169 //
       
   170 // Lets see how long it really takes to match strings with 
       
   171 // 5 Million a's...it should be in the range of a couple
       
   172 // of seconds.
       
   173 
       
   174 def time_needed[T](i: Int, code: => T) = {
       
   175   val start = System.nanoTime()
       
   176   for (j <- 1 to i) code
       
   177   val end = System.nanoTime()
       
   178   (end - start)/(i * 1.0e9)
       
   179 }
       
   180 
       
   181 for (i <- 0 to 5000000 by 500000) {
       
   182   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
       
   183 }
       
   184 
       
   185 // another "power" test case 
       
   186 simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next) == ONE
       
   187 
       
   188 // the Iterator produces the rexp
       
   189 //
       
   190 //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
       
   191 //
       
   192 //    where SEQ is nested 50 times.
       
   193 
       
   194 */
       
   195 
       
   196 }