author | Christian Urban <urbanc@in.tum.de> |
Sat, 07 Jan 2017 14:52:26 +0000 | |
changeset 471 | 9476086849ad |
parent 469 | 1f4e81950ab4 |
child 477 | b78664a24f5d |
permissions | -rw-r--r-- |
469
1f4e81950ab4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
1 |
|
54 | 2 |
abstract class Rexp |
471 | 3 |
case object ZERO extends Rexp // matches nothing |
4 |
case object ONE extends Rexp // matches the empty string |
|
5 |
case class CHAR(c: Char) extends Rexp // matches a character c |
|
6 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative |
|
7 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence |
|
8 |
case class STAR(r: Rexp) extends Rexp // star |
|
49 | 9 |
|
471 | 10 |
// nullable function: tests whether a regular |
49 | 11 |
// expression can recognise the empty string |
12 |
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
|
13 |
case ZERO => false |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
14 |
case ONE => true |
49 | 15 |
case CHAR(_) => false |
16 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
17 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
18 |
case STAR(_) => true |
|
453
36e5752fa191
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
440
diff
changeset
|
19 |
|
49 | 20 |
} |
21 |
||
22 |
// derivative of a regular expression w.r.t. a character |
|
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 |
||
34 |
// derivative w.r.t. a string (iterates der) |
|
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 |
||
40 |
// main matcher function |
|
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
41 |
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
49 | 42 |
|
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
43 |
//examples from the homework |
343
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
44 |
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
|
45 |
der('a', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
46 |
der('b', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
47 |
der('c', r) |
49 | 48 |
|
471 | 49 |
//optional (one or zero times) |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
50 |
def OPT(r: Rexp) = ALT(r, ONE) |
49 | 51 |
|
471 | 52 |
//n-times (explicitly expanded) |
49 | 53 |
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
|
54 |
case 0 => ONE |
49 | 55 |
case 1 => r |
56 |
case n => SEQ(r, NTIMES(r, n - 1)) |
|
57 |
} |
|
58 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
59 |
// the evil regular expression a?{n} a{n} |
415 | 60 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
422 | 61 |
|
62 |
// the evil regular expression (a*)*b |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
63 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
49 | 64 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
65 |
//for measuring time |
469
1f4e81950ab4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
66 |
def time_needed[T](i: Int, code: => T) = { |
49 | 67 |
val start = System.nanoTime() |
469
1f4e81950ab4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
68 |
for (j <- 1 to i) code |
49 | 69 |
val end = System.nanoTime() |
70 |
(end - start)/(i * 1.0e9) |
|
71 |
} |
|
72 |
||
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
73 |
//test: (a?{n}) (a{n}) |
433
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
74 |
for (i <- 1 to 20) { |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
75 |
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
|
76 |
} |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
77 |
|
415 | 78 |
for (i <- 1 to 20) { |
79 |
println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
|
49 | 80 |
} |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
81 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
82 |
//test: (a*)* b |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
83 |
for (i <- 1 to 20) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
84 |
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
|
85 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
86 |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
87 |
for (i <- 1 to 20) { |
415 | 88 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
89 |
} |
|
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
90 |