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