matcher.scala
changeset 75 898c25a4e399
parent 64 2d625418c011
child 85 1a4065f965fb
--- a/matcher.scala	Fri Nov 23 19:31:37 2012 +0000
+++ b/matcher.scala	Fri Nov 23 21:21:27 2012 +0000
@@ -4,11 +4,12 @@
 
 case object NULL extends Rexp
 case object EMPTY extends Rexp
+case object ALLC extends Rexp            // recognises any character
 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 NOT(r: Rexp) extends Rexp
+case class NOT(r: Rexp) extends Rexp     // negation of a regular expression
 
 
 // nullable function: tests whether the regular 
@@ -16,6 +17,7 @@
 def nullable (r: Rexp) : Boolean = r match {
   case NULL => false
   case EMPTY => true
+  case ALLC => false
   case CHAR(_) => false
   case ALT(r1, r2) => nullable(r1) || nullable(r2)
   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
@@ -28,6 +30,7 @@
 def no_more (r: Rexp) : Boolean = r match {
   case NULL => true
   case EMPTY => false
+  case ALLC => false
   case CHAR(_) => false
   case ALT(r1, r2) => no_more(r1) && no_more(r2)
   case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1)
@@ -39,7 +42,9 @@
 // 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 EMPTY => NULL  
+  case ALLC => EMPTY
+  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))