progs/re1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 02 Oct 2016 15:07:21 +0100
changeset 434 8664ff87cd77
parent 433 c08290ee4f1f
child 440 e14cd32ad497
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
     1
abstract class Rexp
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
     2
case object ZERO extends Rexp
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
     3
case object ONE extends Rexp
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
case class CHAR(c: Char) extends Rexp
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class STAR(r: Rexp) extends Rexp 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// nullable function: tests whether the regular 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// expression can recognise the empty string
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def nullable (r: Rexp) : Boolean = r match {
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    12
  case ZERO => false
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    13
  case ONE => true
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  case CHAR(_) => false
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case STAR(_) => true
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
// derivative of a regular expression w.r.t. a character
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def der (c: Char, r: Rexp) : Rexp = r match {
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    22
  case ZERO => ZERO
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    23
  case ONE => ZERO
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    24
  case CHAR(d) => if (c == d) ONE else ZERO
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case SEQ(r1, r2) => 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    else SEQ(der(c, r1), r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case STAR(r) => SEQ(der(c, r), STAR(r))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// derivative w.r.t. a string (iterates der)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def ders (s: List[Char], r: Rexp) : Rexp = s match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  case Nil => r
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    35
  case c::s => ders(s, der(c, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// main matcher function
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    39
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    41
//examples from the homework
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    42
val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    43
der('a', r)
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    44
der('b', r)
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    45
der('c', r)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 119
diff changeset
    47
//optional: one or zero times
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    48
def OPT(r: Rexp) = ALT(r, ONE)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//n-times
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    52
  case 0 => ONE
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case 1 => r
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  case n => SEQ(r, NTIMES(r, n - 1))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 119
diff changeset
    57
// the evil regular expression  a?{n} a{n}
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    58
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    59
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    60
// the evil regular expression (a*)*b
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    61
val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b'))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 119
diff changeset
    63
//for measuring time
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def time_needed[T](i: Int, code: => T) = {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  val start = System.nanoTime()
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  for (j <- 1 to i) code
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  val end = System.nanoTime()
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  (end - start)/(i * 1.0e9)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    71
//test: (a?{n}) (a{n})
433
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    72
for (i <- 1 to 20) {
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    73
  println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    74
}
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    75
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    76
for (i <- 1 to 20) {
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    77
  println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
363
0d6deecdb2eb updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    79
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    80
//test: (a*)* b
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    81
for (i <- 1 to 6502 by 500) {
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    82
  println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    83
}
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 433
diff changeset
    84
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    85
for (i <- 1 to 6502 by 500) {
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    86
  println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    87
}
363
0d6deecdb2eb updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    88