diff -r b47e140bcccd -r 47a299e7010f progs/re1.scala --- a/progs/re1.scala Thu Jul 25 14:39:37 2019 +0100 +++ b/progs/re1.scala Sun Jul 28 01:00:41 2019 +0100 @@ -1,8 +1,8 @@ -// Simple matcher for basic regular expressions +// A simple matcher for basic regular expressions abstract class Rexp case object ZERO extends Rexp // matches nothing -case object ONE extends Rexp // matches the empty string +case object ONE extends Rexp // matches an empty string case class CHAR(c: Char) extends Rexp // matches a character c case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence @@ -19,9 +19,7 @@ case STAR(_) => true } - - -// derivative of a regular expression w.r.t. a character +// the derivative of a regular expression w.r.t. a character def der (c: Char, r: Rexp) : Rexp = r match { case ZERO => ZERO case ONE => ZERO @@ -33,18 +31,18 @@ case STAR(r1) => SEQ(der(c, r1), STAR(r1)) } -// derivative w.r.t. a string (iterates der) +// the derivative w.r.t. a string (iterates der) def ders (s: List[Char], r: Rexp) : Rexp = s match { case Nil => r case c::s => ders(s, der(c, r)) } -// main matcher function +// the main matcher function def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) -//examples from the homework +// examples from the homework val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) der('a', r) @@ -57,10 +55,10 @@ der('z', der('y', der('x', r2))) -//optional regular expression (one or zero times) +// the optional regular expression (one or zero times) def OPT(r: Rexp) = ALT(r, ONE) -//n-times regular expression (explicitly expanded) +// the n-times regular expression (explicitly expanded) def NTIMES(r: Rexp, n: Int) : Rexp = n match { case 0 => ONE case 1 => r @@ -76,40 +74,31 @@ // the evil regular expression (a*)*b val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) -//for measuring time +// for measuring time 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) + "%.5f".format((end - start) / (i * 1.0e9)) } -//test: (a?{n}) (a{n}) +// test: (a?{n}) (a{n}) println("Test (a?{n}) (a{n})") -for (i <- 1 to 20) { - println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) -} -for (i <- 1 to 20) { - println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) +for (i <- 0 to 20 by 2) { + println(s"$i: ${time_needed(2, matches(EVIL1(i), "a" * i))}") } -//test: (a*)* b +// test: (a*)* b println("Test (a*)* b") -for (i <- 1 to 20) { - println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) -} - -for (i <- 1 to 20) { - println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) +for (i <- 0 to 20 by 2) { + println(s"$i: ${time_needed(2, matches(EVIL2, "a" * i))}") } - - -// size of a regular expressions - for testing purposes +// the size of a regular expressions - for testing purposes def size(r: Rexp) : Int = r match { case ZERO => 1 case ONE => 1 @@ -126,7 +115,7 @@ size(EVIL1(3)) // 17 size(EVIL1(5)) // 29 size(EVIL1(7)) // 41 - +size(EVIL1(20)) // 119 // given a regular expression and building successive // derivatives might result into bigger and bigger @@ -147,6 +136,6 @@ size(ders(("ab" * 200).toList, BIG)) // 366808 -for (i <- 1 to 21) { - println(i + " " + "%.5f".format(time_needed(2, matches(BIG, "ab" * i)))) +for (i <- 0 to 200 by 10) { + println(s"$i: ${time_needed(2, matches(BIG, "ab" * i))}") }