main_templates3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 07 Dec 2021 23:17:51 +0000
changeset 418 fa7f7144f2bb
parent 400 e48ea8300b2d
child 424 daf561a83ba6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
     1
// Main Part 3 about Regular Expression Matching
288
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
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
     4
object M3 {
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
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    11
case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
219
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
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    15
import scala.language.implicitConversions    
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    16
import scala.language.reflectiveCalls 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    17
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    18
def charlist2rexp(s: List[Char]): Rexp = s match {
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    19
  case Nil => ONE
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    20
  case c::Nil => CHAR(c)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    21
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    22
}
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    23
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    24
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    25
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    26
// "a|b"
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    27
ALT(CHAR('a'), CHAR('b'))
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    28
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    29
val reg : Rexp = "a" | "b" // CHAR('a')
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    31
// some convenience for typing regular expressions
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    33
//the usual binary choice can be defined in terms of ALTs
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    34
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    35
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    36
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
import scala.language.implicitConversions    
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
import scala.language.reflectiveCalls 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
def charlist2rexp(s: List[Char]): Rexp = s match {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case Nil => ONE
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  case c::Nil => CHAR(c)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    47
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    48
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    49
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
implicit def RexpOps (r: Rexp) = new {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  def | (s: Rexp) = ALT(r, s)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  def % = STAR(r)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  def ~ (s: Rexp) = SEQ(r, s)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
implicit def stringOps (s: String) = new {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  def | (r: Rexp) = ALT(s, r)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  def | (r: String) = ALT(s, r)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  def % = STAR(s)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  def ~ (r: Rexp) = SEQ(s, r)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  def ~ (r: String) = SEQ(s, r)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    64
// (1) Complete the function nullable according to
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
// the definition given in the coursework; this 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// function checks whether a regular expression
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
// can match the empty string and Returns a boolean
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// accordingly.
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    70
def nullable (r: Rexp) : Boolean = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    73
// (2) Complete the function der according to
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// the definition given in the coursework; this
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// function calculates the derivative of a 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// regular expression w.r.t. a character.
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    78
def der (c: Char, r: Rexp) : Rexp = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    81
// (3) Implement the flatten function flts. It
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    82
// deletes 0s from a list of regular expressions
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    83
// and also 'spills out', or flattens, nested 
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    84
// ALTernativeS.
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    85
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    86
def flts(rs: List[Rexp]) : List[Rexp] = ???
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    87
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    88
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    89
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    90
// (4) Complete the simp function according to
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    91
// the specification given in the coursework description; 
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    92
// this function simplifies a regular expression from
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// the inside out, like you would simplify arithmetic 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
// expressions; however it does not simplify inside 
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    95
// STAR-regular expressions. Use the _.distinct and 
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    96
// flts functions.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    98
def simp(r: Rexp) : Rexp = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
   101
// (5) Complete the two functions below; the first 
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
// calculates the derivative w.r.t. a string; the second
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
// is the regular expression matcher taking a regular
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// expression and a string and checks whether the
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// string matches the regular expression
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
   107
def ders (s: List[Char], r: Rexp) : Rexp = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
   109
def matcher(r: Rexp, s: String): Boolean = ???
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
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
   112
// (6) Complete the size function for regular
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
// expressions according to the specification 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
// given in the coursework.
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
   116
def size(r: Rexp): Int = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
// some testing data
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
matcher(("a" ~ "b") ~ "c", "abc")  // => true
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
matcher(("a" ~ "b") ~ "c", "ab")   // => false
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
// the supposedly 'evil' regular expression (a*)* b
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
matcher(EVIL, "a" * 1000 ++ "b")   // => true
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
matcher(EVIL, "a" * 1000)          // => false
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
// size without simplifications
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
size(der('a', der('a', EVIL)))             // => 28
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
size(der('a', der('a', der('a', EVIL))))   // => 58
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
// size with simplification
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
size(simp(der('a', der('a', EVIL))))           // => 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
size(simp(der('a', der('a', der('a', EVIL))))) // => 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   139
// Python needs around 30 seconds for matching 28 a's with EVIL. 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   140
// 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
   141
// 30 seconds.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
//
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
// Lets see how long it really takes to match strings with 
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   144
// 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
   145
// of seconds.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
def time_needed[T](i: Int, code: => T) = {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  val start = System.nanoTime()
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  for (j <- 1 to i) code
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  val end = System.nanoTime()
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   151
  "%.5f".format((end - start)/(i * 1.0e9))
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
for (i <- 0 to 5000000 by 500000) {
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   155
  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   158
// another "power" test case 
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   159
simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next()) == ONE
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   160
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   161
// the Iterator produces the rexp
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   162
//
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   163
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   164
//
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   165
//    where SEQ is nested 50 times.
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   166
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
*/
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
   169
}