--- a/Fahad/Eclipse/ScalaProjects/bin/Handouts/Handout2.sc Sun Nov 02 12:24:41 2014 +0000
+++ b/Fahad/Eclipse/ScalaProjects/bin/Handouts/Handout2.sc Tue Nov 04 19:51:01 2014 +0000
@@ -16,14 +16,6 @@
def OPT(r: Rexp) = ALT(r, EMPTY) //> OPT: (r: Handouts.Handout2.Rexp)Handouts.Handout2.ALT
- /*
- def NTIMES(r: Rexp, n: Int): Rexp = n match {
- case 0 => EMPTY
- case 1 => r
- case n => SEQ(r, NTIMES(r, n - 1))
- }
- */
-
def nullable(r: Rexp): Boolean = r match {
case NULL => false
case EMPTY => true
--- a/Fahad/Eclipse/ScalaProjects/src/Handouts/Handout2.sc Sun Nov 02 12:24:41 2014 +0000
+++ b/Fahad/Eclipse/ScalaProjects/src/Handouts/Handout2.sc Tue Nov 04 19:51:01 2014 +0000
@@ -16,14 +16,6 @@
def OPT(r: Rexp) = ALT(r, EMPTY) //> OPT: (r: Handouts.Handout2.Rexp)Handouts.Handout2.ALT
- /*
- def NTIMES(r: Rexp, n: Int): Rexp = n match {
- case 0 => EMPTY
- case 1 => r
- case n => SEQ(r, NTIMES(r, n - 1))
- }
- */
-
def nullable(r: Rexp): Boolean = r match {
case NULL => false
case EMPTY => true
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/Eclipse/ScalaProjects/src/POSIX/PosixAlgo.sc Tue Nov 04 19:51:01 2014 +0000
@@ -0,0 +1,44 @@
+package POSIX
+
+object PosixAlgo {
+
+ abstract class Rexp
+ case object NULL extends Rexp
+ case object EMPTY extends Rexp
+ case class CHAR(c: Char) extends Rexp
+ case class ALT(r1: Rexp, r2: Rexp) extends Rexp
+ case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
+ case class STAR(r: Rexp) extends Rexp
+ case class RECD(x: String, r: Rexp) extends Rexp
+
+ abstract class Val
+ case object Void extends Val
+ case class Chr(c: Char) extends Val
+ case class Sequ(v1: Val, v2: Val) extends Val
+ case class Left(v: Val) extends Val
+ case class Right(v: Val) extends Val
+ case class Stars(vs: List[Val]) extends Val
+ case class Rec(x: String, v: Val) extends Val
+
+ def nullable(r: Rexp): Boolean = r match {
+ case NULL => false
+ case EMPTY => true
+ case CHAR(_) => false
+ case ALT(r1, r2) => nullable(r1) || nullable(r2)
+ case SEQ(r1, r2) => nullable(r1) && nullable(r2)
+ case STAR(_) => true
+ case RECD(_, r1) => nullable(r1)
+ } //> nullable: (r: POSIX.PosixAlgo.Rexp)Boolean
+
+ 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(der(c, r1), der(c, r2))
+ case SEQ(r1, r2) =>
+ 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))
+ case RECD(_, r1) => der(c, r1)
+ } //> der: (c: Char, r: POSIX.PosixAlgo.Rexp)POSIX.PosixAlgo.Rexp
+}
\ No newline at end of file