progs/automata/der.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 09 Nov 2024 06:31:45 +0000
changeset 973 f25d338d16c9
parent 784 7dac4492b0e6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
784
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
     1
// Another automaton construction
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
     2
//================================
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     3
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 586
diff changeset
     4
import $file.dfa, dfa._ 
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     5
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     6
// regular expressions
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     7
abstract class Rexp
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     8
case object ZERO extends Rexp                    // matches nothing
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
     9
case object ONE extends Rexp                     // matches the empty string
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    10
case class CHAR(c: Char) extends Rexp            // matches a character c
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    13
case class STAR(r: Rexp) extends Rexp            // star
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    14
784
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    15
// the nullable function: tests whether the regular 
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    16
// expression can recognise the empty string
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    17
def nullable (r: Rexp) : Boolean = r match {
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    18
  case ZERO => false
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    19
  case ONE => true
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    20
  case CHAR(_) => false
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    21
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    22
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    23
  case STAR(_) => true
486
8178fcf377dc updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
}
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    25
784
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    26
// the derivative of a regular expression w.r.t. a character
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    27
def der (c: Char, r: Rexp) : Rexp = r match {
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    28
  case ZERO => ZERO
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    29
  case ONE => ZERO
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    30
  case CHAR(d) => if (c == d) ONE else ZERO
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    31
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    32
  case SEQ(r1, r2) => 
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    33
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    34
    else SEQ(der(c, r1), r2)
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    35
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    36
}
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    37
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    38
784
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    39
def flaw(r: Rexp) : DFA[Rexp, Char] = {
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    40
  DFA(r, 
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    41
      { case (r, c) => der(c, r) }, 
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    42
      nullable(_))
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    43
}
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    44
784
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    45
val r = STAR(CHAR('a'))
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    46
val pseudo = flaw(r)
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    47
println(pseudo.accepts("".toList))    // true
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    48
println(pseudo.accepts("a".toList))   // true
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    49
println(pseudo.accepts("aa".toList))  // true
7dac4492b0e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 779
diff changeset
    50
println(pseudo.accepts("bb".toList))  // false