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