Fahad/Eclipse/ScalaProjects/src/Handouts/Handout2.sc
author Chengsong
Wed, 23 Aug 2023 03:02:31 +0100
changeset 668 3831621d7b14
parent 39 56afb5950289
permissions -rw-r--r--
added technical Overview section, almost done introduction
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package Handouts
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
import io.Source
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
import scala.util.matching.Regex
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
import scala.util._
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
object Handout2 {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
  abstract class Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
  case object NULL extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
  case object EMPTY extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  case class CHAR(c: Char) extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
  case class ALT(r1: Rexp, r2: Rexp) extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
  case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
  case class STAR(r: Rexp) extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
  case class NTIMES(r: Rexp, n: Int) extends Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
  def OPT(r: Rexp) = ALT(r, EMPTY)                //> OPT: (r: Handouts.Handout2.Rexp)Handouts.Handout2.ALT
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
  def nullable(r: Rexp): Boolean = r match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
    case NULL => false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
    case EMPTY => true
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
    case CHAR(_) => false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
    case ALT(r1, r2) => nullable(r1) || nullable(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
    case SEQ(r1, r2) => nullable(r1) && nullable(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
    case STAR(_) => true
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
    case NTIMES(r, i) => if (i == 0) true else nullable(r)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
  }                                               //> nullable: (r: Handouts.Handout2.Rexp)Boolean
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
  def der(c: Char, r: Rexp): Rexp = r match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
    case NULL => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
    case EMPTY => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
    case CHAR(d) => if (c == d) EMPTY else NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
    case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
    case SEQ(r1, r2) =>
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
      if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
      else SEQ(der(c, r1), r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
    case STAR(r) => SEQ(der(c, r), STAR(r))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
    case NTIMES(r, i) =>
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
      if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
  }                                               //> der: (c: Char, r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
  def _ders(s: List[Char], r: Rexp): Rexp = s match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
    case Nil => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
    case c :: s => ders(s, der(c, r))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
  }                                               //> _ders: (s: List[Char], r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    47
  def ders(s: List[Char], r: Rexp): Rexp = s match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    48
    case Nil => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    49
    case c :: s => ders(s, simp(der(c, r)))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    50
  }                                               //> ders: (s: List[Char], r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    51
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    52
  def matches(r: Rexp, s: String): Boolean =
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    53
    nullable(ders(s.toList, r))                   //> matches: (r: Handouts.Handout2.Rexp, s: String)Boolean
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    54
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    55
  def simp(r: Rexp): Rexp = r match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    56
    case ALT(r1, r2) => {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    57
      val r1s = simp(r1)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    58
      val r2s = simp(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    59
      (r1s, r2s) match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    60
        case (NULL, _) => r2s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    61
        case (_, NULL) => r1s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    62
        case _ => if (r1s == r2s) r1s else ALT(r1s, r2s)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    63
      }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    64
    }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    65
    case SEQ(r1, r2) => {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    66
      val r1s = simp(r1)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    67
      val r2s = simp(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    68
      (r1s, r2s) match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    69
        case (NULL, _) => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    70
        case (_, NULL) => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    71
        case (EMPTY, _) => r2s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    72
        case (_, EMPTY) => r1s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    73
        case _ => SEQ(r1s, r2s)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    74
      }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    75
    }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    76
    case NTIMES(r, n) => NTIMES(simp(r), n)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    77
    case r => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    78
  }                                               //> simp: (r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    79
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    80
  /************************************************************************************************************************************/
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    81
  // der test
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    82
  // r = {(a.b) + b}*
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    83
  val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    84
                                                  //> r  : Handouts.Handout2.STAR = STAR(ALT(SEQ(CHAR(a),CHAR(b)),CHAR(b)))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    85
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    86
  // => [{(_.b) + null} . {(a.b)+b}*]
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    87
  der('a', r)                                     //> res0: Handouts.Handout2.Rexp = SEQ(ALT(SEQ(EMPTY,CHAR(b)),NULL),STAR(ALT(SE
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    88
                                                  //| Q(CHAR(a),CHAR(b)),CHAR(b))))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    89
  // => [{(null.b) + _} . {(a.b)+b}*]
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    90
  der('b', r)                                     //> res1: Handouts.Handout2.Rexp = SEQ(ALT(SEQ(NULL,CHAR(b)),EMPTY),STAR(ALT(SE
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    91
                                                  //| Q(CHAR(a),CHAR(b)),CHAR(b))))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    92
  // => [{(null.b) + _} . {(a.b)+b}*]
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    93
  der('c', r)                                     //> res2: Handouts.Handout2.Rexp = SEQ(ALT(SEQ(NULL,CHAR(b)),NULL),STAR(ALT(SEQ
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    94
                                                  //| (CHAR(a),CHAR(b)),CHAR(b))))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    95
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    96
  val r2 = ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    97
                                                  //> r2  : Handouts.Handout2.ALT = ALT(SEQ(CHAR(a),CHAR(b)),CHAR(b))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    98
  der('a', r2)                                    //> res3: Handouts.Handout2.Rexp = ALT(SEQ(EMPTY,CHAR(b)),NULL)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    99
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   100
  val r3 = SEQ(CHAR('a'), CHAR('b'))              //> r3  : Handouts.Handout2.SEQ = SEQ(CHAR(a),CHAR(b))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   101
  der('a', r3)                                    //> res4: Handouts.Handout2.Rexp = SEQ(EMPTY,CHAR(b))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   102
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   103
  val r4 = CHAR('a')                              //> r4  : Handouts.Handout2.CHAR = CHAR(a)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   104
  der('a', r4)                                    //> res5: Handouts.Handout2.Rexp = EMPTY
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   105
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   106
  nullable(r)                                     //> res6: Boolean = true
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   107
  nullable(r2)                                    //> res7: Boolean = false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   108
  nullable(r3)                                    //> res8: Boolean = false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   109
  nullable(r4)                                    //> res9: Boolean = false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   110
}