| 68 |      1 | // Part 1 about Regular Expression Matching
 | 
|  |      2 | //==========================================
 | 
| 4 |      3 | 
 | 
|  |      4 | abstract class Rexp
 | 
|  |      5 | case object ZERO extends Rexp
 | 
|  |      6 | case object ONE extends Rexp
 | 
|  |      7 | case class CHAR(c: Char) extends Rexp
 | 
| 68 |      8 | case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
 | 
|  |      9 | case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
 | 
|  |     10 | case class STAR(r: Rexp) extends Rexp             // star
 | 
|  |     11 | 
 | 
|  |     12 | 
 | 
|  |     13 | // some convenience for typing in regular expressions
 | 
|  |     14 | 
 | 
|  |     15 | import scala.language.implicitConversions    
 | 
|  |     16 | import scala.language.reflectiveCalls 
 | 
| 4 |     17 | 
 | 
| 68 |     18 | def charlist2rexp(s: List[Char]): Rexp = s match {
 | 
|  |     19 |   case Nil => ONE
 | 
|  |     20 |   case c::Nil => CHAR(c)
 | 
|  |     21 |   case c::s => SEQ(CHAR(c), charlist2rexp(s))
 | 
|  |     22 | }
 | 
|  |     23 | implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
 | 
|  |     24 | 
 | 
|  |     25 | implicit def RexpOps (r: Rexp) = new {
 | 
|  |     26 |   def | (s: Rexp) = ALT(r, s)
 | 
|  |     27 |   def % = STAR(r)
 | 
|  |     28 |   def ~ (s: Rexp) = SEQ(r, s)
 | 
| 4 |     29 | }
 | 
|  |     30 | 
 | 
| 68 |     31 | implicit def stringOps (s: String) = new {
 | 
|  |     32 |   def | (r: Rexp) = ALT(s, r)
 | 
|  |     33 |   def | (r: String) = ALT(s, r)
 | 
|  |     34 |   def % = STAR(s)
 | 
|  |     35 |   def ~ (r: Rexp) = SEQ(s, r)
 | 
|  |     36 |   def ~ (r: String) = SEQ(s, r)
 | 
| 4 |     37 | }
 | 
|  |     38 | 
 | 
| 68 |     39 | // (1a) Complete the function nullable according to
 | 
|  |     40 | // the definition given in the coursework; this 
 | 
|  |     41 | // function checks whether a regular expression
 | 
|  |     42 | // can match the empty string
 | 
|  |     43 | 
 | 
|  |     44 | def nullable (r: Rexp) : Boolean = ...
 | 
| 4 |     45 | 
 | 
|  |     46 | 
 | 
| 68 |     47 | // (1b) Complete the function der according to
 | 
|  |     48 | // the definition given in the coursework; this
 | 
|  |     49 | // function calculates the derivative of a 
 | 
|  |     50 | // regular expression w.r.t. a character
 | 
|  |     51 | 
 | 
|  |     52 | def der (c: Char, r: Rexp) : Rexp = ...
 | 
|  |     53 | 
 | 
|  |     54 | // (1c) Complete the function der according to
 | 
|  |     55 | // the specification given in the coursework; this
 | 
|  |     56 | // function simplifies a regular expression;
 | 
|  |     57 | // however it does not simplify inside STAR-regular
 | 
|  |     58 | // expressions
 | 
|  |     59 | 
 | 
|  |     60 | def simp(r: Rexp) : Rexp = ... 
 | 
|  |     61 | 
 | 
|  |     62 | // (1d) Complete the two functions below; the first 
 | 
|  |     63 | // calculates the derivative w.r.t. a string; the second
 | 
|  |     64 | // is the regular expression matcher taking a regular
 | 
|  |     65 | // expression and a string and checks whether the
 | 
|  |     66 | // string matches the regular expression
 | 
|  |     67 | 
 | 
|  |     68 | def ders (s: List[Char], r: Rexp) : Rexp = ... 
 | 
|  |     69 | 
 | 
|  |     70 | def matcher(r: Rexp, s: String): Boolean = ...
 | 
| 4 |     71 | 
 | 
|  |     72 | 
 | 
| 68 |     73 | // (1e) Complete the function below: it searches (from the left to 
 | 
|  |     74 | // right) in string s1 all the non-empty substrings that match the 
 | 
|  |     75 | // regular expression -- these substrings are assumed to be
 | 
|  |     76 | // the longest substrings matched by the regular expression and
 | 
|  |     77 | // assumed to be non-overlapping. All these substrings in s1 are replaced
 | 
|  |     78 | // by s2.
 | 
|  |     79 | 
 | 
|  |     80 | def replace(r: Rexp, s1: String, s2: String): String = ...
 | 
| 4 |     81 | 
 | 
| 68 |     82 | 
 | 
| 4 |     83 | 
 | 
| 68 |     84 | // some testing data
 | 
|  |     85 | // the supposedly 'evil' regular expression (a*)* b
 | 
|  |     86 | /*
 | 
|  |     87 | val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 | 
|  |     88 | println(matcher(EVIL, "a" * 1000 ++ "b"))
 | 
|  |     89 | println(matcher(EVIL, "a" * 1000))
 | 
| 4 |     90 | 
 | 
|  |     91 | 
 | 
|  |     92 | def time_needed[T](i: Int, code: => T) = {
 | 
|  |     93 |   val start = System.nanoTime()
 | 
|  |     94 |   for (j <- 1 to i) code
 | 
|  |     95 |   val end = System.nanoTime()
 | 
|  |     96 |   (end - start)/(i * 1.0e9)
 | 
|  |     97 | }
 | 
|  |     98 | 
 | 
| 68 |     99 | for (i <- 1 to 5000001 by 500000) {
 | 
|  |    100 |   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
 | 
| 4 |    101 | }
 | 
| 68 |    102 | */
 | 
| 4 |    103 | 
 | 
|  |    104 | 
 |