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