testing4/re.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 10 Apr 2020 12:12:48 +0100
changeset 333 24bc76d97db2
parent 329 8a34b2ebc8cc
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
     1
// Core Part about Regular Expression Matching
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
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
     4
object CW9c {
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
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    13
case class STAR(r: Rexp) extends Rexp             // star
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    15
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    16
// some convenience for typing regular expressions
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    18
import scala.language.implicitConversions    
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    19
import scala.language.reflectiveCalls 
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    20
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def charlist2rexp(s: List[Char]): Rexp = s match {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  case Nil => ONE
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case c::Nil => CHAR(c)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
implicit def RexpOps (r: Rexp) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  def | (s: Rexp) = ALT(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  def % = STAR(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  def ~ (s: Rexp) = SEQ(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
implicit def stringOps (s: String) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def | (r: Rexp) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  def | (r: String) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  def % = STAR(s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  def ~ (r: Rexp) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  def ~ (r: String) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    42
// (5) Complete the function nullable according to
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    43
// the definition given in the coursework; this 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// function checks whether a regular expression
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    45
// can match the empty string and Returns a boolean
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    46
// accordingly.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    48
def nullable (r: Rexp) : Boolean = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    49
	r match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    50
		case ZERO => false
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    51
		case ONE => true
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    52
		case CHAR(c) => false
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    53
		case ALT(r1, r2) => (nullable(r1) || nullable(r2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    54
		case SEQ(r1, r2) => (nullable(r1) && nullable(r2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    55
		case STAR(r) => true
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    56
	}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    59
// (6) Complete the function der according to
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// the definition given in the coursework; this
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    61
// function calculates the derivative of a 
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    62
// regular expression w.r.t. a character.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    64
def der (c: Char, r: Rexp) : Rexp = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    65
	r match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    66
		case ZERO => ZERO
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    67
		case ONE => ZERO
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    68
		case CHAR(d) => if(d == c) ONE else ZERO
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    69
		case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    70
		case SEQ(r1, r2) => if(nullable(r1)) {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    71
								(ALT(SEQ(der(c, r1), r2), der(c, r2)))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    72
							} else {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    73
								SEQ(der(c, r1), r2)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    74
							}
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    75
		case STAR(r) => SEQ(der(c, r), STAR(r))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    76
	}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    79
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    80
// (7) Complete the simp function according to
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// the specification given in the coursework; this
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    82
// function simplifies a regular expression from
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    83
// the inside out, like you would simplify arithmetic 
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    84
// expressions; however it does not simplify inside 
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    85
// STAR-regular expressions.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    87
def simp(r: Rexp) : Rexp = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    88
	r match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    89
		case STAR(r) => STAR(r) // does not process r star
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    90
		case SEQ(r1, r2) => {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    91
			val x = (simp(r1), simp(r2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    92
			if(x._1 == ZERO) ZERO else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    93
			if(x._2 == ZERO) ZERO else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    94
			if(x._1 == ONE) simp(x._2) else 
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    95
			if(x._2 == ONE) simp(x._1) else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    96
			if(x._1 == x._2) simp(x._2) else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    97
			SEQ(simp(x._1), simp(x._2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    98
		}
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    99
		case ALT(r1, r2) => {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   100
			val x = (simp(r1), simp(r2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   101
			if(x._1 == ZERO) simp(x._2) else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   102
			if(x._2 == ZERO) simp(x._1) else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   103
			if(x._1 == x._2) simp(x._2) else
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   104
			ALT(simp(x._1), simp(x._2))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   105
		}
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   106
		case r => r // if single regex, return it
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   107
	}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   110
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   111
// (8) Complete the two functions below; the first 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
// calculates the derivative w.r.t. a string; the second
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
// is the regular expression matcher taking a regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
// expression and a string and checks whether the
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   115
// string matches the regular expression
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   117
def ders (s: List[Char], r: Rexp) : Rexp = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   118
	s match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   119
		case Nil => r
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   120
		case c :: cs => ders(cs, simp(der(c,r)))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   121
	}
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
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   124
def matcher(r: Rexp, s: String): Boolean = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   125
	val listOfCharacters = s.toList
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   126
	val result = ders(listOfCharacters, r)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   127
	nullable(result)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   128
}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   130
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   131
// (9) Complete the size function for regular
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   132
// expressions according to the specification 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
// given in the coursework.
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   135
def size(r: Rexp): Int = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   136
	r match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   137
		case ZERO => 1
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   138
		case ONE => 1
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   139
		case CHAR(c) => 1
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   140
		case ALT(r1, r2) => 1 + size(r1) + size(r2)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   141
		case SEQ(r1, r2) => 1 + size(r1) + size(r2)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   142
		case STAR(r) => 1 + size(r)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   143
	}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
236
e461b5325b5e updated
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   146
// some testing data
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   147
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   148
/*
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   149
matcher(("a" ~ "b") ~ "c", "abc")  // => true
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   150
matcher(("a" ~ "b") ~ "c", "ab")   // => false
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   151
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   152
// the supposedly 'evil' regular expression (a*)* b
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   153
// val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   154
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   155
matcher(EVIL, "a" * 1000 ++ "b")   // => true
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   156
matcher(EVIL, "a" * 1000)          // => false
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
// size without simplifications
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   159
size(der('a', der('a', EVIL)))             // => 28
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   160
size(der('a', der('a', der('a', EVIL))))   // => 58
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
// size with simplification
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   163
size(simp(der('a', der('a', EVIL))))           // => 8
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   164
size(simp(der('a', der('a', der('a', EVIL))))) // => 8
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   165
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   166
// 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
   167
// Java 9 and later increase this to an "astonishing" 40000 a's in
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   168
// 30 seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
//
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   170
// Lets see how long it really takes to match strings with 
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   171
// 5 Million a's...it should be in the range of a couple
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   172
// of seconds.
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
def time_needed[T](i: Int, code: => T) = {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  val start = System.nanoTime()
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  for (j <- 1 to i) code
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  val end = System.nanoTime()
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  (end - start)/(i * 1.0e9)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   181
for (i <- 0 to 5000000 by 500000) {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   182
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   183
}
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   184
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   185
// another "power" test case 
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   186
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
   187
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   188
// the Iterator produces the rexp
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
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   191
//
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   192
//    where SEQ is nested 50 times.
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   193
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   194
*/
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   195
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   196
}