progs/re1.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 17 Nov 2019 09:18:47 +0000
changeset 691 991849dfbcb1
parent 647 180600c04da2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
     1
// A simple matcher for basic regular expressions
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
     2
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
     3
abstract class Rexp
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
     4
case object ZERO extends Rexp                    // matches nothing
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
     5
case object ONE extends Rexp                     // matches an empty string
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
     6
case class CHAR(c: Char) extends Rexp            // matches a character c
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
     7
case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
     8
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
     9
case class STAR(r: Rexp) extends Rexp            // star
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
    11
// nullable function: tests whether a regular 
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// expression can recognise the empty string
586
Christian Urban <urbanc@in.tum.de>
parents: 563
diff changeset
    13
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
    14
  case ZERO => false
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    15
  case ONE => true
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case CHAR(_) => false
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  case STAR(_) => true
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    22
// the derivative of a regular expression w.r.t. a character
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
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
    24
  case ZERO => ZERO
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    25
  case ONE => ZERO
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    26
  case CHAR(d) => if (c == d) ONE else ZERO
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  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
    28
  case SEQ(r1, r2) => 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
    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
    30
    else SEQ(der(c, r1), r2)
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    31
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    34
// the derivative w.r.t. a string (iterates der)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def ders (s: List[Char], r: Rexp) : Rexp = s match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  case Nil => r
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    37
  case c::s => ders(s, der(c, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    40
// the main matcher function
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 623
diff changeset
    41
def matcher(r: Rexp, s: String) : Boolean = 
586
Christian Urban <urbanc@in.tum.de>
parents: 563
diff changeset
    42
  nullable(ders(s.toList, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    44
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    45
// examples from the homework
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    46
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    47
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
    48
der('a', r)
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    49
der('b', r)
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 261
diff changeset
    50
der('c', r)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 546
diff changeset
    52
val r2 = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z'))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 546
diff changeset
    53
der('x', r2)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 546
diff changeset
    54
der('y', der('x', r2))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 546
diff changeset
    55
der('z', der('y', der('x', r2)))
546
6589afc6789b updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
    56
6589afc6789b updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
    57
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    58
// the optional regular expression (one or zero times)
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    59
def OPT(r: Rexp) = ALT(r, ONE)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    61
// the n-times regular expression (explicitly expanded)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
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
    63
  case 0 => ONE
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case 1 => r
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case n => SEQ(r, NTIMES(r, n - 1))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    68
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    69
// Test Cases
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    70
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 119
diff changeset
    71
// the evil regular expression  a?{n} a{n}
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    72
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
    73
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    74
// the evil regular expression (a*)*b
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    75
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    77
// for measuring time
469
1f4e81950ab4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 467
diff changeset
    78
def time_needed[T](i: Int, code: => T) = {
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  val start = System.nanoTime()
469
1f4e81950ab4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 467
diff changeset
    80
  for (j <- 1 to i) code
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  val end = System.nanoTime()
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 623
diff changeset
    82
  (end - start) / (i * 1.0e9)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
    85
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    86
// test: (a?{n}) (a{n})
556
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    87
println("Test (a?{n}) (a{n})")
433
c08290ee4f1f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    88
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    89
for (i <- 0 to 20 by 2) {
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 623
diff changeset
    90
  println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f")
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
}
363
0d6deecdb2eb updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    92
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    93
// test: (a*)* b
556
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    94
println("Test (a*)* b")
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    95
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    96
for (i <- 0 to 20 by 2) {
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 623
diff changeset
    97
  println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 363
diff changeset
    98
}
363
0d6deecdb2eb updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    99
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   100
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   101
// the size of a regular expressions - for testing purposes 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   102
def size(r: Rexp) : Int = r match {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   103
  case ZERO => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   104
  case ONE => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   105
  case CHAR(_) => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   106
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   107
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   108
  case STAR(r) => 1 + size(r)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   109
}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   110
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   111
// the expicit expansion in EVIL1(n) increases
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   112
// drastically its size
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   113
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   114
size(EVIL1(1))  // 5
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   115
size(EVIL1(3))  // 17
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   116
size(EVIL1(5))  // 29
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   117
size(EVIL1(7))  // 41
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   118
size(EVIL1(20)) // 119
479
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   119
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   120
// given a regular expression and building successive
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   121
// derivatives might result into bigger and bigger
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   122
// regular expressions...here is an example for this:
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   123
563
bddf14e026b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
   124
// (a+b)* o a o b o (a+b)*
479
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   125
val BIG_aux = STAR(ALT(CHAR('a'), CHAR('b')))
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   126
val BIG = SEQ(BIG_aux, SEQ(CHAR('a'),SEQ(CHAR('b'), BIG_aux)))
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   127
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   128
size(ders("".toList, BIG))              // 13
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   129
size(ders("ab".toList, BIG))            // 51
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   130
size(ders("abab".toList, BIG))          // 112
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   131
size(ders("ababab".toList, BIG))        // 191
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   132
size(ders("abababab".toList, BIG))      // 288
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   133
size(ders("ababababab".toList, BIG))    // 403
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   134
size(ders("abababababab".toList, BIG))  // 536
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   135
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   136
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   137
size(ders(("ab" * 200).toList, BIG))    // 366808
52aa298211f6 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   138
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
   139
for (i <- 0 to 200 by 10) {
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 623
diff changeset
   140
  println(f"$i: ${time_needed(2, matcher(BIG, "ab" * i))}%.5f")
556
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   141
}
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   142
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   143
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   144
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   145
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   146
//////////////////////////////////////
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   147
def concat(A: Set[String], B: Set[String]) : Set[String] =
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   148
  for (s1 <- A; s2 <- B) yield s1 ++ s2
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   149
  
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   150
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   151
val A = Set("foo", "bar")
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   152
val B = Set("a", "b")
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   153
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
   154