main_templates3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 01 Nov 2022 15:03:48 +0000
changeset 428 cdfa6a293453
parent 424 daf561a83ba6
child 477 a4e1f63157d8
permissions -rw-r--r--
updated solutions and templates
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
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     2
//==============================================
288
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
abstract class Rexp
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case object ZERO extends Rexp
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case object ONE extends Rexp
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class CHAR(c: Char) extends Rexp
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    10
case class ALTs(rs: List[Rexp]) extends Rexp  // alternatives 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    11
case class SEQs(rs: List[Rexp]) extends Rexp  // sequences
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    12
case class STAR(r: Rexp) extends Rexp         // star
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    13
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    15
//the usual binary choice and binary sequence can be defined 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    16
//in terms of ALTs and SEQs
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    17
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    18
def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    19
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    20
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    21
// some convenience for typing regular expressions
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
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    46
// examples for the implicits:
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    47
// ALT(CHAR('a'), CHAR('b'))
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    48
// val areg : Rexp = "a" | "b"
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    49
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    50
// SEQ(CHAR('a'), CHAR('b')) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    51
// val sreg : Rexp = "a" ~ "b"
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    54
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    55
//======================
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    56
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    57
// (1)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    58
def nullable (r: Rexp) : Boolean = ???
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    59
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    60
// (2) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    61
def der (c: Char, r: Rexp) : Rexp = ???
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    62
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    63
// (3) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    64
def denest(rs: List[Rexp]) : List[Rexp] = ???
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    65
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    66
// (4)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    67
def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    69
// (5)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    70
def ALTs_smart(rs: List[Rexp]) : Rexp = ???
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    71
def SEQs_smart(rs: List[Rexp]) : Rexp = ???
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    72
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    73
// (6)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    74
def simp(r: Rexp) : Rexp = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    76
// (7)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    77
def ders (s: List[Char], r: Rexp) : Rexp = ???
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    78
def matcher(r: Rexp, s: String): Boolean = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    80
// (8) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    81
def size(r: Rexp): Int = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    84
// Some testing data
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    85
//===================
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
/*
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    87
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    88
simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE))   // => ALTs(List(ONE, CHAR(a)))
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    89
simp(((CHAR('a') | ZERO) ~ ONE) | 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    90
     (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO)))   // => CHAR(a)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    91
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    92
matcher(("a" ~ "b") ~ "c", "ab")   // => false
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
matcher(("a" ~ "b") ~ "c", "abc")  // => true
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    94
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// the supposedly 'evil' regular expression (a*)* b
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    99
matcher(EVIL, "a" * 1000)          // => false
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
matcher(EVIL, "a" * 1000 ++ "b")   // => true
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   101
219
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 without simplifications
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   104
size(der('a', der('a', EVIL)))             // => 36
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   105
size(der('a', der('a', der('a', EVIL))))   // => 83
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
// size with simplification
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   108
size(simp(der('a', der('a', EVIL))))           // => 7
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   109
size(simp(der('a', der('a', der('a', EVIL))))) // => 7
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   111
// Python needs around 30 seconds for matching 28 a's with EVIL. 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   112
// 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
   113
// 30 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
// Lets see how long it really takes to match strings with 
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   116
// 5 Million a's...it should be in the range of a few
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   117
// of seconds.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
def time_needed[T](i: Int, code: => T) = {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  val start = System.nanoTime()
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  for (j <- 1 to i) code
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  val end = System.nanoTime()
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   123
  "%.5f".format((end - start)/(i * 1.0e9))
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
for (i <- 0 to 5000000 by 500000) {
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   127
  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
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
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   130
// another "power" test case 
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   131
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
   132
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   133
// the Iterator produces the rexp
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   134
//
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   135
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   136
//
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   137
//    where SEQ is nested 50 times.
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   138
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
*/
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
   141
}