Fahad/Eclipse/ScalaProjects/src/Handouts/Posix.sc
author fahadausaf <fahad.ausaf@icloud.com>
Tue, 04 Nov 2014 19:51:39 +0000
changeset 40 e5afb97b54f6
permissions -rw-r--r--
a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package Handouts
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
object Posix {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
  abstract class Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
  case object NULL extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
  case object EMPTY extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
  case class CHAR(c: Char) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
  case class ALT(r1: Rexp, r2: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
  case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  case class STAR(r: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
  case class RECD(x: String, r: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
  abstract class Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
  case object Void extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
  case class Chr(c: Char) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
  case class Sequ(v1: Val, v2: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
  case class Left(v: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
  case class Right(v: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
  case class Stars(vs: List[Val]) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
  case class Rec(x: String, v: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
  def charlist2rexp(s: List[Char]): Rexp = s match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
    case Nil => EMPTY
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
    case c :: Nil => CHAR(c)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
    case c :: s => SEQ(CHAR(c), charlist2rexp(s))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
  }                                               //> charlist2rexp: (s: List[Char])Handouts.Posix.Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
  implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
                                                  //> string2rexp: (s: String)Handouts.Posix.Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
  implicit def RexpOps(r: Rexp) = new {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
    def |(s: Rexp) = ALT(r, s)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
    def % = STAR(r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
    def ~(s: Rexp) = SEQ(r, s)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
  }                                               //> RexpOps: (r: Handouts.Posix.Rexp)AnyRef{def |(s: Handouts.Posix.Rexp): Hand
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
                                                  //| outs.Posix.ALT; def %: Handouts.Posix.STAR; def ~(s: Handouts.Posix.Rexp): 
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
                                                  //| Handouts.Posix.SEQ}
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
  implicit def stringOps(s: String) = new {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
    def |(r: Rexp) = ALT(s, r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
    def |(r: String) = ALT(s, r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
    def % = STAR(s)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
    def ~(r: Rexp) = SEQ(s, r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
    def ~(r: String) = SEQ(s, r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
    def $(r: Rexp) = RECD(s, r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
  }                                               //> stringOps: (s: String)AnyRef{def |(r: Handouts.Posix.Rexp): Handouts.Posix.
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    47
                                                  //| ALT; def |(r: String): Handouts.Posix.ALT; def %: Handouts.Posix.STAR; def 
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    48
                                                  //| ~(r: Handouts.Posix.Rexp): Handouts.Posix.SEQ; def ~(r: String): Handouts.P
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    49
                                                  //| osix.SEQ; def $(r: Handouts.Posix.Rexp): Handouts.Posix.RECD}
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    50
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    51
  // size of a regular expressions - for testing purposes
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    52
  def size(r: Rexp): Int = r match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    53
    case NULL => 1
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    54
    case EMPTY => 1
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    55
    case CHAR(_) => 1
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    56
    case ALT(r1, r2) => 1 + size(r1) + size(r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    57
    case SEQ(r1, r2) => 1 + size(r1) + size(r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    58
    case STAR(r) => 1 + size(r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    59
    case RECD(_, r) => 1 + size(r)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    60
  }                                               //> size: (r: Handouts.Posix.Rexp)Int
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    61
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    62
  // nullable function: tests whether the regular
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    63
  // expression can recognise the empty string
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    64
  def nullable(r: Rexp): Boolean = r match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    65
    case NULL => false
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    66
    case EMPTY => true
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    67
    case CHAR(_) => false
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    68
    case ALT(r1, r2) => nullable(r1) || nullable(r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    69
    case SEQ(r1, r2) => nullable(r1) && nullable(r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    70
    case STAR(_) => true
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    71
    case RECD(_, r1) => nullable(r1)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    72
  }                                               //> nullable: (r: Handouts.Posix.Rexp)Boolean
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    73
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    74
  // derivative of a regular expression w.r.t. a character
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    75
  def der(c: Char, r: Rexp): Rexp = r match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    76
    case NULL => NULL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    77
    case EMPTY => NULL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    78
    case CHAR(d) => if (c == d) EMPTY else NULL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    79
    case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    80
    case SEQ(r1, r2) =>
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    81
      if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    82
      else SEQ(der(c, r1), r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    83
    case STAR(r) => SEQ(der(c, r), STAR(r))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    84
    case RECD(_, r1) => der(c, r1)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    85
  }                                               //> der: (c: Char, r: Handouts.Posix.Rexp)Handouts.Posix.Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    86
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    87
  // derivative w.r.t. a string (iterates der)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    88
  def ders(s: List[Char], r: Rexp): Rexp = s match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    89
    case Nil => r
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    90
    case c :: s => ders(s, der(c, r))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    91
  }                                               //> ders: (s: List[Char], r: Handouts.Posix.Rexp)Handouts.Posix.Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    92
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    93
  // extracts a string from value
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    94
  def flatten(v: Val): String = v match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    95
    case Void => ""
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    96
    case Chr(c) => c.toString
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    97
    case Left(v) => flatten(v)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    98
    case Right(v) => flatten(v)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    99
    case Sequ(v1, v2) => flatten(v1) + flatten(v2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   100
    case Stars(vs) => vs.map(flatten).mkString
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   101
    case Rec(_, v) => flatten(v)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   102
  }                                               //> flatten: (v: Handouts.Posix.Val)String
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   103
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   104
  // extracts an environment from a value
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   105
  def env(v: Val): List[(String, String)] = v match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   106
    case Void => Nil
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   107
    case Chr(c) => Nil
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   108
    case Left(v) => env(v)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   109
    case Right(v) => env(v)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   110
    case Sequ(v1, v2) => env(v1) ::: env(v2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   111
    case Stars(vs) => vs.flatMap(env)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   112
    case Rec(x, v) => (x, flatten(v)) :: env(v)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   113
  }                                               //> env: (v: Handouts.Posix.Val)List[(String, String)]
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   114
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   115
  def mkeps(r: Rexp): Val = r match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   116
    case EMPTY => Void
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   117
    case ALT(r1, r2) =>
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   118
      if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   119
    case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   120
    case STAR(r) => Stars(Nil)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   121
    case RECD(x, r) => Rec(x, mkeps(r))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   122
  }                                               //> mkeps: (r: Handouts.Posix.Rexp)Handouts.Posix.Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   123
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   124
  def inj(r: Rexp, c: Char, v: Val): Val = (r, v) match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   125
    case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1) :: vs)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   126
    case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   127
    case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   128
    case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   129
    case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   130
    case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   131
    case (CHAR(d), Void) => Chr(d)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   132
    case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   133
  }                                               //> inj: (r: Handouts.Posix.Rexp, c: Char, v: Handouts.Posix.Val)Handouts.Posix
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   134
                                                  //| .Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   135
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   136
  // main lexing function (produces a value)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   137
  def lex(r: Rexp, s: List[Char]): Val = s match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   138
    case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   139
    case c :: cs => inj(r, c, lex(der(c, r), cs))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   140
  }                                               //> lex: (r: Handouts.Posix.Rexp, s: List[Char])Handouts.Posix.Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   141
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   142
  def lexing(r: Rexp, s: String): Val = lex(r, s.toList)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   143
                                                  //> lexing: (r: Handouts.Posix.Rexp, s: String)Handouts.Posix.Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   144
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   145
  val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).%
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   146
                                                  //> r  : Handouts.Posix.STAR = STAR(ALT(RECD(1,CHAR(a)),ALT(RECD(2,CHAR(b)),REC
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   147
                                                  //| D(3,SEQ(CHAR(a),CHAR(b))))))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   148
  env(lexing(r, "ba"))                            //> res0: List[(String, String)] = List((2,b), (1,a))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   149
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   150
  val r1 = "a" | "b"                              //> r1  : Handouts.Posix.ALT = ALT(CHAR(a),CHAR(b))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   151
  lexing(r1, "a")                                 //> res1: Handouts.Posix.Val = Left(Chr(a))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   152
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   153
  // Lexing Rules for a Small While Language
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   154
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   155
  def PLUS(r: Rexp) = r ~ r.%                     //> PLUS: (r: Handouts.Posix.Rexp)Handouts.Posix.SEQ
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   156
  val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   157
                                                  //> SYM  : Handouts.Posix.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   158
                                                  //| (ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   159
                                                  //| c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   160
                                                  //| ,CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   161
                                                  //| AR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   162
  val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   163
                                                  //> DIGIT  : Handouts.Posix.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),C
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   164
                                                  //| HAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   165
                                                  //| (9))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   166
  val ID = SYM ~ (SYM | DIGIT).%                  //> ID  : Handouts.Posix.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   167
                                                  //| ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   168
                                                  //| AR(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   169
                                                  //| k)),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   170
                                                  //| ,CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z)),STAR(ALT(AL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   171
                                                  //| T(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(A
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   172
                                                  //| LT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   173
                                                  //| ,CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   174
                                                  //| AR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   175
                                                  //| w)),CHAR(x)),CHAR(y)),CHAR(z)),ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   176
                                                  //| CHAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHA
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   177
                                                  //| R(9)))))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   178
  val NUM = PLUS(DIGIT)                           //> NUM  : Handouts.Posix.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   179
                                                  //| ,CHAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   180
                                                  //| AR(9)),STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),CHAR(1)),CHAR(2)),C
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   181
                                                  //| HAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR(9))))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   182
  val KEYWORD: Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   183
                                                  //> KEYWORD  : Handouts.Posix.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   184
                                                  //| AR(s),SEQ(CHAR(k),SEQ(CHAR(i),CHAR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   185
                                                  //| ),SEQ(CHAR(l),CHAR(e)))))),SEQ(CHAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   186
                                                  //| CHAR(t),SEQ(CHAR(h),SEQ(CHAR(e),CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHA
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   187
                                                  //| R(s),CHAR(e))))),SEQ(CHAR(r),SEQ(CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   188
                                                  //| ),SEQ(CHAR(r),SEQ(CHAR(i),SEQ(CHAR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   189
                                                  //| SEQ(CHAR(u),CHAR(e))))),SEQ(CHAR(f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHA
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   190
                                                  //| R(e))))))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   191
  val SEMI: Rexp = ";"                            //> SEMI  : Handouts.Posix.Rexp = CHAR(;)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   192
  val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   193
                                                  //> OP  : Handouts.Posix.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   194
                                                  //| (CHAR(:),CHAR(=)),SEQ(CHAR(=),CHAR(=))),CHAR(-)),CHAR(+)),CHAR(*)),SEQ(CHAR
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   195
                                                  //| (!),CHAR(=))),CHAR(<)),CHAR(>)),SEQ(CHAR(<),CHAR(=))),SEQ(CHAR(>),CHAR(=)))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   196
                                                  //| ,CHAR(%)),CHAR(/))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   197
  val WHITESPACE = PLUS(" " | "\n" | "\t")        //> WHITESPACE  : Handouts.Posix.SEQ = SEQ(ALT(ALT(CHAR( ),CHAR(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   198
                                                  //| )),CHAR(	)),STAR(ALT(ALT(CHAR( ),CHAR(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   199
                                                  //| )),CHAR(	))))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   200
  val RPAREN: Rexp = ")"                          //> RPAREN  : Handouts.Posix.Rexp = CHAR())
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   201
  val LPAREN: Rexp = "("                          //> LPAREN  : Handouts.Posix.Rexp = CHAR(()
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   202
  val BEGIN: Rexp = "{"                           //> BEGIN  : Handouts.Posix.Rexp = CHAR({)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   203
  val END: Rexp = "}"                             //> END  : Handouts.Posix.Rexp = CHAR(})
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   204
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   205
  /*
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   206
 * val WHILE_REGS = (("k" $ KEYWORD) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   207
                  ("i" $ ID) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   208
                  ("o" $ OP) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   209
                  ("n" $ NUM) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   210
                  ("s" $ SEMI) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   211
                  ("p" $ (LPAREN | RPAREN)) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   212
                  ("b" $ (BEGIN | END)) |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   213
                  ("w" $ WHITESPACE)).%
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   214
*/
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   215
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   216
  val WHILE_REGS = (KEYWORD |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   217
    ID |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   218
    OP |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   219
    NUM |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   220
    SEMI |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   221
    LPAREN | RPAREN |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   222
    BEGIN | END |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   223
    WHITESPACE).%                                 //> WHILE_REGS  : Handouts.Posix.STAR = STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(AL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   224
                                                  //| T(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CHAR(s),SEQ(CHAR(k),SEQ(CHAR(i),C
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   225
                                                  //| HAR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i),SEQ(CHAR(l),CHAR(e)))))),SEQ(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   226
                                                  //| CHAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(CHAR(t),SEQ(CHAR(h),SEQ(CHAR(e)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   227
                                                  //| ,CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e))))),SEQ(CHAR(r),SE
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   228
                                                  //| Q(CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w),SEQ(CHAR(r),SEQ(CHAR(i),SEQ(C
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   229
                                                  //| HAR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),SEQ(CHAR(u),CHAR(e))))),SEQ(CHA
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   230
                                                  //| R(f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e)))))),SEQ(ALT(ALT(ALT(ALT(A
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   231
                                                  //| LT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   232
                                                  //| ALT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   233
                                                  //| h)),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   234
                                                  //| ,CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   235
                                                  //| AR(y)),CHAR(z)),STAR(AL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   236
                                                  //| Output exceeds cutoff limit.
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   237
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   238
  // Some Tests
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   239
  //============
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   240
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   241
  def time[T](code: => T) = {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   242
    val start = System.nanoTime()
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   243
    val result = code
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   244
    val end = System.nanoTime()
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   245
    println((end - start) / 1.0e9)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   246
    result
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   247
  }                                               //> time: [T](code: => T)T
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   248
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   249
  val abc = List('a', 'b', 'c')                   //> abc  : List[Char] = List(a, b, c)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   250
  val nullRexp = null                             //> nullRexp  : Null = null
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   251
  val myRexp = charlist2rexp(abc)                 //> myRexp  : Handouts.Posix.Rexp = SEQ(CHAR(a),SEQ(CHAR(b),CHAR(c)))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   252
  val myRexp2 = string2rexp("FahadAusaf")         //> myRexp2  : Handouts.Posix.Rexp = SEQ(CHAR(F),SEQ(CHAR(a),SEQ(CHAR(h),SEQ(CH
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   253
                                                  //| AR(a),SEQ(CHAR(d),SEQ(CHAR(A),SEQ(CHAR(u),SEQ(CHAR(s),SEQ(CHAR(a),CHAR(f)))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   254
                                                  //| )))))))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   255
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   256
  RexpOps(myRexp2)                                //> res2: AnyRef{def |(s: Handouts.Posix.Rexp): Handouts.Posix.ALT; def %: Hand
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   257
                                                  //| outs.Posix.STAR; def ~(s: Handouts.Posix.Rexp): Handouts.Posix.SEQ} = Hando
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   258
                                                  //| uts.Posix$$anonfun$main$1$$anon$1@6ee3572b
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   259
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   260
  stringOps("Fahad")                              //> res3: AnyRef{def |(r: Handouts.Posix.Rexp): Handouts.Posix.ALT; def |(r: St
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   261
                                                  //| ring): Handouts.Posix.ALT; def %: Handouts.Posix.STAR; def ~(r: Handouts.Po
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   262
                                                  //| six.Rexp): Handouts.Posix.SEQ; def ~(r: String): Handouts.Posix.SEQ; def $(
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   263
                                                  //| r: Handouts.Posix.Rexp): Handouts.Posix.RECD} = Handouts.Posix$$anonfun$mai
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   264
                                                  //| n$1$$anon$2@f7000ed
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   265
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   266
  size(myRexp2)                                   //> res4: Int = 19
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   267
  nullable(nullRexp)                              //> scala.MatchError: null
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   268
                                                  //| 	at Handouts.Posix$$anonfun$main$1.nullable$1(Handouts.Posix.scala:58)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   269
                                                  //| 	at Handouts.Posix$$anonfun$main$1.apply$mcV$sp(Handouts.Posix.scala:200)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   270
                                                  //| 
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   271
                                                  //| 	at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   272
                                                  //| cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   273
                                                  //| 	at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   274
                                                  //| orksheetSupport.scala:65)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   275
                                                  //| 	at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   276
                                                  //| ksheetSupport.scala:75)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   277
                                                  //| 	at Handouts.Posix$.main(Handouts.Posix.scala:21)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   278
                                                  //| 	at Handouts.Posix.main(Handouts.Posix.scala)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   279
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   280
  val newRexp = der('a', myRexp)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   281
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   282
  //this is some crap
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   283
}