diff -r e9d14d58be3c -r daf561a83ba6 main_solution3/re.scala --- a/main_solution3/re.scala Thu Jan 13 12:55:03 2022 +0000 +++ b/main_solution3/re.scala Mon Apr 11 23:55:27 2022 +0100 @@ -1,4 +1,4 @@ -// Core Part about Regular Expression Matching +// Main Part 3 about Regular Expression Matching //============================================= object M3 { @@ -12,17 +12,14 @@ case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence case class STAR(r: Rexp) extends Rexp // star -// some convenience for typing in regular expressions - //the usual binary choice can be defined in terms of ALTs def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2)) - +// some convenience for typing in regular expressions import scala.language.implicitConversions import scala.language.reflectiveCalls - def charlist2rexp(s: List[Char]): Rexp = s match { case Nil => ONE case c::Nil => CHAR(c) @@ -76,6 +73,10 @@ } +// (3) Implement the flatten function flts. It +// deletes 0s from a list of regular expressions +// and also 'spills out', or flattens, nested +// ALTernativeS. def flts(rs: List[Rexp]) : List[Rexp] = rs match { case Nil => Nil @@ -84,7 +85,7 @@ case r::rs => r :: flts(rs) } -// (3) Complete the simp function according to +// (4) Complete the simp function according to // the specification given in the coursework; this // function simplifies a regular expression from // the inside out, like you would simplify arithmetic @@ -110,7 +111,7 @@ simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) -// (4) Complete the two functions below; the first +// (5) Complete the two functions below; the first // calculates the derivative w.r.t. a string; the second // is the regular expression matcher taking a regular // expression and a string and checks whether the @@ -124,7 +125,7 @@ // main matcher function def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r)) -// (5) Complete the size function for regular +// (6) Complete the size function for regular // expressions according to the specification // given in the coursework. @@ -146,7 +147,7 @@ //matcher(("a" ~ "b") ~ "c", "ab") // => false // the supposedly 'evil' regular expression (a*)* b -val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) +// val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) //println(matcher(EVIL, "a" * 1000 ++ "b")) // => true //println(matcher(EVIL, "a" * 1000)) // => false