55 case NULL => NULL |
55 case NULL => NULL |
56 case EMPTY => EMPTY |
56 case EMPTY => EMPTY |
57 case r => NTIMES(r, n) |
57 case r => NTIMES(r, n) |
58 } |
58 } |
59 } |
59 } |
60 case class NMTIMES(r: Rexp, n: Int, m: Int) extends Rexp { |
60 case class NUPTOM(r: Rexp, n: Int, m: Int) extends Rexp { |
61 if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.") |
|
62 |
|
63 override def simp = if (m == 0) EMPTY else |
61 override def simp = if (m == 0) EMPTY else |
64 r.simp match { |
62 r.simp match { |
65 case NULL => NULL |
63 case NULL => NULL |
66 case EMPTY => EMPTY |
64 case EMPTY => EMPTY |
67 case r => NMTIMES(r, n, m) |
65 case r => NUPTOM(r, n, m) |
68 } |
66 } |
69 } |
67 } |
|
68 def NMTIMES(r: Rexp, n: Int, m: Int) = { |
|
69 if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.") |
|
70 else NUPTOM(r, n, m - n) |
|
71 } |
|
72 |
|
73 |
70 case class NOT(r: Rexp) extends Rexp { |
74 case class NOT(r: Rexp) extends Rexp { |
71 override def simp = NOT(r.simp) |
75 override def simp = NOT(r.simp) |
72 } |
76 } |
73 case class OPT(r: Rexp) extends Rexp { |
77 case class OPT(r: Rexp) extends Rexp { |
74 override def simp = OPT(r.simp) |
78 override def simp = OPT(r.simp) |
83 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
87 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
84 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
88 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
85 case STAR(_) => true |
89 case STAR(_) => true |
86 case PLUS(r) => nullable(r) |
90 case PLUS(r) => nullable(r) |
87 case NTIMES(r, i) => if (i == 0) true else nullable(r) |
91 case NTIMES(r, i) => if (i == 0) true else nullable(r) |
88 case NMTIMES(r, i, j) => if (i == 0) true else nullable(r) |
92 case NUPTOM(r, i, j) => if (i == 0) true else nullable(r) |
89 case RANGE(_) => false |
93 case RANGE(_) => false |
90 case NOT(r) => !(nullable(r)) |
94 case NOT(r) => !(nullable(r)) |
91 case OPT(_) => true |
95 case OPT(_) => true |
92 } |
96 } |
93 |
97 |
99 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
103 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
100 case SEQ(r1, r2) => |
104 case SEQ(r1, r2) => |
101 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
105 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
102 else SEQ(der(c, r1), r2) |
106 else SEQ(der(c, r1), r2) |
103 case STAR(r) => SEQ(der(c, r), STAR(r)) |
107 case STAR(r) => SEQ(der(c, r), STAR(r)) |
104 case PLUS(r) => der(c, SEQ(r, STAR(r))) |
108 case PLUS(r) => SEQ(der(c, r), STAR(r)) |
105 case NTIMES(r, i) => |
109 case NTIMES(r, i) => |
106 if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1))) |
110 if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1))) |
107 case NMTIMES(r, i, j) => |
111 case NUPTOM(r, i, j) => |
108 if (j == 0) NULL |
112 if (i == 0 && j == 0) NULL else |
109 if (i == 0) der(c, NTIMES(r, j)) |
113 if (i == 0) ALT(der(c, NTIMES(r, j)), der(c, NUPTOM(r, 0, j - 1))) |
110 else der(c, SEQ(r, NMTIMES(r, i - 1, j - 1))) |
114 else der(c, SEQ(r, NUPTOM(r, i - 1, j))) |
111 case RANGE(cs) => if (cs contains c) EMPTY else NULL |
115 case RANGE(cs) => if (cs contains c) EMPTY else NULL |
112 case NOT(r) => NOT(der (c, r)) |
116 case NOT(r) => NOT(der (c, r)) |
113 case OPT(r) => der(c, r) |
117 case OPT(r) => der(c, r) |
114 } |
118 } |
115 |
119 |
186 println(matcher(EVIL1, "aaa" * 45 + "a")) |
190 println(matcher(EVIL1, "aaa" * 45 + "a")) |
187 println("EVIL2:") |
191 println("EVIL2:") |
188 println(matcher(EVIL2, "aaa" * 40)) |
192 println(matcher(EVIL2, "aaa" * 40)) |
189 println(matcher(EVIL2, "aaa" * 43 + "aa")) |
193 println(matcher(EVIL2, "aaa" * 43 + "aa")) |
190 println(matcher(EVIL2, "aaa" * 45 + "a")) |
194 println(matcher(EVIL2, "aaa" * 45 + "a")) |
|
195 |
|
196 println("TEST") |
|
197 val test = NMTIMES(RANGE(LOWERCASE + "."), 2, 6) |
|
198 |
|
199 println(matcher(test,"ab")) |
|
200 println(matcher(test,"abcdef")) |
|
201 println(matcher(test,"abc")) |
|
202 println(matcher(test,"....")) |
|
203 println(matcher(test,"asdfg")) |
|
204 println(matcher(test,"abcdefg")) |
|
205 |
|
206 println(test) |
|
207 println(ders_simp("a".toList, test)) |
|
208 println(ders_simp("aa".toList, test)) |
|
209 println(ders_simp("aaa".toList, test)) |
|
210 println(ders_simp("aaaa".toList, test)) |
|
211 println(ders_simp("aaaaa".toList, test)) |
|
212 println(ders_simp("aaaaaa".toList, test)) |
|
213 println(ders_simp("aaaaaaa".toList, test)) |