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-- |
623 | 1 |
// A simple matcher for basic regular expressions |
477 | 2 |
|
54 | 3 |
abstract class Rexp |
471 | 4 |
case object ZERO extends Rexp // matches nothing |
623 | 5 |
case object ONE extends Rexp // matches an empty string |
471 | 6 |
case class CHAR(c: Char) extends Rexp // matches a character c |
7 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative |
|
8 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence |
|
9 |
case class STAR(r: Rexp) extends Rexp // star |
|
49 | 10 |
|
471 | 11 |
// nullable function: tests whether a regular |
49 | 12 |
// expression can recognise the empty string |
586 | 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 | 16 |
case CHAR(_) => false |
17 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
18 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
19 |
case STAR(_) => true |
|
20 |
} |
|
21 |
||
623 | 22 |
// the derivative of a regular expression w.r.t. a character |
49 | 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 | 27 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
28 |
case SEQ(r1, r2) => |
|
29 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
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 | 32 |
} |
33 |
||
623 | 34 |
// the derivative w.r.t. a string (iterates der) |
49 | 35 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
36 |
case Nil => r |
|
54 | 37 |
case c::s => ders(s, der(c, r)) |
49 | 38 |
} |
39 |
||
623 | 40 |
// the main matcher function |
631 | 41 |
def matcher(r: Rexp, s: String) : Boolean = |
586 | 42 |
nullable(ders(s.toList, r)) |
49 | 43 |
|
477 | 44 |
|
623 | 45 |
// examples from the homework |
477 | 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 | 51 |
|
550 | 52 |
val r2 = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z')) |
53 |
der('x', r2) |
|
54 |
der('y', der('x', r2)) |
|
55 |
der('z', der('y', der('x', r2))) |
|
546 | 56 |
|
57 |
||
623 | 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 | 60 |
|
623 | 61 |
// the n-times regular expression (explicitly expanded) |
49 | 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 | 64 |
case 1 => r |
65 |
case n => SEQ(r, NTIMES(r, n - 1)) |
|
66 |
} |
|
67 |
||
477 | 68 |
|
69 |
// Test Cases |
|
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 | 72 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
422 | 73 |
|
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 | 76 |
|
623 | 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 | 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 | 81 |
val end = System.nanoTime() |
631 | 82 |
(end - start) / (i * 1.0e9) |
49 | 83 |
} |
84 |
||
480 | 85 |
|
623 | 86 |
// test: (a?{n}) (a{n}) |
556 | 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 | 89 |
for (i <- 0 to 20 by 2) { |
631 | 90 |
println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f") |
49 | 91 |
} |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
92 |
|
623 | 93 |
// test: (a*)* b |
556 | 94 |
println("Test (a*)* b") |
95 |
||
623 | 96 |
for (i <- 0 to 20 by 2) { |
631 | 97 |
println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f") |
415 | 98 |
} |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
99 |
|
477 | 100 |
|
623 | 101 |
// the size of a regular expressions - for testing purposes |
477 | 102 |
def size(r: Rexp) : Int = r match { |
103 |
case ZERO => 1 |
|
104 |
case ONE => 1 |
|
105 |
case CHAR(_) => 1 |
|
106 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
107 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
108 |
case STAR(r) => 1 + size(r) |
|
109 |
} |
|
110 |
||
111 |
// the expicit expansion in EVIL1(n) increases |
|
112 |
// drastically its size |
|
113 |
||
114 |
size(EVIL1(1)) // 5 |
|
115 |
size(EVIL1(3)) // 17 |
|
116 |
size(EVIL1(5)) // 29 |
|
117 |
size(EVIL1(7)) // 41 |
|
623 | 118 |
size(EVIL1(20)) // 119 |
479 | 119 |
|
120 |
// given a regular expression and building successive |
|
121 |
// derivatives might result into bigger and bigger |
|
122 |
// regular expressions...here is an example for this: |
|
123 |
||
563 | 124 |
// (a+b)* o a o b o (a+b)* |
479 | 125 |
val BIG_aux = STAR(ALT(CHAR('a'), CHAR('b'))) |
126 |
val BIG = SEQ(BIG_aux, SEQ(CHAR('a'),SEQ(CHAR('b'), BIG_aux))) |
|
127 |
||
128 |
size(ders("".toList, BIG)) // 13 |
|
129 |
size(ders("ab".toList, BIG)) // 51 |
|
130 |
size(ders("abab".toList, BIG)) // 112 |
|
131 |
size(ders("ababab".toList, BIG)) // 191 |
|
132 |
size(ders("abababab".toList, BIG)) // 288 |
|
133 |
size(ders("ababababab".toList, BIG)) // 403 |
|
134 |
size(ders("abababababab".toList, BIG)) // 536 |
|
135 |
||
136 |
||
137 |
size(ders(("ab" * 200).toList, BIG)) // 366808 |
|
138 |
||
623 | 139 |
for (i <- 0 to 200 by 10) { |
631 | 140 |
println(f"$i: ${time_needed(2, matcher(BIG, "ab" * i))}%.5f") |
556 | 141 |
} |
647 | 142 |
|
143 |
||
144 |
||
145 |
||
146 |
////////////////////////////////////// |
|
147 |
def concat(A: Set[String], B: Set[String]) : Set[String] = |
|
148 |
for (s1 <- A; s2 <- B) yield s1 ++ s2 |
|
149 |
||
150 |
||
151 |
val A = Set("foo", "bar") |
|
152 |
val B = Set("a", "b") |
|
153 |
||
154 |