test
This commit is contained in:
commit
e6c9c27b33
|
@ -0,0 +1 @@
|
|||
/target
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "aut"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,64 @@
|
|||
#[derive(Debug,Clone)]
|
||||
enum RegExp {
|
||||
Char(char),
|
||||
Empt,
|
||||
Epsilon,
|
||||
Union(Box<RegExp>, Box<RegExp>),
|
||||
Concat(Box<RegExp>, Box<RegExp>),
|
||||
Kleene(Box<RegExp>),
|
||||
}
|
||||
|
||||
fn is_nullable (r : &RegExp) -> bool {
|
||||
match r {
|
||||
RegExp::Epsilon => true,
|
||||
RegExp::Kleene(_) => true,
|
||||
RegExp::Union(r1, r2) => is_nullable(&*r1) | is_nullable(&*r2),
|
||||
RegExp::Concat(r1, r2) => is_nullable(&*r1) & is_nullable(&*r2),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn derivative(r : &RegExp, a : char) -> RegExp {
|
||||
match r {
|
||||
RegExp::Empt => RegExp::Empt,
|
||||
RegExp::Epsilon => RegExp::Empt,
|
||||
RegExp::Char(c) => if *c == a {
|
||||
RegExp::Epsilon
|
||||
} else { RegExp::Empt },
|
||||
RegExp::Union(r1, r2) =>
|
||||
RegExp::Union(Box::new(derivative(&*r1, a)), Box::new(derivative(&*r2, a))),
|
||||
RegExp::Kleene(r) =>
|
||||
RegExp::Concat(Box::new(derivative(&*r, a)),
|
||||
Box::new(RegExp::Kleene(Box::new(*r.clone())))),
|
||||
RegExp::Concat(r1, r2) => {
|
||||
let r : RegExp = RegExp::Concat(Box::new(derivative(&*r1, a)),
|
||||
Box::new(*r2.clone()));
|
||||
if is_nullable(&*r1) {
|
||||
RegExp::Union(Box::new(derivative(&*r2, a)), Box::new(r))
|
||||
} else {
|
||||
r
|
||||
}
|
||||
}
|
||||
_ => RegExp::Empt,
|
||||
}
|
||||
}
|
||||
|
||||
fn regexp_match(r : RegExp, s : &String) -> bool {
|
||||
let chrs = s.chars();
|
||||
let mut rs : RegExp = r;
|
||||
for c in chrs {
|
||||
println!("Computing der({:?}, {:?}) ...", rs, c);
|
||||
rs = derivative(&rs, c);
|
||||
}
|
||||
println!("Result: {:?}", rs);
|
||||
is_nullable(&rs)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let r1 = RegExp::Concat(Box::new( RegExp::Char('a')), Box::new( RegExp::Char('b')));
|
||||
let r2 = RegExp::Kleene(Box::new(r1.clone()));
|
||||
let r3 = RegExp::Concat(Box::new(RegExp::Union(Box::new(r1.clone()), Box::new(RegExp::Char('c')))), Box::new(r2.clone()));
|
||||
let inp = String::from("c");
|
||||
let res = regexp_match(r3.clone(), &inp);
|
||||
println!("match {:?}, {:?} = {:?}", r3, inp, res);
|
||||
}
|
Loading…
Reference in New Issue