Fahad/Eclipse/ScalaProjects/src/Handouts/Handout2.sc
author fahadausaf <fahad.ausaf@icloud.com>
Sat, 01 Nov 2014 20:25:21 +0000
changeset 36 d205c05e13d6
child 39 56afb5950289
permissions -rw-r--r--
Handout 2
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
  /*
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
  def NTIMES(r: Rexp, n: Int): Rexp = n match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
    case 0 => EMPTY
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
    case 1 => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
    case n => SEQ(r, NTIMES(r, n - 1))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
  }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
  */
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
  def nullable(r: Rexp): Boolean = r match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
    case NULL => false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
    case EMPTY => true
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
    case CHAR(_) => false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
    case ALT(r1, r2) => nullable(r1) || nullable(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
    case SEQ(r1, r2) => nullable(r1) && nullable(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
    case STAR(_) => true
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
    case NTIMES(r, i) => if (i == 0) true else nullable(r)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
  }                                               //> nullable: (r: Handouts.Handout2.Rexp)Boolean
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
  def der(c: Char, r: Rexp): Rexp = r match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
    case NULL => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
    case EMPTY => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
    case CHAR(d) => if (c == d) EMPTY else NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
    case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
    case SEQ(r1, r2) =>
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
      if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
      else SEQ(der(c, r1), r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
    case STAR(r) => SEQ(der(c, r), STAR(r))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
    case NTIMES(r, i) =>
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    47
      if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    48
  }                                               //> der: (c: Char, r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    49
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    50
  def _ders(s: List[Char], r: Rexp): Rexp = s match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    51
    case Nil => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    52
    case c :: s => ders(s, der(c, r))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    53
  }                                               //> _ders: (s: List[Char], r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
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 ders(s: List[Char], r: Rexp): Rexp = s match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    56
    case Nil => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    57
    case c :: s => ders(s, simp(der(c, r)))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    58
  }                                               //> ders: (s: List[Char], r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    59
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    60
  def matches(r: Rexp, s: String): Boolean =
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    61
    nullable(ders(s.toList, r))                   //> matches: (r: Handouts.Handout2.Rexp, s: String)Boolean
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    62
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    63
  def simp(r: Rexp): Rexp = r match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    64
    case ALT(r1, r2) => {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    65
      val r1s = simp(r1)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    66
      val r2s = simp(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    67
      (r1s, r2s) match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    68
        case (NULL, _) => r2s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    69
        case (_, NULL) => r1s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    70
        case _ => if (r1s == r2s) r1s else ALT(r1s, r2s)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    71
      }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    72
    }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    73
    case SEQ(r1, r2) => {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    74
      val r1s = simp(r1)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    75
      val r2s = simp(r2)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    76
      (r1s, r2s) match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    77
        case (NULL, _) => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    78
        case (_, NULL) => NULL
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    79
        case (EMPTY, _) => r2s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    80
        case (_, EMPTY) => r1s
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    81
        case _ => SEQ(r1s, r2s)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    82
      }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    83
    }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    84
    case NTIMES(r, n) => NTIMES(simp(r), n)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    85
    case r => r
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    86
  }                                               //> simp: (r: Handouts.Handout2.Rexp)Handouts.Handout2.Rexp
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    87
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    88
  /************************************************************************************************************************************/
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    89
  // der test
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    90
  // r = {(a.b) + b}*
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    91
  val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    92
                                                  //> r  : Handouts.Handout2.STAR = STAR(ALT(SEQ(CHAR(a),CHAR(b)),CHAR(b)))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    93
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    94
  // => [{(_.b) + null} . {(a.b)+b}*]
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    95
  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
    96
                                                  //| Q(CHAR(a),CHAR(b)),CHAR(b))))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    97
  // => [{(null.b) + _} . {(a.b)+b}*]
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    98
  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
    99
                                                  //| Q(CHAR(a),CHAR(b)),CHAR(b))))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   100
  // => [{(null.b) + _} . {(a.b)+b}*]
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   101
  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
   102
                                                  //| (CHAR(a),CHAR(b)),CHAR(b))))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   103
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   104
  val r2 = ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   105
                                                  //> r2  : Handouts.Handout2.ALT = ALT(SEQ(CHAR(a),CHAR(b)),CHAR(b))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   106
  der('a', r2)                                    //> res3: Handouts.Handout2.Rexp = ALT(SEQ(EMPTY,CHAR(b)),NULL)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   107
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   108
  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
   109
  der('a', r3)                                    //> res4: Handouts.Handout2.Rexp = SEQ(EMPTY,CHAR(b))
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   110
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   111
  val r4 = CHAR('a')                              //> r4  : Handouts.Handout2.CHAR = CHAR(a)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   112
  der('a', r4)                                    //> res5: Handouts.Handout2.Rexp = EMPTY
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   113
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   114
  nullable(r)                                     //> res6: Boolean = true
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   115
  nullable(r2)                                    //> res7: Boolean = false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   116
  nullable(r3)                                    //> res8: Boolean = false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   117
  nullable(r4)                                    //> res9: Boolean = false
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   118
}