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