# HG changeset patch # User Christian Urban # Date 1349338053 -3600 # Node ID d48cfc286cb13313106d7a84ae95f11d0849152e # Parent 4d81b2dc827188ae4221e93a20c886fa76600bbe added diff -r 4d81b2dc8271 -r d48cfc286cb1 regexp.scala --- a/regexp.scala Wed Oct 03 02:14:55 2012 +0100 +++ b/regexp.scala Thu Oct 04 09:07:33 2012 +0100 @@ -1,3 +1,4 @@ +// regular expressions abstract class Rexp case object NULL extends Rexp @@ -7,7 +8,54 @@ case class SEQ(r1: Rexp, r2: Rexp) extends Rexp case class STAR(r: Rexp) extends Rexp -// whether it can match the empty string +// some convenience for typing in regular expressions +implicit def string2rexp(s : String) : Rexp = { + s.foldRight (EMPTY: Rexp) ( (c, r) => SEQ(CHAR(c), r) ) +} + +// for example +println(STAR("abc")) + +// produces STAR(SEQ(CHAR(a),SEQ(CHAR(b),SEQ(CHAR(c),EMPTY)))) + + + +// a simple-minded regular expression matcher: +// it loops for examples like STAR(EMPTY) with +// strings this regular expression does not match + +def smatchers(rs: List[Rexp], s: List[Char]) : Boolean = (rs, s) match { + case (NULL::rs, s) => false + case (EMPTY::rs, s) => smatchers(rs, s) + case (CHAR(c)::rs, Nil) => false + case (CHAR(c)::rs, d::s) => (c ==d) && smatchers(rs, s) + case (ALT(r1, r2)::rs, s) => smatchers(r1::rs, s) || smatchers(r2::rs, s) + case (SEQ(r1, r2)::rs, s) => smatchers(r1::r2::rs, s) + case (STAR(r)::rs, s) => smatchers(rs, s) || smatchers(r::STAR(r)::rs, s) + case (Nil, s) => s == Nil +} + +def smatcher(r: Rexp, s: String) = smatchers(List(r), s.toList) + +// regular expression: a +println(smatcher(CHAR('a'), "ab")) + +// regular expression: a + (b o c) +println(smatcher(ALT(CHAR('a'), SEQ(CHAR('b'), CHAR('c'))), "ab")) + +// regular expression: a + (b o c) +println(smatcher(ALT(CHAR('a'), SEQ(CHAR('b'), CHAR('c'))), "bc")) + +// loops for regular expression epsilon* +//println(smatcher(STAR(EMPTY), "a")) + + + +// Regular expression matcher that works properly +//================================================ + +// nullable function: tests whether the regular +// expression can recognise the empty string def nullable (r: Rexp) : Boolean = r match { case NULL => false case EMPTY => true @@ -17,36 +65,37 @@ case STAR(_) => true } -// derivative of a regular expression -def deriv (r: Rexp, c: Char) : Rexp = r match { +// derivative of a regular expression w.r.t. a character +def der (c: Char, r: Rexp) : Rexp = r match { case NULL => NULL case EMPTY => NULL case CHAR(d) => if (c == d) EMPTY else NULL - case ALT(r1, r2) => ALT(deriv(r1, c), deriv(r2, c)) + case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) case SEQ(r1, r2) => - if (nullable(r1)) ALT(SEQ(deriv(r1, c), r2), deriv(r2, c)) - else SEQ(deriv(r1, c), r2) - case STAR(r) => SEQ(deriv(r, c), STAR(r)) + if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) + else SEQ(der(c, r1), r2) + case STAR(r) => SEQ(der(c, r), STAR(r)) } -def derivs (r: Rexp, s: List[Char]) : Rexp = s match { +// derivative w.r.t. a string (iterates der) +def ders (s: List[Char], r: Rexp) : Rexp = s match { case Nil => r - case c::cs => derivs(deriv(r, c), cs) + case c::s => ders(s, der(c, r)) } -// regular expression matching -def matches(r: Rexp, s: String) : Boolean = nullable(derivs(r, s.toList)) +// main matcher function +def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) -/* Examples */ -println(matches(SEQ(SEQ(CHAR('c'), CHAR('a')), CHAR('b')),"cab")) -println(matches(STAR(CHAR('a')),"aaa")) +//examples -/* Convenience using implicits */ -implicit def string2rexp(s : String) : Rexp = { - s.foldRight (EMPTY: Rexp) ( (c, r) => SEQ(CHAR(c), r) ) -} +println(matcher(SEQ(STAR("a"), STAR("b")), "bbaaa")) +println(matcher(ALT(STAR("a"), STAR("b")), "")) +println(matcher("abc", "")) +println(matcher(STAR(ALT(EMPTY, "a")), "")) +println(matcher(STAR(EMPTY), "a")) +println(matcher("cab","cab")) +println(matcher(STAR("a"),"aaa")) +println(matcher("cab" ,"cab")) +println(matcher(STAR("a"),"aaa")) -println(matches("cab" ,"cab")) -println(matches(STAR("a"),"aaa")) -println(matches(STAR("a"),"aaab"))