Files
@ c827cb147cf5
Branch filter:
Location: Regular-Expresso/src/main.rs - annotation
c827cb147cf5
7.2 KiB
application/rls-services+xml
added the finite automaton determinization
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | f4558f3f9f5e f4558f3f9f5e 6f7aa78a47f7 6f7aa78a47f7 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 1a11f659e7b5 a86ff65ec321 a86ff65ec321 a86ff65ec321 a86ff65ec321 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 6f7aa78a47f7 6f7aa78a47f7 1a11f659e7b5 1a11f659e7b5 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 a522e81d4f37 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e 1a11f659e7b5 1a11f659e7b5 f4558f3f9f5e 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 6f7aa78a47f7 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 f4558f3f9f5e f4558f3f9f5e 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 a86ff65ec321 a86ff65ec321 a86ff65ec321 a86ff65ec321 1a11f659e7b5 1a11f659e7b5 f4558f3f9f5e f4558f3f9f5e 1a11f659e7b5 6f7aa78a47f7 f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 c827cb147cf5 f4558f3f9f5e f4558f3f9f5e f4558f3f9f5e a86ff65ec321 a522e81d4f37 1a11f659e7b5 c827cb147cf5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 1a11f659e7b5 f4558f3f9f5e | use std::collections::{HashMap, HashSet};
const START: usize = usize::MAX;
trait Token {
fn is_skippable(&self) -> bool {false}
fn list_first(&self) -> Vec<usize>;
fn list_last(&self) -> Vec<usize>;
fn list_neighbours(&self) -> Vec<(usize, usize)>;
}
struct Symbol {
position: usize,
value: char
}
struct Asterisk {
content: Box<dyn Token>
}
struct Plus {
content: Box<dyn Token>
}
struct Chain {
content: Vec<Box<dyn Token>>
}
impl Token for Symbol {
fn list_first(&self) -> Vec<usize> {
return vec![self.position];
}
fn list_last(&self) -> Vec<usize> {
return vec![self.position];
}
fn list_neighbours(&self) -> Vec<(usize, usize)> {
return vec![];
}
}
impl Token for Asterisk {
fn is_skippable(&self) -> bool {true}
fn list_first(&self) -> Vec<usize> {
return self.content.list_first();
}
fn list_last(&self) -> Vec<usize> {
return self.content.list_last();
}
fn list_neighbours(&self) -> Vec<(usize, usize)> {
let mut res = self.content.list_neighbours();
for x in self.list_last() {
for y in self.list_first() {
res.push((x, y));
}
}
return res;
}
}
impl Token for Plus {
fn list_first(&self) -> Vec<usize> {
return self.content.list_first();
}
fn list_last(&self) -> Vec<usize> {
return self.content.list_last();
}
fn list_neighbours(&self) -> Vec<(usize, usize)> {
let mut res = self.content.list_neighbours();
for x in self.list_last() {
for y in self.list_first() {
res.push((x, y));
}
}
return res;
}
}
impl Token for Chain {
fn is_skippable(&self) -> bool {
return self.content.iter().all(|x| x.is_skippable());
}
fn list_first(&self) -> Vec<usize> {
let mut res = Vec::new();
for token in self.content.iter() {
res.append(&mut token.list_first());
if !token.is_skippable() {break;}
}
return res;
}
fn list_last(&self) -> Vec<usize> {
let mut res = Vec::new();
for token in self.content.iter().rev() {
res.append(&mut token.list_last());
if !token.is_skippable() {break;}
}
return res;
}
fn list_neighbours(&self) -> Vec<(usize, usize)> {
let mut res = Vec::new();
let mut previous: Vec<&Box<dyn Token>> = Vec::new();
for token in self.content.iter() {
for t in previous.iter() {
for x in t.list_last() {
for y in token.list_first() {
res.push((x, y));
}
}
}
res.append(&mut token.list_neighbours());
if token.is_skippable() {
previous.push(token);
} else {
previous = vec![token];
}
}
return res;
}
}
fn find_closing_parenthesis(s: &String) -> Option<usize> {
let chars: Vec<char> = s.chars().collect();
let mut counter = 0;
for (i, c) in chars.iter().enumerate() {
if *c == '(' {counter += 1;}
else if *c == ')' {counter -= 1;}
if counter == 0 {return Some(i);}
}
return None;
}
fn parse(pattern: &String, offset: usize) -> Chain {
let chars: Vec<char> = pattern.chars().collect();
let mut res: Vec<Box<dyn Token>> = Vec::new();
let mut i = 0;
while i < pattern.len() {
let c = chars[i];
match c {
'(' => {
let j = find_closing_parenthesis(&pattern[i..].to_string()).unwrap() + i;
let inner_content = parse(&pattern[i+1..j].to_string(), offset+i+1);
res.push(Box::new(inner_content));
i = j+1;
}
'*' => {
let token = res.pop().unwrap();
res.push(Box::new(Asterisk{content: token}));
i += 1;
}
'+' => {
let token = res.pop().unwrap();
res.push(Box::new(Plus{content: token}));
i += 1;
}
c => {
res.push(Box::new(Symbol{position: i+offset, value: c}));
i += 1;
}
}
}
return Chain{content: res};
}
fn encode_set(set: &HashSet<usize>) -> u64 {
let mut res = 0;
for x in set.iter() {
res ^= 1<<x;
}
return res;
}
fn decode_set(x: u64) ->HashSet<usize> {
if x == START as u64 {return HashSet::from([START]);}
let mut x = x;
let mut res: HashSet<usize> = HashSet::new();
while x > 0 {
let y = x.trailing_zeros();
res.insert(y as usize);
x ^= 1 << y;
}
return res;
}
struct Regexp {
rules: HashMap<(usize, char), HashSet<usize>>,
end_states: HashSet<usize>
}
impl Regexp {
fn new(pattern: &String) -> Regexp {
let r = parse(pattern, 0);
let pattern_chars = Vec::from_iter(pattern.chars());
let mut rules: HashMap<(usize, char), HashSet<usize>> = HashMap::new();
for i in r.list_first() {
let c = pattern_chars[i];
let key = (START, c);
match rules.get_mut(&key) {
Some(set) => {set.insert(i);},
None => {rules.insert(key, HashSet::from([i]));}
};
}
for (i, j) in r.list_neighbours() {
let c = pattern_chars[j];
let key = (i, c);
match rules.get_mut(&key) {
Some(set) => {set.insert(j);},
None => {rules.insert(key, HashSet::from([j]));}
};
}
let mut end_states = HashSet::from_iter(r.list_last().into_iter());
if r.is_skippable() {
end_states.insert(START);
}
return Regexp{rules, end_states};
}
pub fn eval(&self, s: String) -> bool {
let mut multistate = HashSet::from([START]);
for c in s.chars() {
let mut new_multistate = HashSet::new();
for state in multistate {
if let Some(x) = self.rules.get(&(state, c)) {
new_multistate = new_multistate.union(&x).map(|&y| y).collect();
} else if let Some(x) = self.rules.get(&(state, '.')) {
new_multistate = new_multistate.union(&x).map(|&y| y).collect();
}
}
multistate = new_multistate;
}
return multistate.iter().any(|x| self.end_states.contains(x));
}
fn determinize(&self) -> RegexpDFA {
let mut rules: HashMap<(u64, char), u64> = HashMap::new();
let mut end_states: HashSet<u64> = HashSet::new();
if self.end_states.contains(&START) {end_states.insert(START as u64);}
let mut stack = Vec::from([START as u64]);
let mut processed_states = HashSet::new();
while !stack.is_empty() {
let state = stack.pop().unwrap();
let multistate = decode_set(state);
let mut new_rules: HashMap<char, HashSet<usize>> = HashMap::new();
for key in self.rules.keys().filter(|key| multistate.contains(&key.0)) {
let (_st, c) = key;
if !new_rules.contains_key(c) {
new_rules.insert(*c, HashSet::new());
}
for target in &self.rules[key] {
new_rules.get_mut(c).unwrap().insert(*target);
}
}
for (c, target_set) in new_rules.into_iter() {
let encoded_target = encode_set(&target_set);
rules.insert((state, c), encoded_target);
if target_set.iter().any(|st| self.end_states.contains(st)) {
end_states.insert(encoded_target);
}
if !processed_states.contains(&encoded_target) {
stack.push(encoded_target);
processed_states.insert(encoded_target);
}
}
}
return RegexpDFA{rules, end_states};
}
}
struct RegexpDFA {
rules: HashMap<(u64, char), u64>,
end_states: HashSet<u64>
}
impl RegexpDFA {
fn eval(&self, s: String) -> bool {
let mut state = START as u64;
for c in s.chars() {
if let Some(x) = self.rules.get(&(state, c)) {
state = *x;
} else {
return false;
}
}
return self.end_states.contains(&state);
}
}
fn main() {
let tests = ["", "a", "ab", "aabb", "abab", "abcd", "abcbcdbcd"];
for pattern in ["a*b*", "a+b+", "(ab)*", "(ab)+", "a((bc)*d)*"] {
println!("# {pattern}");
let r = Regexp::new(&pattern.to_string()).determinize();
for &t in tests.iter() {
println!("{t} {}", r.eval(t.to_string()));
}
println!();
}
}
|