main_testing3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 13 Jan 2022 12:55:03 +0000
changeset 423 e9d14d58be3c
parent 420 4edc1a308652
child 424 daf561a83ba6
permissions -rw-r--r--
test
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
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
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
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    11
case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
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
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    16
// some convenience for typing regular expressions
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    17
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    18
//the usual binary choice can be defined in terms of ALTs
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    19
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    20
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    21
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
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    46
// (1) Complete the function nullable according to
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    47
// the definition given in the coursework; this 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// function checks whether a regular expression
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    49
// can match the empty string and Returns a boolean
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    50
// accordingly.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    52
def nullable (r: Rexp) : Boolean = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    53
  case ZERO => false
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    54
  case ONE => true
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    55
  case CHAR(c) => false
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    56
  case ALTs(rs) => {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    57
    if (rs.size == 0) false
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    58
    else if (nullable(rs.head)) true
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    59
    else nullable(ALTs(rs.tail))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    60
  }
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    61
  case SEQ(c, s) => nullable(c) && nullable(s)
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    62
  case STAR(r) => true
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    63
  case _ => false
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    66
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    67
// (2) Complete the function der according to
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// the definition given in the coursework; this
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    69
// function calculates the derivative of a 
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    70
// regular expression w.r.t. a character.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    72
def der (c: Char, r: Rexp) : Rexp = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    73
  case ZERO => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    74
  case ONE => ZERO
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    75
  case CHAR(x) => {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    76
    if (x==c) ONE
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    77
    else ZERO
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    78
  }
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    79
  case ALTs(rs) => ALTs(for (i <- rs) yield der(c, i))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    80
  case SEQ(x, y) => {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    81
    if (nullable(x)) ALTs(List(SEQ(der(c, x), y), der(c, y)))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    82
    else SEQ(der(c, x), y)
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    83
  }
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    84
  case STAR(x) => SEQ(der(c, x), STAR(x))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    85
}
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    86
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    87
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    88
// (3) Implement the flatten function flts. It
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    89
// deletes 0s from a list of regular expressions
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    90
// and also 'spills out', or flattens, nested 
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    91
// ALTernativeS.
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    92
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    93
def flts(rs: List[Rexp]) : List[Rexp] = rs match {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    94
  case Nil => Nil
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    95
  case ZERO::rest => flts(rest)
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    96
  case ALTs(rs_other)::rest => rs_other ::: flts(rest)
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    97
  case r::rest => r::flts(rest)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   100
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   101
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   102
// (4) Complete the simp function according to
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   103
// the specification given in the coursework description; 
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   104
// this function simplifies a regular expression from
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   105
// the inside out, like you would simplify arithmetic 
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   106
// expressions; however it does not simplify inside 
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   107
// STAR-regular expressions. Use the _.distinct and 
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   108
// flts functions.
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   109
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   110
def simp(r: Rexp) : Rexp = r match {
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   111
  case SEQ(x, ZERO) => ZERO
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   112
  case SEQ(ZERO, x) => ZERO
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   113
  case SEQ(x, ONE) => x
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   114
  case SEQ(ONE, x) => x
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   115
  case SEQ(x, y) => SEQ(simp(x), simp(y))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   116
  case ALTs(rs) => {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   117
    val list = flts(for (x <- rs) yield simp(x)).distinct
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   118
    if (list.size == 0) ZERO
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   119
    else if (list.size == 1) list.head
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   120
    else ALTs(list)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   121
  }
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   122
  case x => x
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   125
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   126
// (5) Complete the two functions below; the first 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
// calculates the derivative w.r.t. a string; the second
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
// is the regular expression matcher taking a regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
// expression and a string and checks whether the
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   130
// string matches the regular expression
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   132
def ders (s: List[Char], r: Rexp) : Rexp = s match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   133
  case Nil => r
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   134
  case c::rest => {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   135
    val deriv = simp(der(c,r))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   136
    ders(rest, deriv)
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   137
  }
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   140
def matcher(r: Rexp, s: String): Boolean = nullable(ders(s.toList, r))
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   142
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   143
// (6) Complete the size function for regular
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   144
// expressions according to the specification 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
// given in the coursework.
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   147
def size(r: Rexp): Int = r match {
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   148
  case Nil => 0
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   149
  case ZERO => 1
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   150
  case ONE => 1
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   151
  case CHAR(x) => 1
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   152
  case ALTs(rs) => 1 + (for (x <- rs) yield size(x)).sum
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   153
  case SEQ(x, y) => 1 + size(x) + size(y)
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   154
  case STAR(x) => 1 + size(x)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   157
236
e461b5325b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   158
// some testing data
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   159
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   160
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   161
// matcher(("a" ~ "b") ~ "c", "abc")  // => true
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   162
// matcher(("a" ~ "b") ~ "c", "ab")   // => false
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   163
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   164
// the supposedly 'evil' regular expression (a*)* b
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   165
// val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   166
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   167
// matcher(EVIL, "a" * 1000 ++ "b")   // => true
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   168
// matcher(EVIL, "a" * 1000)          // => false
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
// size without simplifications
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   171
// size(der('a', der('a', EVIL)))             // => 28
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   172
// size(der('a', der('a', der('a', EVIL))))   // => 58
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
// size with simplification
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   175
// size(simp(der('a', der('a', EVIL))))           // => 8
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   176
// size(simp(der('a', der('a', der('a', EVIL))))) // => 8
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   177
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   178
// 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
   179
// Java 9 and later increase this to an "astonishing" 40000 a's in
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   180
// 30 seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
//
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   182
// Lets see how long it really takes to match strings with 
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   183
// 5 Million a's...it should be in the range of a couple
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   184
// of seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   186
// def time_needed[T](i: Int, code: => T) = {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   187
//   val start = System.nanoTime()
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   188
//   for (j <- 1 to i) code
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   189
//   val end = System.nanoTime()
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   190
//   "%.5f".format((end - start)/(i * 1.0e9))
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   191
// }
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   193
// for (i <- 0 to 5000000 by 500000) {
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   194
//   println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   195
// }
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   196
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   197
// another "power" test case 
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   198
// simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next()) == ONE
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   199
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   200
// the Iterator produces the rexp
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   201
//
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   202
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   203
//
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   204
//    where SEQ is nested 50 times.
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   205
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   206
// This a dummy comment. Hopefully it works!
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   207
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   208
}
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   209