1 // A version with simplification of derivatives; |
|
2 // this keeps the regular expressions small, which |
|
3 // is good for the run-time |
|
4 |
|
5 |
|
6 abstract class Rexp |
|
7 case object ZERO extends Rexp |
|
8 case object ONE extends Rexp |
|
9 case class CHAR(c: Char) extends Rexp |
|
10 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
11 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
12 case class STAR(r: Rexp) extends Rexp |
|
13 case class NTIMES(r: Rexp, n: Int) extends Rexp |
|
14 |
|
15 |
|
16 |
|
17 // the nullable function: tests whether the regular |
|
18 // expression can recognise the empty string |
|
19 def nullable (r: Rexp) : Boolean = r match { |
|
20 case ZERO => false |
|
21 case ONE => true |
|
22 case CHAR(_) => false |
|
23 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
24 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
25 case STAR(_) => true |
|
26 case NTIMES(r, i) => if (i == 0) true else nullable(r) |
|
27 } |
|
28 |
|
29 // the derivative of a regular expression w.r.t. a character |
|
30 def der (c: Char, r: Rexp) : Rexp = r match { |
|
31 case ZERO => ZERO |
|
32 case ONE => ZERO |
|
33 case CHAR(d) => if (c == d) ONE else ZERO |
|
34 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
35 case SEQ(r1, r2) => |
|
36 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
37 else SEQ(der(c, r1), r2) |
|
38 case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
|
39 case NTIMES(r, i) => |
|
40 if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1)) |
|
41 } |
|
42 |
|
43 def simp(r: Rexp) : Rexp = r match { |
|
44 case ALT(r1, r2) => (simp(r1), simp(r2)) match { |
|
45 case (ZERO, r2s) => r2s |
|
46 case (r1s, ZERO) => r1s |
|
47 case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) |
|
48 } |
|
49 case SEQ(r1, r2) => (simp(r1), simp(r2)) match { |
|
50 case (ZERO, _) => ZERO |
|
51 case (_, ZERO) => ZERO |
|
52 case (ONE, r2s) => r2s |
|
53 case (r1s, ONE) => r1s |
|
54 case (r1s, r2s) => SEQ(r1s, r2s) |
|
55 } |
|
56 case r => r |
|
57 } |
|
58 |
|
59 |
|
60 // the derivative w.r.t. a string (iterates der) |
|
61 def ders(s: List[Char], r: Rexp) : Rexp = s match { |
|
62 case Nil => r |
|
63 case c::s => ders(s, simp(der(c, r))) |
|
64 } |
|
65 |
|
66 |
|
67 // the main matcher function |
|
68 def matcher(r: Rexp, s: String) : Boolean = |
|
69 nullable(ders(s.toList, r)) |
|
70 |
|
71 |
|
72 // one or zero |
|
73 def OPT(r: Rexp) = ALT(r, ONE) |
|
74 |
|
75 |
|
76 // Test Cases |
|
77 |
|
78 // evil regular expressions: (a?){n} a{n} and (a*)* b |
|
79 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
|
80 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
|
81 |
|
82 |
|
83 def time_needed[T](i: Int, code: => T) = { |
|
84 val start = System.nanoTime() |
|
85 for (j <- 1 to i) code |
|
86 val end = System.nanoTime() |
|
87 (end - start)/(i * 1.0e9) |
|
88 } |
|
89 |
|
90 |
|
91 //test: (a?{n}) (a{n}) |
|
92 for (i <- 0 to 8000 by 1000) { |
|
93 println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f") |
|
94 } |
|
95 |
|
96 //test: (a*)* b |
|
97 for (i <- 0 to 6000000 by 500000) { |
|
98 println(f"$i: ${time_needed(3, matcher(EVIL2, "a" * i))}%.5f") |
|
99 } |
|
100 |
|
101 |
|
102 // size of a regular expressions - for testing purposes |
|
103 def size(r: Rexp) : Int = r match { |
|
104 case ZERO => 1 |
|
105 case ONE => 1 |
|
106 case CHAR(_) => 1 |
|
107 case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
108 case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
109 case STAR(r) => 1 + size(r) |
|
110 case NTIMES(r, _) => 1 + size(r) |
|
111 } |
|
112 |
|
113 |
|
114 // now the size of the derivatives grows |
|
115 // much, much slower |
|
116 |
|
117 size(ders("".toList, EVIL2)) // 5 |
|
118 size(ders("a".toList, EVIL2)) // 8 |
|
119 size(ders("aa".toList, EVIL2)) // 8 |
|
120 size(ders("aaa".toList, EVIL2)) // 8 |
|
121 size(ders("aaaa".toList, EVIL2)) // 8 |
|
122 size(ders("aaaaa".toList, EVIL2)) // 8 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|