progs/re1.scala
changeset 258 1e4da6d2490c
parent 119 a6684e8961d0
child 261 24531cfaa36a
--- a/progs/re1.scala	Mon Sep 22 13:42:14 2014 +0100
+++ b/progs/re1.scala	Fri Sep 26 14:06:55 2014 +0100
@@ -1,7 +1,4 @@
-import scala.language.implicitConversions    
-
 abstract class Rexp
-
 case object NULL extends Rexp
 case object EMPTY extends Rexp
 case class CHAR(c: Char) extends Rexp
@@ -9,15 +6,6 @@
 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
 case class STAR(r: Rexp) extends Rexp 
 
-// some convenience for typing in regular expressions
-def charlist2rexp(s : List[Char]) : Rexp = s match {
-  case Nil => EMPTY
-  case c::Nil => CHAR(c)
-  case c::s => SEQ(CHAR(c), charlist2rexp(s))
-}
-implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
-
-
 // nullable function: tests whether the regular 
 // expression can recognise the empty string
 def nullable (r: Rexp) : Boolean = r match {
@@ -50,12 +38,13 @@
 // main matcher function
 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
 
-//example
+//example from the homework
 //val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
 //der('a', r)
 //der('b', r)
+//der('c', r)
 
-//one or zero
+//optional: one or zero times
 def OPT(r: Rexp) = ALT(r, EMPTY)
 
 //n-times
@@ -65,8 +54,10 @@
   case n => SEQ(r, NTIMES(r, n - 1))
 }
 
-def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
+// the evil regular expression  a?{n} a{n}
+def EVIL(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
 
+//for measuring time
 def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
   for (j <- 1 to i) code
@@ -77,5 +68,3 @@
 for (i <- 1 to 29) {
   println(i + ": " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i))))
 }
-
-