725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// A simple matcher for basic regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
// Call the test cases with X = {1,2,3}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
// amm re1.sc testX
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
// or
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
// amm re1.sc all
|
745
|
10 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
// regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
case object ZERO extends Rexp // matches nothing
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
case object ONE extends Rexp // matches an empty string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case class CHAR(c: Char) extends Rexp // matches a character c
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case class STAR(r: Rexp) extends Rexp // star
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
// nullable function: tests whether a regular
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
// expression can recognise the empty string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
def nullable(r: Rexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
case ZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
case ONE => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
case CHAR(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case STAR(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
// the derivative of a regular expression w.r.t. a character
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
case ZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case ONE => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
case CHAR(d) => if (c == d) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
case SEQ(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
else SEQ(der(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
// the derivative w.r.t. a string (iterates der)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
def ders (s: List[Char], r: Rexp) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
case Nil => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
case c::s => ders(s, der(c, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
// the main matcher function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
def matcher(r: Rexp, s: String) : Boolean =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
nullable(ders(s.toList, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
// some examples from the homework
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
der('a', r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
der('b', r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
der('c', r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
val r2 = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z'))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
der('x', r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
der('y', der('x', r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
der('z', der('y', der('x', r2)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
// the optional regular expression (one or zero times)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
def OPT(r: Rexp) = ALT(r, ONE)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
// the n-times regular expression (explicitly expanded)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
case 0 => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
case 1 => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
case n => SEQ(r, NTIMES(r, n - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
// Test Cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
//============
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
// the evil regular expression a?{n} a{n}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
// the evil regular expression (a*)* b
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
// for measuring time
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
def time_needed[T](i: Int, code: => T) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
val start = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
for (j <- 1 to i) code
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
val end = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
(end - start) / (i * 1.0e9)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
// test: (a?{n}) (a{n})
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
@doc("Test (a?{n}) (a{n})")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
def test1() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
println("Test (a?{n}) (a{n})")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
for (i <- 0 to 20 by 2) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
// test: (a*)* b
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
@doc("Test (a*)* b")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
def test2() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
println("Test (a*)* b")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
for (i <- 0 to 20 by 2) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
// the size of a regular expressions - for testing purposes
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
def size(r: Rexp) : Int = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
case ZERO => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
case ONE => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
case CHAR(_) => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case ALT(r1, r2) => 1 + size(r1) + size(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
case STAR(r) => 1 + size(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
// the expicit expansion in EVIL1(n) increases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
// drastically its size
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
size(EVIL1(1)) // 5
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
size(EVIL1(3)) // 17
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
size(EVIL1(5)) // 29
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
size(EVIL1(7)) // 41
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
size(EVIL1(20)) // 119
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
// given a regular expression and building successive
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
// derivatives might result into bigger and bigger
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
// regular expressions...here is an example for this:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
// (a+b)* o a o b o (a+b)*
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
val BIG_aux = STAR(ALT(CHAR('a'), CHAR('b')))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
val BIG = SEQ(BIG_aux, SEQ(CHAR('a'),SEQ(CHAR('b'), BIG_aux)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
size(ders("".toList, BIG)) // 13
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
size(ders("ab".toList, BIG)) // 51
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
size(ders("abab".toList, BIG)) // 112
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
size(ders("ababab".toList, BIG)) // 191
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
size(ders("abababab".toList, BIG)) // 288
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
size(ders("ababababab".toList, BIG)) // 403
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
size(ders("abababababab".toList, BIG)) // 536
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
size(ders(("ab" * 200).toList, BIG)) // 366808
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
@doc("Test (a + b)* o (a o b) o (a + b)*")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
def test3() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
println("Test (a + b)* o (a o b) o (a + b)*")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
for (i <- 0 to 200 by 10) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
println(f"$i: ${time_needed(2, matcher(BIG, "ab" * i))}%.5f")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
@doc("All tests.")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
def all() = { test1(); test2() ; test3() } |