diff -r b47e140bcccd -r 47a299e7010f progs/re3.scala --- a/progs/re3.scala Thu Jul 25 14:39:37 2019 +0100 +++ b/progs/re3.scala Sun Jul 28 01:00:41 2019 +0100 @@ -14,7 +14,7 @@ -// nullable function: tests whether the regular +// the nullable function: tests whether the regular // expression can recognise the empty string def nullable (r: Rexp) : Boolean = r match { case ZERO => false @@ -26,7 +26,7 @@ case NTIMES(r, i) => if (i == 0) true else nullable(r) } -// derivative of a regular expression w.r.t. a character +// the derivative of a regular expression w.r.t. a character def der (c: Char, r: Rexp) : Rexp = r match { case ZERO => ZERO case ONE => ZERO @@ -57,35 +57,35 @@ } -// derivative w.r.t. a string (iterates der) +// the derivative w.r.t. a string (iterates der) def ders(s: List[Char], r: Rexp) : Rexp = s match { case Nil => r case c::s => ders(s, simp(der(c, r))) } -// derivative w.r.t. a string (iterates der) +// the derivative w.r.t. a string (iterates der) def dersp(s: List[Char], r: Rexp) : Rexp = s match { case Nil => r case c::s => dersp(s, der(c, r)) } -// main matcher function +// the main matcher function def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) -//tests +// tests val q = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z')) dersp("x".toList, q) dersp("xy".toList, q) dersp("xyz".toList, q) -//one or zero +// one or zero def OPT(r: Rexp) = ALT(r, ONE) // Test Cases -//evil regular expressions: (a?){n} a{n} and (a*)* b +// evil regular expressions: (a?){n} a{n} and (a*)* b def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))