progs/re3.scala
changeset 363 0d6deecdb2eb
parent 341 ac1187b2e5c9
equal deleted inserted replaced
362:57ea439feaff 363:0d6deecdb2eb
    75 for (i <- 1 to 12001 by 500) {
    75 for (i <- 1 to 12001 by 500) {
    76   println(i + " " + "%.5f".format(time_needed(1, matches(EVIL(i), "a" * i))))
    76   println(i + " " + "%.5f".format(time_needed(1, matches(EVIL(i), "a" * i))))
    77 }
    77 }
    78 
    78 
    79 
    79 
       
    80 // some convenience for typing in regular expressions
       
    81 import scala.language.implicitConversions    
       
    82 import scala.language.reflectiveCalls 
       
    83 def charlist2rexp(s : List[Char]) : Rexp = s match {
       
    84   case Nil => EMPTY
       
    85   case c::Nil => CHAR(c)
       
    86   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    87 }
       
    88 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
       
    89 
       
    90 implicit def RexpOps (r: Rexp) = new {
       
    91   def | (s: Rexp) = ALT(r, s)
       
    92   def % = STAR(r)
       
    93   def ~ (s: Rexp) = SEQ(r, s)
       
    94 }
       
    95 
       
    96 implicit def stringOps (s: String) = new {
       
    97   def | (r: Rexp) = ALT(s, r)
       
    98   def | (r: String) = ALT(s, r)
       
    99   def % = STAR(s)
       
   100   def ~ (r: Rexp) = SEQ(s, r)
       
   101   def ~ (r: String) = SEQ(s, r)
       
   102 }
       
   103 
       
   104 
       
   105 
       
   106 def PLUS(r: Rexp) = SEQ(r, STAR(r))
       
   107 def RANGE(s: List[Char]) : Rexp = s match {
       
   108   case Nil => NULL
       
   109   case c::s => ALT(CHAR(c), RANGE(s))
       
   110 } 
       
   111 
       
   112 println("EVILS:")
       
   113 val EVIL1 = PLUS(PLUS("a" ~ "a" ~ "a"))
       
   114 val EVIL2 = PLUS(PLUS("aaaaaaaaaaaaaaaaaaa" ~ OPT("a")))  // 19 as ~ a?
       
   115 
       
   116 
       
   117 //40 * aaa matches
       
   118 //43 * aaa + aa does not
       
   119 //45 * aaa + a
       
   120 
       
   121 println("EVIL1:")
       
   122 println(matches(EVIL1, "aaa" * 40))
       
   123 println(matches(EVIL1, "aaa" * 43 + "aa"))
       
   124 println(matches(EVIL1, "aaa" * 45 + "a"))
       
   125 println("EVIL2:")
       
   126 println(matches(EVIL2, "aaa" * 40))
       
   127 println(matches(EVIL2, "aaa" * 43 + "aa"))
       
   128 println(matches(EVIL2, "aaa" * 45 + "a"))
       
   129 
       
   130 
       
   131 
       
   132 
       
   133 println("EMAIL:")
       
   134 val LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
       
   135 val DIGITS = "0123456789"
       
   136 val EMAIL = PLUS(RANGE((LOWERCASE + DIGITS + "_" + "." + "-").toList)) ~ "@" ~ 
       
   137             PLUS(RANGE((LOWERCASE + DIGITS + "." + "-").toList)) ~ "." ~
       
   138             NMTIMES(RANGE((LOWERCASE + ".").toList), 2, 6)
       
   139 
       
   140 val my_email = "christian.urban@kcl.ac.uk"