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