// Part 1 about Regular Expression Matching//==========================================abstract class Rexpcase object ZERO extends Rexpcase object ONE extends Rexpcase class CHAR(c: Char) extends Rexpcase class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequencecase class STAR(r: Rexp) extends Rexp // star// some convenience for typing in regular expressionsimport scala.language.implicitConversions import scala.language.reflectiveCalls def charlist2rexp(s: List[Char]): Rexp = s match { case Nil => ONE case c::Nil => CHAR(c) case c::s => SEQ(CHAR(c), charlist2rexp(s))}implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)implicit def RexpOps (r: Rexp) = new { def | (s: Rexp) = ALT(r, s) def % = STAR(r) def ~ (s: Rexp) = SEQ(r, s)}implicit def stringOps (s: String) = new { def | (r: Rexp) = ALT(s, r) def | (r: String) = ALT(s, r) def % = STAR(s) def ~ (r: Rexp) = SEQ(s, r) def ~ (r: String) = SEQ(s, r)}// (1a) Complete the function nullable according to// the definition given in the coursework; this // function checks whether a regular expression// can match the empty stringdef nullable (r: Rexp) : Boolean = ...// (1b) Complete the function der according to// the definition given in the coursework; this// function calculates the derivative of a // regular expression w.r.t. a characterdef der (c: Char, r: Rexp) : Rexp = ...// (1c) Complete the function der according to// the specification given in the coursework; this// function simplifies a regular expression;// however it does not simplify inside STAR-regular// expressionsdef simp(r: Rexp) : Rexp = ... // (1d) Complete the two functions below; the first // calculates the derivative w.r.t. a string; the second// is the regular expression matcher taking a regular// expression and a string and checks whether the// string matches the regular expressiondef ders (s: List[Char], r: Rexp) : Rexp = ... def matcher(r: Rexp, s: String): Boolean = ...// (1e) Complete the function below: it searches (from the left to // right) in string s1 all the non-empty substrings that match the // regular expression -- these substrings are assumed to be// the longest substrings matched by the regular expression and// assumed to be non-overlapping. All these substrings in s1 are replaced// by s2.def replace(r: Rexp, s1: String, s2: String): String = ...// some testing data// the supposedly 'evil' regular expression (a*)* b/*val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))println(matcher(EVIL, "a" * 1000 ++ "b"))println(matcher(EVIL, "a" * 1000))def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() for (j <- 1 to i) code val end = System.nanoTime() (end - start)/(i * 1.0e9)}for (i <- 1 to 5000001 by 500000) { println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))}*/