864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// CW1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
// extended Rexps
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
case class RANGE(s: Set[Char]) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
case class PLUS(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
case class OPTIONAL(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case class NTIMES(r: Rexp, n: Int) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case class UPTO(r: Rexp, n: Int) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case class FROM(r: Rexp, n: Int) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case class BETWEEN(r: Rexp, n: Int, m: Int) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case class NOT(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
// functions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class CFUN(f: Char => Boolean) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
// abbreviations
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
def FCHAR(c: Char) = CFUN((a: Char) => a == c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
def FSET(s: Set[Char]) = CFUN((a: Char) => s.contains(a))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
val FALL = CFUN(_ => true)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
def nullable (r: Rexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
case ZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
case ONE => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
case CHAR(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
case STAR(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
case RANGE(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case PLUS(r1) => nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case OPTIONAL(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case NTIMES(r1, i) => if (i == 0) true else nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
case UPTO(_, _) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
case FROM(r1, i) => if (i == 0) true else nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
case BETWEEN(r1, i, _) => if (i == 0) true else nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
case NOT(r1) => !nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
case CFUN(f) => false
|
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 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
case ZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
case ONE => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
case CHAR(d) => if (c == d) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
case SEQ(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
else SEQ(der(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
case RANGE(s) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
if (s.contains(c)) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
case PLUS(r1) => SEQ(der(c, r1), STAR(r1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
case OPTIONAL(r1) => der(c, r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
case NTIMES(r, i) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
case UPTO(r1, i) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
if (i == 0) ZERO else SEQ(der(c, r1), UPTO(r1, i - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
case FROM(r1, i) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
if (i == 0) SEQ(der(c, r1), FROM(r1, 0)) else
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
SEQ(der(c, r1), FROM(r1, i - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
case BETWEEN(r1, i, j) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
if (i == 0) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
if (j == 0) ZERO else SEQ(der(c, r1), BETWEEN(r1, 0, j - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
} else SEQ(der(c, r1), BETWEEN(r1, i - 1, j - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
case NOT(r1) => NOT(der(c, r1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
case CFUN(f) => if (f(c)) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
}
|
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 |
def simp(r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
case (ZERO, r2s) => r2s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
case (r1s, ZERO) => r1s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
case (ZERO, _) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
case (_, ZERO) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
case (ONE, r2s) => r2s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
case (r1s, ONE) => r1s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
case r => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
def ders(s: List[Char], r: Rexp) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
case Nil => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
case c::s => ders(s, simp(der(c, r)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
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 |
val Regex31 = NTIMES(CHAR('a'),3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
val Regex32 = NTIMES(OPTIONAL(CHAR('a')),3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
val Regex33 = UPTO(CHAR('a'),3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
val Regex34 = UPTO(OPTIONAL(CHAR('a')),3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
val Regex35 = BETWEEN(CHAR('a'),3,5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
val Regex36 = BETWEEN(OPTIONAL(CHAR('a')),3,5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
val string31 = ""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
val string32 = "a"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
val string33 = "aa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
val string34 = "aaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
val string35 = "aaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
val string36 = "aaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
val string37 = "aaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
println("Question3")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
println(matcher(Regex31, string31))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
println(matcher(Regex31, string32))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
println(matcher(Regex31, string33))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
println(matcher(Regex31, string34))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
println(matcher(Regex31, string35))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
println(matcher(Regex31, string36))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
println(matcher(Regex31, string37)); println("")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
println(matcher(Regex32, string31))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
println(matcher(Regex32, string32))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
println(matcher(Regex32, string33))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
println(matcher(Regex32, string34))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
println(matcher(Regex32, string35))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
println(matcher(Regex32, string36))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
println(matcher(Regex32, string37)); println("")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
println(matcher(Regex33, string31))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
println(matcher(Regex33, string32))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
println(matcher(Regex33, string33))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
println(matcher(Regex33, string34))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
println(matcher(Regex33, string35))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
println(matcher(Regex33, string36))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
println(matcher(Regex33, string37)); println("")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
println(matcher(Regex34, string31))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
println(matcher(Regex34, string32))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
println(matcher(Regex34, string33))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
println(matcher(Regex34, string34))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
println(matcher(Regex34, string35))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
println(matcher(Regex34, string36))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
println(matcher(Regex34, string37)); println("")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
println(matcher(Regex35, string31))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
println(matcher(Regex35, string32))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
println(matcher(Regex35, string33))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
println(matcher(Regex35, string34))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
println(matcher(Regex35, string35))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
println(matcher(Regex35, string36))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
println(matcher(Regex35, string37)); println("")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
println(matcher(Regex36, string31))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
println(matcher(Regex36, string32))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
println(matcher(Regex36, string33))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
println(matcher(Regex36, string34))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
println(matcher(Regex36, string35))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
println(matcher(Regex36, string36))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
println(matcher(Regex36, string37)); println("")
|
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 |
val RegexX = BETWEEN(CHAR('a'), 0, 5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
val stringX = ""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
println(matcher(RegexX, stringX))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
val str0 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
val str1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
val str2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
val matchA = (c:Char) => c == 'a'
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
val reg_1 = SEQ(SEQ(CFUN(matchA), CFUN(matchA)), CFUN(matchA))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
val reg_2 = SEQ(NTIMES(CFUN(matchA), 19), OPTIONAL(CFUN(matchA)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
val reg_1_mod = PLUS(PLUS(reg_1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
val reg_2_mod = PLUS(PLUS(reg_2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
matcher(reg_1_mod, str0)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
matcher(reg_1_mod, str1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
matcher(reg_1_mod, str2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
matcher(reg_2_mod, str0)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
matcher(reg_2_mod, str1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
matcher(reg_2_mod, str2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
//Q3: matcher test (table)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
// a^{3}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
val re1 = NTIMES(CHAR('a'), 3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
matcher(re1, "") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
matcher(re1, "a") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
matcher(re1, "aa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
matcher(re1, "aaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
matcher(re1, "aaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
matcher(re1, "aaaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
// (a?)^{3}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
val re2 = NTIMES(OPTIONAL(CHAR('a')), 3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
matcher(re2, "") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
matcher(re2, "a") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
matcher(re2, "aa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
matcher(re2, "aaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
matcher(re2, "aaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
matcher(re2, "aaaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
// (a)^{..3}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
val re3 = UPTO((CHAR('a')), 3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
matcher(re3, "") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
matcher(re3, "a") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
matcher(re3, "aa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
matcher(re3, "aaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
matcher(re3, "aaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
matcher(re3, "aaaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
// (a?)^{..3}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
val re4 = UPTO(OPTIONAL(CHAR('a')), 3)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
matcher(re4, "") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
matcher(re4, "a") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
matcher(re4, "aa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
matcher(re4, "aaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
matcher(re4, "aaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
matcher(re4, "aaaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
// (a)^{3..5}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
val re5 = BETWEEN(CHAR('a'), 3, 5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
matcher(re5, "") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
matcher(re5, "a") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
matcher(re5, "aa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
matcher(re5, "aaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
matcher(re5, "aaaaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
matcher(re5, "aaaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
247 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
248 |
// (a?)^{3..5}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
249 |
val re6 = BETWEEN(OPTIONAL(CHAR('a')), 3, 5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
250 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
251 |
matcher(re6, "") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
252 |
matcher(re6, "a") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
253 |
matcher(re6, "aa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
254 |
matcher(re6, "aaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
255 |
matcher(re6, "aaaaa") //true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
256 |
matcher(re6, "aaaaaa") //false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
258 |
//Q5: regular expression for email addresses
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
259 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
260 |
val alphaNum = ('a' to 'z').toSet ++ ('0' to '9')
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
261 |
val Q5email = SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
262 |
PLUS(RANGE(alphaNum + '_' + '-' + '.')),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
263 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
264 |
CHAR('@'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
265 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
266 |
PLUS(RANGE(alphaNum + '-' + '.')),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
CHAR('.'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
BETWEEN(RANGE(('a' to 'z').toSet + '.'), 2, 6)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
272 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
273 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
274 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
275 |
ders("nicolas.volken@kcl.ac.uk".toList, Q5email)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
276 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
277 |
// Q6
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
278 |
val Q6 = SEQ(CHAR('/'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
279 |
SEQ(CHAR('*'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
280 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
281 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
282 |
NOT(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
283 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
284 |
STAR(FALL),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
285 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
286 |
CHAR('*'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
287 |
SEQ(
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
288 |
CHAR('/'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
289 |
STAR(FALL)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
290 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
291 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
292 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
293 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
294 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
295 |
,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
296 |
SEQ(CHAR('*'),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
297 |
CHAR('/')
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
298 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
299 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
300 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
301 |
)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
302 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
303 |
matcher(Q6, "/**/")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
304 |
matcher(Q6, "/*foobar*/")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
305 |
matcher(Q6, "/*test*/test*/")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
306 |
matcher(Q6, "/*test/*test*/")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
307 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
308 |
// Q7
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
309 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
310 |
val Q7r1 = SEQ(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
311 |
val Q7r2 = SEQ(BETWEEN(CHAR('a'), 19, 19), OPTIONAL(CHAR('a')))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
312 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
313 |
val Q7str5 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
314 |
val Q7str6 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
315 |
val Q7str7 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
316 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
317 |
matcher(PLUS(PLUS(Q7r1)), Q7str5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
318 |
matcher(PLUS(PLUS(Q7r2)), Q7str5)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
319 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
320 |
matcher(PLUS(PLUS(Q7r1)), Q7str6)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
321 |
matcher(PLUS(PLUS(Q7r2)), Q7str6)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
322 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
323 |
matcher(PLUS(PLUS(Q7r1)), Q7str7)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
324 |
matcher(PLUS(PLUS(Q7r2)), Q7str7)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
325 |
|