author | Christian Urban <urbanc@in.tum.de> |
Wed, 24 Oct 2018 16:03:07 +0100 | |
changeset 586 | 451a95e1bc25 |
parent 563 | bddf14e026b3 |
child 623 | 47a299e7010f |
permissions | -rw-r--r-- |
477 | 1 |
// Simple matcher for basic regular expressions |
2 |
||
54 | 3 |
abstract class Rexp |
471 | 4 |
case object ZERO extends Rexp // matches nothing |
5 |
case object ONE extends Rexp // matches the empty string |
|
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 |
||
513 | 22 |
|
498 | 23 |
|
49 | 24 |
// derivative of a regular expression w.r.t. a character |
25 |
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
|
26 |
case ZERO => ZERO |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
27 |
case ONE => ZERO |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
28 |
case CHAR(d) => if (c == d) ONE else ZERO |
49 | 29 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
30 |
case SEQ(r1, r2) => |
|
31 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
32 |
else SEQ(der(c, r1), r2) |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
33 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
49 | 34 |
} |
35 |
||
36 |
// derivative w.r.t. a string (iterates der) |
|
37 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
38 |
case Nil => r |
|
54 | 39 |
case c::s => ders(s, der(c, r)) |
49 | 40 |
} |
41 |
||
42 |
// main matcher function |
|
586 | 43 |
def matches(r: Rexp, s: String) : Boolean = |
44 |
nullable(ders(s.toList, r)) |
|
49 | 45 |
|
477 | 46 |
|
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
47 |
//examples from the homework |
477 | 48 |
|
343
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
49 |
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
|
50 |
der('a', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
51 |
der('b', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
52 |
der('c', r) |
49 | 53 |
|
550 | 54 |
val r2 = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z')) |
55 |
der('x', r2) |
|
56 |
der('y', der('x', r2)) |
|
57 |
der('z', der('y', der('x', r2))) |
|
546 | 58 |
|
59 |
||
477 | 60 |
//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
|
61 |
def OPT(r: Rexp) = ALT(r, ONE) |
49 | 62 |
|
477 | 63 |
//n-times regular expression (explicitly expanded) |
49 | 64 |
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
|
65 |
case 0 => ONE |
49 | 66 |
case 1 => r |
67 |
case n => SEQ(r, NTIMES(r, n - 1)) |
|
68 |
} |
|
69 |
||
477 | 70 |
|
71 |
// Test Cases |
|
72 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
73 |
// the evil regular expression a?{n} a{n} |
415 | 74 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
422 | 75 |
|
76 |
// the evil regular expression (a*)*b |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
77 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
49 | 78 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
79 |
//for measuring time |
469
1f4e81950ab4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
80 |
def time_needed[T](i: Int, code: => T) = { |
49 | 81 |
val start = System.nanoTime() |
469
1f4e81950ab4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
82 |
for (j <- 1 to i) code |
49 | 83 |
val end = System.nanoTime() |
84 |
(end - start)/(i * 1.0e9) |
|
85 |
} |
|
86 |
||
480 | 87 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
88 |
//test: (a?{n}) (a{n}) |
556 | 89 |
println("Test (a?{n}) (a{n})") |
433
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
90 |
for (i <- 1 to 20) { |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
91 |
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
|
92 |
} |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
93 |
|
415 | 94 |
for (i <- 1 to 20) { |
95 |
println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
|
49 | 96 |
} |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
97 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
98 |
//test: (a*)* b |
556 | 99 |
println("Test (a*)* b") |
100 |
||
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
101 |
for (i <- 1 to 20) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
102 |
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
|
103 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
104 |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
105 |
for (i <- 1 to 20) { |
415 | 106 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
107 |
} |
|
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
108 |
|
477 | 109 |
|
110 |
||
111 |
||
112 |
// size of a regular expressions - for testing purposes |
|
113 |
def size(r: Rexp) : Int = r match { |
|
114 |
case ZERO => 1 |
|
115 |
case ONE => 1 |
|
116 |
case CHAR(_) => 1 |
|
117 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
118 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
119 |
case STAR(r) => 1 + size(r) |
|
120 |
} |
|
121 |
||
122 |
// the expicit expansion in EVIL1(n) increases |
|
123 |
// drastically its size |
|
124 |
||
125 |
size(EVIL1(1)) // 5 |
|
126 |
size(EVIL1(3)) // 17 |
|
127 |
size(EVIL1(5)) // 29 |
|
128 |
size(EVIL1(7)) // 41 |
|
479 | 129 |
|
130 |
||
131 |
// given a regular expression and building successive |
|
132 |
// derivatives might result into bigger and bigger |
|
133 |
// regular expressions...here is an example for this: |
|
134 |
||
563 | 135 |
// (a+b)* o a o b o (a+b)* |
479 | 136 |
val BIG_aux = STAR(ALT(CHAR('a'), CHAR('b'))) |
137 |
val BIG = SEQ(BIG_aux, SEQ(CHAR('a'),SEQ(CHAR('b'), BIG_aux))) |
|
138 |
||
139 |
size(ders("".toList, BIG)) // 13 |
|
140 |
size(ders("ab".toList, BIG)) // 51 |
|
141 |
size(ders("abab".toList, BIG)) // 112 |
|
142 |
size(ders("ababab".toList, BIG)) // 191 |
|
143 |
size(ders("abababab".toList, BIG)) // 288 |
|
144 |
size(ders("ababababab".toList, BIG)) // 403 |
|
145 |
size(ders("abababababab".toList, BIG)) // 536 |
|
146 |
||
147 |
||
148 |
size(ders(("ab" * 200).toList, BIG)) // 366808 |
|
149 |
||
556 | 150 |
for (i <- 1 to 21) { |
151 |
println(i + " " + "%.5f".format(time_needed(2, matches(BIG, "ab" * i)))) |
|
152 |
} |