main_templates3/re.scala
changeset 396 3ffe978a5664
parent 352 97bcf8efe4e0
child 400 e48ea8300b2d
--- a/main_templates3/re.scala	Thu Nov 04 12:20:12 2021 +0000
+++ b/main_templates3/re.scala	Fri Nov 05 16:47:55 2021 +0000
@@ -1,20 +1,24 @@
-// Core Part about Regular Expression Matching
+// Main Part 3 about Regular Expression Matching
 //=============================================
 
-object CW8c {
+object M3 {
 
 // Regular Expressions
 abstract class Rexp
 case object ZERO extends Rexp
 case object ONE extends Rexp
 case class CHAR(c: Char) extends Rexp
-case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
+case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
 case class STAR(r: Rexp) extends Rexp             // star
 
 
 // some convenience for typing regular expressions
 
+//the usual binary choice can be defined in terms of ALTs
+def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
+
+
 import scala.language.implicitConversions    
 import scala.language.reflectiveCalls 
 
@@ -56,17 +60,27 @@
 def der (c: Char, r: Rexp) : Rexp = ???
 
 
-// (3) Complete the simp function according to
-// the specification given in the coursework; this
-// function simplifies a regular expression from
+// (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] = ???
+
+
+
+// (4) Complete the simp function according to
+// the specification given in the coursework description; 
+// this function simplifies a regular expression from
 // the inside out, like you would simplify arithmetic 
 // expressions; however it does not simplify inside 
-// STAR-regular expressions.
+// STAR-regular expressions. Use the _.distinct and 
+// flts functions.
 
 def simp(r: Rexp) : Rexp = ???
 
 
-// (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
@@ -77,7 +91,7 @@
 def matcher(r: Rexp, s: String): Boolean = ???
 
 
-// (5) Complete the size function for regular
+// (6) Complete the size function for regular
 // expressions according to the specification 
 // given in the coursework.