progs/matcher/cw1template.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 11 Oct 2024 19:13:00 +0100
changeset 967 ce5de01b9632
parent 929 progs/matcher/re3.sc@9541e073f2ed
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
     1
// A template of the simple matcher with simplification 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
     2
// of derivatives to be used in CW1
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
abstract class Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
case object ZERO extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
case object ONE extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    12
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    13
case class RANGE(cs: Set[Char]) extends Rexp             // set of characters
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    14
case class PLUS(r: Rexp) extends Rexp                    // plus
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    15
case class OPTIONAL(r: Rexp) extends Rexp                // optional
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    16
case class INTER(r1: Rexp, r2: Rexp) extends Rexp        // intersection
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    17
case class NTIMES(r: Rexp, n: Int) extends Rexp          // n-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    18
case class UPTO(r: Rexp, n: Int) extends Rexp            // up n-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    19
case class FROM(r: Rexp, n: Int) extends Rexp            // from n-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    20
case class BETWEEN(r: Rexp, n: Int, m: Int) extends Rexp // between nm-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    21
case class NOT(r: Rexp) extends Rexp                     // not
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    22
case class CFUN(f: Char => Boolean) extends Rexp  
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    23
 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
// the nullable function: tests whether the regular 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
// expression can recognise the empty string
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
def nullable (r: Rexp) : Boolean = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  case ZERO => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  case ONE => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  case CHAR(_) => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  case STAR(_) => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    36
  // ??? other cases
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
// the derivative of a regular expression w.r.t. a character
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 782
diff changeset
    40
def der(c: Char, r: Rexp) : Rexp = r match {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  case ZERO => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  case ONE => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  case CHAR(d) => if (c == d) ONE else ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
  case SEQ(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
    else SEQ(der(c, r1), r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  case NTIMES(r, i) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
    if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
    51
  // ??? other cases  
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    54
// simplification
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
def simp(r: Rexp) : Rexp = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    case (ZERO, r2s) => r2s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    case (r1s, ZERO) => r1s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    case (ZERO, _) => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    case (_, ZERO) => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    case (ONE, r2s) => r2s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
    case (r1s, ONE) => r1s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    case (r1s, r2s) => SEQ(r1s, r2s)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case r => r
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    71
// the derivative w.r.t. a string (iterates der and simp)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
def ders(s: List[Char], r: Rexp) : Rexp = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  case Nil => r
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  case c::s => ders(s, simp(der(c, r)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
// the main matcher function
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
def matcher(r: Rexp, s: String) : Boolean = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  nullable(ders(s.toList, r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80