main_solution3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 07 Dec 2021 23:17:51 +0000
changeset 418 fa7f7144f2bb
parent 400 e48ea8300b2d
child 424 daf561a83ba6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Core Part about Regular Expression Matching
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//=============================================
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     4
object M3 {
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// Regular Expressions
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
abstract class Rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
case object ZERO extends Rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case object ONE extends Rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    11
case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    13
case class STAR(r: Rexp) extends Rexp             // star
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
// some convenience for typing in regular expressions
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    17
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    18
//the usual binary choice can be defined in terms of ALTs
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    19
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    20
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    21
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
import scala.language.implicitConversions    
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
import scala.language.reflectiveCalls 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
def charlist2rexp(s: List[Char]): Rexp = s match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  case Nil => ONE
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  case c::Nil => CHAR(c)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
implicit def RexpOps (r: Rexp) = new {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  def | (s: Rexp) = ALT(r, s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  def % = STAR(r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  def ~ (s: Rexp) = SEQ(r, s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
implicit def stringOps (s: String) = new {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  def | (r: Rexp) = ALT(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  def | (r: String) = ALT(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  def % = STAR(s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  def ~ (r: Rexp) = SEQ(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  def ~ (r: String) = SEQ(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
// (1) Complete the function nullable according to
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
// the definition given in the coursework; this 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
// function checks whether a regular expression
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
// can match the empty string and Returns a boolean
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
// accordingly.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
def nullable (r: Rexp) : Boolean = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  case ZERO => false
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  case ONE => true
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  case CHAR(_) => false
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    57
  case ALTs(rs) => rs.exists(nullable)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case STAR(_) => true
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
// (2) Complete the function der according to
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
// the definition given in the coursework; this
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
// function calculates the derivative of a 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
// regular expression w.r.t. a character.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
def der (c: Char, r: Rexp) : Rexp = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case ZERO => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  case ONE => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  case CHAR(d) => if (c == d) ONE else ZERO
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    71
  case ALTs(rs) => ALTs(rs.map(der(c, _)))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  case SEQ(r1, r2) => 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
    else SEQ(der(c, r1), r2)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    78
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    79
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    80
def flts(rs: List[Rexp]) : List[Rexp] = rs match {
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    81
  case Nil => Nil
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    82
  case ZERO::tl => flts(tl)
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    83
  case ALTs(rs1)::rs2 => rs1 ::: flts(rs2)  
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    84
  case r::rs => r :: flts(rs) 
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    85
}
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    86
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
// (3) Complete the simp function according to
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
// the specification given in the coursework; this
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
// function simplifies a regular expression from
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// the inside out, like you would simplify arithmetic 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// expressions; however it does not simplify inside 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
// STAR-regular expressions.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    94
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
def simp(r: Rexp) : Rexp = r match {
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    96
  case ALTs(rs) => (flts(rs.map(simp)).distinct) match {
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    97
    case Nil => ZERO
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    98
    case r::Nil => r  
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    99
    case rs => ALTs(rs)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  }
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
    case (ZERO, _) => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
    case (_, ZERO) => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
    case (ONE, r2s) => r2s
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
    case (r1s, ONE) => r1s
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
    case (r1s, r2s) => SEQ(r1s, r2s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  }
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case r => r
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   111
simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
// (4) Complete the two functions below; the first 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
// calculates the derivative w.r.t. a string; the second
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
// is the regular expression matcher taking a regular
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
// expression and a string and checks whether the
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
// string matches the regular expression.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
def ders (s: List[Char], r: Rexp) : Rexp = s match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case Nil => r
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case c::s => ders(s, simp(der(c, r)))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
// main matcher function
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
// (5) Complete the size function for regular
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
// expressions according to the specification 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
// given in the coursework.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
def size(r: Rexp): Int = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
  case ZERO => 1
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  case ONE => 1
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  case CHAR(_) => 1
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   136
  case ALTs(rs) => 1 + rs.map(size).sum
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  case SEQ(r1, r2) => 1 + size(r1) + size (r2)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  case STAR(r1) => 1 + size(r1)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
// some testing data
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
//matcher(("a" ~ "b") ~ "c", "abc")  // => true
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
//matcher(("a" ~ "b") ~ "c", "ab")   // => false
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
// the supposedly 'evil' regular expression (a*)* b
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   151
//println(matcher(EVIL, "a" * 1000 ++ "b"))   // => true
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   152
//println(matcher(EVIL, "a" * 1000))          // => false
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
// size without simplifications
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   155
//println(size(der('a', der('a', EVIL))))             // => 28
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   156
//println(size(der('a', der('a', der('a', EVIL)))))   // => 58
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
// size with simplification
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   159
//println(simp(der('a', der('a', EVIL))))          
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   160
//println(simp(der('a', der('a', der('a', EVIL)))))
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   161
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   162
//println(size(simp(der('a', der('a', EVIL)))))           // => 8
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   163
//println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 8
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
// Python needs around 30 seconds for matching 28 a's with EVIL. 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
// Java 9 and later increase this to an "astonishing" 40000 a's in
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
// around 30 seconds.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
//
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
// Lets see how long it takes to match strings with 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
// 5 Million a's...it should be in the range of a 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
// couple of seconds.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
def time_needed[T](i: Int, code: => T) = {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
  val start = System.nanoTime()
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  for (j <- 1 to i) code
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  val end = System.nanoTime()
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   177
  "%.5f".format((end - start)/(i * 1.0e9))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
//for (i <- 0 to 5000000 by 500000) {
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   181
//  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
//}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
// another "power" test case 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
//simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next) == ONE
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
// the Iterator produces the rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
//
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
//
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   191
//    where SEQ is nested 50 times.
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
}