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