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