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