main_templates3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 06 Jun 2024 22:18:15 +0100
changeset 490 4778fefecd0c
parent 477 a4e1f63157d8
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
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
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    20
// some convenience for typing regular expressions
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
def charlist2rexp(s: List[Char]): Rexp = s match {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case Nil => ONE
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case c::Nil => CHAR(c)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
}
477
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    27
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    28
import scala.language.implicitConversions
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
477
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    30
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    31
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    32
extension (r: Rexp) {
219
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
477
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    38
// some examples for the conversion and extension:
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    40
// val areg : Rexp = "a" | "b"
477
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    41
//  => ALTs(List(CHAR('a'), CHAR('b')))
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    42
//
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    43
// val sreg : Rexp = "a" ~ "b"
477
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    44
//  => SEQs(List(CHAR('a'), CHAR('b'))) 
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    45
//
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    46
// val star_reg : Rexp = ("a" ~ "b").%
a4e1f63157d8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
    47
//  => STAR(SEQs(List(CHAR('a'), CHAR('b')))) 
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    49
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    50
//======================
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    51
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    52
// (1)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    53
def nullable (r: Rexp) : Boolean = ???
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    54
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    55
// (2) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    56
def der (c: Char, r: Rexp) : Rexp = ???
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    57
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    58
// (3) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    59
def denest(rs: List[Rexp]) : List[Rexp] = ???
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    60
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    61
// (4)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    62
def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    64
// (5)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    65
def ALTs_smart(rs: List[Rexp]) : Rexp = ???
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    66
def SEQs_smart(rs: List[Rexp]) : Rexp = ???
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    67
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    68
// (6)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    69
def simp(r: Rexp) : Rexp = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    71
// (7)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    72
def ders (s: List[Char], r: Rexp) : Rexp = ???
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    73
def matcher(r: Rexp, s: String): Boolean = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    75
// (8) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    76
def size(r: Rexp): Int = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    79
// Some testing data
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    80
//===================
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
/*
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    82
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    83
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
    84
simp(((CHAR('a') | ZERO) ~ ONE) | 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    85
     (((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
    86
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    87
matcher(("a" ~ "b") ~ "c", "ab")   // => false
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
matcher(("a" ~ "b") ~ "c", "abc")  // => true
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    89
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
// the supposedly 'evil' regular expression (a*)* b
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    94
matcher(EVIL, "a" * 1000)          // => false
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
matcher(EVIL, "a" * 1000 ++ "b")   // => true
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    96
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
// size without simplifications
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    99
size(der('a', der('a', EVIL)))             // => 36
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   100
size(der('a', der('a', der('a', EVIL))))   // => 83
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
// size with simplification
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   103
size(simp(der('a', der('a', EVIL))))           // => 7
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   104
size(simp(der('a', der('a', der('a', EVIL))))) // => 7
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   106
// Python needs around 30 seconds for matching 28 a's with EVIL. 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   107
// 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
   108
// 30 seconds.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
//
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
// 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
   111
// 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
   112
// of seconds.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
def time_needed[T](i: Int, code: => T) = {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  val start = System.nanoTime()
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  for (j <- 1 to i) code
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  val end = System.nanoTime()
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   118
  "%.5f".format((end - start)/(i * 1.0e9))
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
for (i <- 0 to 5000000 by 500000) {
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   122
  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
}
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
// another "power" test case 
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   126
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
   127
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   128
// the Iterator produces the rexp
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   129
//
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   130
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   131
//
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   132
//    where SEQ is nested 50 times.
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
   133
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
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
   136
}