main_testing3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 23 Dec 2022 16:59:59 +0000
changeset 457 9cf317975ae7
parent 455 557d18cce0f0
child 463 0315d9983cd0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
     1
// Main Part 3 about Regular Expression Matching
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     2
//==============================================
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     4
object M3 {
249
1997cfcd6334 updated
Christian Urban <urbanc@in.tum.de>
parents: 236
diff changeset
     5
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
     6
// Regular Expressions
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
abstract class Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case object ZERO extends Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case object ONE extends Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    11
case class ALTs(rs: List[Rexp]) extends Rexp  // alternatives 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    12
case class SEQs(rs: List[Rexp]) extends Rexp  // sequences
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    13
case class STAR(r: Rexp) extends Rexp         // star
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    16
//the usual binary choice and binary sequence can be defined 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    17
//in terms of ALTs and SEQs
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    18
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    19
def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    20
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    21
// some convenience for typing in regular expressions
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    22
import scala.language.implicitConversions    
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    23
import scala.language.reflectiveCalls 
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    24
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def charlist2rexp(s: List[Char]): Rexp = s match {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case Nil => ONE
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case c::Nil => CHAR(c)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
implicit def RexpOps (r: Rexp) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  def | (s: Rexp) = ALT(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  def % = STAR(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def ~ (s: Rexp) = SEQ(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
implicit def stringOps (s: String) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  def | (r: Rexp) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  def | (r: String) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  def % = STAR(s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  def ~ (r: Rexp) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  def ~ (r: String) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    46
// (1) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    47
def nullable (r: Rexp) : Boolean = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    48
  case ZERO => false
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    49
  case ONE => true
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    50
  case CHAR(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    51
  case ALTs(rs) => rs.exists(nullable)
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    52
  case SEQs(rs) => rs.forall(nullable)
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    53
  case STAR(_) => true
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    56
// (2) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    57
def der(c: Char, r: Rexp) : Rexp = r match {
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    58
  case ZERO => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    59
  case ONE => ZERO
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    60
  case CHAR(d) => if (c == d) ONE else ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    61
  case ALTs(rs) => ALTs(rs.map(der(c, _)))
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    62
  case SEQs(Nil) => ZERO
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    63
  case SEQs(r1::rs) => 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    64
    if (nullable(r1)) ALT(SEQs(der(c, r1)::rs), der(c, SEQs(rs)))
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    65
    else SEQs(der(c, r1):: rs)
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
    66
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    67
}
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    68
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    69
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    70
// (3) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    71
def denest(rs: List[Rexp]) : List[Rexp] = rs match {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    72
  case Nil => Nil
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    73
  case ZERO::tl => denest(tl)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    74
  case ALTs(rs1)::rs2 => rs1 ::: denest(rs2)  
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    75
  case r::rs => r :: denest(rs) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    76
}
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    77
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    78
// (4)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    79
def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = rs match {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    80
  case Nil => acc
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    81
  case ZERO::rs => ZERO::Nil
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    82
  case ONE::rs => flts(rs, acc)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    83
  case SEQs(rs1)::rs => flts(rs, acc ::: rs1)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    84
  case r::rs => flts(rs, acc :+ r) 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    87
// (5)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    88
def ALTs_smart(rs: List[Rexp]) : Rexp = rs match {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    89
  case Nil => ZERO
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    90
  case r::Nil => r  
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    91
  case rs => ALTs(rs)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    92
}
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    93
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    94
def SEQs_smart(rs: List[Rexp]) : Rexp = rs match {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    95
  case Nil => ONE
457
9cf317975ae7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    96
  case ZERO::Nil => ZERO
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    97
  case r::Nil => r
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    98
  case rs => SEQs(rs) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    99
}
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   100
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   101
// (6) 
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   102
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   103
def simp(r: Rexp) : Rexp = r match {
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   104
  case ALTs(rs) => 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   105
    ALTs_smart(denest(rs.map(simp)).distinct)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   106
  case SEQs(rs) => 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   107
    SEQs_smart(flts(rs.map(simp)))
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   108
  case r => r
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   111
//println("Simp tests")
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   112
//println(simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)))
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   113
//println(simp(((CHAR('a') | ZERO) ~ ONE) | 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   114
//              (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))))
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   115
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   116
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   117
// (7) 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   119
def ders (s: List[Char], r: Rexp) : Rexp = s match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   120
  case Nil => r
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   121
  case c::s => ders(s, simp(der(c, r)))
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   124
// main matcher function
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   125
def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   126
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   127
// (8) 
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   128
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   129
def size(r: Rexp): Int = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   130
  case ZERO => 1
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   131
  case ONE => 1
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   132
  case CHAR(_) => 1
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   133
  case ALTs(rs) => 1 + rs.map(size).sum
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   134
  case SEQs(rs) => 1 + rs.map(size).sum
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   135
  case STAR(r1) => 1 + size(r1)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   138
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   139
236
e461b5325b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   140
// some testing data
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   141
/*
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   142
println(matcher(("a" ~ "b") ~ "c", "abc"))  // => true
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   143
println(matcher(("a" ~ "b") ~ "c", "ab"))   // => false
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   144
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   145
// the supposedly 'evil' regular expression (a*)* b
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   146
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   147
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   148
println(matcher(EVIL, "a" * 1000 ++ "b"))   // => true
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   149
println(matcher(EVIL, "a" * 1000))          // => false
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
// size without simplifications
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   152
println(size(der('a', der('a', EVIL))))             // => 36
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   153
println(size(der('a', der('a', der('a', EVIL)))))   // => 83
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
// size with simplification
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   156
println(simp(der('a', der('a', EVIL))))          
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   157
println(simp(der('a', der('a', der('a', EVIL)))))
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   158
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   159
println(size(simp(der('a', der('a', EVIL)))))           // => 7
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   160
println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 7
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   161
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   162
// Python needs around 30 seconds for matching 28 a's with EVIL. 
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   163
// Java 9 and later increase this to an "astonishing" 40000 a's in
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   164
// around 30 seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
//
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   166
// Lets see how long it takes to match strings with 
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   167
// 5 Million a's...it should be in the range of a 
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   168
// few seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   170
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   171
  val start = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   172
  for (j <- 1 to i) code
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   173
  val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   174
  "%.5f".format((end - start)/(i * 1.0e9))
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   175
}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   177
for (i <- 0 to 5000000 by 500000) {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   178
  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   179
}
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   180
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   181
// another "power" test case 
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   182
println(simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next()) == ONE)
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   183
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   184
// the Iterator produces the rexp
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   185
//
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   186
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   187
//
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   188
//    where SEQ is nested 100 times.
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   189
*/ 
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   190
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   191
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   192
assert(simp(ZERO | ONE) == ONE)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   193
assert(simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   194
assert(simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   195
assert(simp(((ONE ~ ONE) ~ ONE) ~ CHAR('a')) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   196
assert(simp(((ONE | ONE) ~ ONE) ~ CHAR('a')) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   197
assert(simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   198
assert(simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   199
assert(simp(CHAR('a') | CHAR('a')) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   200
assert(simp(CHAR('a') ~ CHAR('a')) == CHAR('a') ~ CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   201
assert(simp(ONE | CHAR('a')) == (ONE | CHAR('a')))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   202
assert(simp(ALT((CHAR('a') | ZERO) ~ ONE,
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   203
                  ((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   204
assert(simp((ZERO | ((ZERO | ZERO) | (ZERO | ZERO))) ~ ((ONE | ZERO) | ONE ) ~ (CHAR('a'))) == ZERO)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   205
assert(simp(ALT(ONE | ONE, ONE | ONE)) == ONE)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   206
assert(simp(ALT(ZERO | CHAR('a'), CHAR('a') | ZERO)) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   207
assert(simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) == ALT(ONE, CHAR('a')))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   208
assert(simp(ALTs(Nil)) == ZERO)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   209
assert(simp(SEQs(List(CHAR('a')))) == CHAR('a'))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   210
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   211
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   212
}
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   213