templates3/re.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 06 Aug 2019 12:46:27 +0100
changeset 271 48e12e7aee6e
parent 165 1347bbd86c52
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about Regular Expression Matching
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
object CW8a {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
abstract class Rexp
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case object ZERO extends Rexp
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case object ONE extends Rexp
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class STAR(r: Rexp) extends Rexp             // star
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
// some convenience for typing in regular expressions
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
import scala.language.implicitConversions    
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
import scala.language.reflectiveCalls 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def charlist2rexp(s: List[Char]): Rexp = s match {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  case Nil => ONE
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case c::Nil => CHAR(c)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
implicit def RexpOps (r: Rexp) = new {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  def | (s: Rexp) = ALT(r, s)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  def % = STAR(r)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  def ~ (s: Rexp) = SEQ(r, s)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
implicit def stringOps (s: String) = new {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def | (r: Rexp) = ALT(s, r)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  def | (r: String) = ALT(s, r)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  def % = STAR(s)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  def ~ (r: Rexp) = SEQ(s, r)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  def ~ (r: String) = SEQ(s, r)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// (1a) Complete the function nullable according to
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
// the definition given in the coursework; this 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// function checks whether a regular expression
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// can match the empty string and Returns a boolean
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// accordingly.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//def nullable (r: Rexp) : Boolean = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// (1b) Complete the function der according to
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
// the definition given in the coursework; this
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// function calculates the derivative of a 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
// regular expression w.r.t. a character.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
//def der (c: Char, r: Rexp) : Rexp = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
// (1c) Complete the simp function according to
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// the specification given in the coursework; this
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
// function simplifies a regular expression from
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// the inside out, like you would simplify arithmetic 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
// expressions; however it does not simplify inside 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
// STAR-regular expressions.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
//def simp(r: Rexp) : Rexp = ... 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
// (1d) Complete the two functions below; the first 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// calculates the derivative w.r.t. a string; the second
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// is the regular expression matcher taking a regular
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// expression and a string and checks whether the
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// string matches the regular expression
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
//def ders (s: List[Char], r: Rexp) : Rexp = ... 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
//def matcher(r: Rexp, s: String): Boolean = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// (1e) Complete the size function for regular
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// expressions according to the specification 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
// given in the coursework.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
//def size(r: Rexp): Int = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
// some testing data
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
/*
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
matcher(("a" ~ "b") ~ "c", "abc")  // => true
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
matcher(("a" ~ "b") ~ "c", "ab")   // => false
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// the supposedly 'evil' regular expression (a*)* b
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
matcher(EVIL, "a" * 1000 ++ "b")   // => true
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
matcher(EVIL, "a" * 1000)          // => false
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
// size without simplifications
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
size(der('a', der('a', EVIL)))             // => 28
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
size(der('a', der('a', der('a', EVIL))))   // => 58
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
// size with simplification
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
size(simp(der('a', der('a', EVIL))))           // => 8
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
size(simp(der('a', der('a', der('a', EVIL))))) // => 8
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
// Java needs around 30 seconds for matching 28 a's with EVIL. 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
//
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
// Lets see how long it takes to match strings with 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
// 0.5 Million a's...it should be in the range of some
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
// seconds.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
def time_needed[T](i: Int, code: => T) = {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  val start = System.nanoTime()
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  for (j <- 1 to i) code
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  val end = System.nanoTime()
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  (end - start)/(i * 1.0e9)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
for (i <- 0 to 5000000 by 500000) {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
*/
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
}