solution/cw2/lexer.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 15 Dec 2021 19:00:01 +0000
changeset 864 b5b1bc0a603b
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
import scala.language.implicitConversions    
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
abstract class Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
case object ZERO extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
case object ONE extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
case class RECD(x: String, r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
case class RANGE(s: Set[Char]) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
case class PLUS(r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case class OPTIONAL(r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case class NTIMES(r: Rexp, n: Int) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
// Values
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
abstract class Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case object Empty extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class Chr(c: Char) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case class Sequ(v1: Val, v2: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case class Left(v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class Right(v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class Stars(vs: List[Val]) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class Rec(x: String, v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
// Convenience typing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
def charlist2rexp(s : List[Char]): Rexp = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  case Nil => ONE
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  case c::Nil => CHAR(c)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
implicit def string2rexp(s : String) : Rexp = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  charlist2rexp(s.toList)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
implicit def RexpOps(r: Rexp) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  def | (s: Rexp) = ALT(r, s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  def % = STAR(r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  def ~ (s: Rexp) = SEQ(r, s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
implicit def stringOps(s: String) = new {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  def | (r: Rexp) = ALT(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  def | (r: String) = ALT(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  def % = STAR(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  def ~ (r: Rexp) = SEQ(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
  def ~ (r: String) = SEQ(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  def $ (r: Rexp) = RECD(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
// nullable
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
def nullable(r: Rexp) : Boolean = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
  case ZERO => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case ONE => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case CHAR(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  case STAR(_) => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  case RECD(_, r1) => nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  case RANGE(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case PLUS(r1) => nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  case OPTIONAL(_) => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case NTIMES(r1, i) => if (i == 0) true else nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
// der
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
def der(c: Char, r: Rexp) : Rexp = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  case ZERO => ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  case ONE => ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  case CHAR(d) => if (c == d) ONE else ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  case SEQ(r1, r2) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
    else SEQ(der(c, r1), r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  case RECD(_, r1) => der(c, r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  case RANGE(s) => if (s.contains(c)) ONE else ZERO 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  case PLUS(r1) => SEQ(der(c, r1), STAR(r1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  case OPTIONAL(r1) => der(c, r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  case NTIMES(r, i) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
    if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// Flatten
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
def flatten(v: Val) : String = v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  case Empty => ""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  case Chr(c) => c.toString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
  case Left(v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  case Right(v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  case Stars(vs) => vs.map(flatten).mkString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  case Rec(_, v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
// Env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
def env(v: Val) : List[(String, String)] = v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  case Empty => Nil
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case Chr(c) => Nil
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case Left(v) => env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case Right(v) => env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  case Sequ(v1, v2) => env(v1) ::: env(v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case Stars(vs) => vs.flatMap(env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case Rec(x, v) => (x, flatten(v))::env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
// Mkeps
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
def mkeps(r: Rexp) : Val = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  case ONE => Empty
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
  case ALT(r1, r2) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  case STAR(r) => Stars(Nil)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case RECD(x, r) => Rec(x, mkeps(r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case PLUS(r) => Stars(List(mkeps(r)))   // the first copy must match the empty string
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case OPTIONAL(r) => if (nullable(r)) Stars(List(mkeps(r))) else Stars(Nil)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  case NTIMES(r, i) => Stars(List.fill(i)(mkeps(r)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
// Inj
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  case (CHAR(d), Empty) => Chr(c) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  case (RANGE(_), Empty) => Chr(c)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  case (OPTIONAL(r), v1) => Stars(List(inj(r, c, v1)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
  case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
// Rectification functions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
def F_ID(v: Val): Val = v
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
  case Right(v) => Right(f2(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  case Left(v) => Left(f1(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
  (v:Val) => Sequ(f1(Empty), f2(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  (v:Val) => Sequ(f1(v), f2(Empty))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
def F_RECD(f: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  case Rec(x, v) => Rec(x, f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
def F_ERROR(v: Val): Val = throw new Exception("error")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
// Simp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
def simp(r: Rexp): (Rexp, Val => Val) = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
  case ALT(r1, r2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
    val (r1s, f1s) = simp(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
    val (r2s, f2s) = simp(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    (r1s, r2s) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
      case (_, ZERO) => (r1s, F_LEFT(f1s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  case SEQ(r1, r2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
    val (r1s, f1s) = simp(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
    val (r2s, f2s) = simp(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
    (r1s, r2s) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
      case (ZERO, _) => (ZERO, F_ERROR)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
      case (_, ZERO) => (ZERO, F_ERROR)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  case r => (r, F_ID)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
// Lex
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  case Nil => if (nullable(r)) mkeps(r) else 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
    { throw new Exception("lexing error") } 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
  case c::cs => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
    val (r_simp, f_simp) = simp(der(c, r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
// Language specific code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip" 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
val LET: Rexp = RANGE(('A' to 'Z').toSet ++ ('a' to 'z'))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
val SYM : Rexp = LET | RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':', ')', '('))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
val PARENS : Rexp = "(" | "{" | ")" | "}"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
val SEMI : Rexp = ";"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
val DIGIT : Rexp = RANGE(('0' to '9').toSet)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
val STRING : Rexp = "\"" ~ (SYM | " " | "\\n" | DIGIT).% ~ "\""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
val ID : Rexp = LET ~ (LET | "_" | DIGIT).%
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
val COMMENT : Rexp = "//" ~ (SYM | " " | DIGIT).% ~ ("\n" | "\r\n") 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
val WHILE_REGS = (("k" $ KEYWORD) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
                  ("o" $ OP) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
                  ("str" $ STRING) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
                  ("p" $ PARENS) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
                  ("s" $ SEMI) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
                  ("w" $ WHITESPACE) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
                  ("i" $ ID) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
                  ("n" $ NUM) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
		  ("c" $ COMMENT)).%
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
def esc(raw: String): String = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
  import scala.reflect.runtime.universe._
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  Literal(Constant(raw)).toString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
def escape(tks: List[(String, String)]) =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
  tks.map{ case (s1, s2) => (s1, esc(s2))}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
// Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
abstract class Token extends Serializable 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
case class T_KEYWORD(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
case class T_OP(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
case class T_STRING(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
case class T_PAREN(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
case object T_SEMI extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
case class T_ID(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
case class T_NUM(n: Int) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
val token : PartialFunction[(String, String), Token] = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
  case ("k", s) => T_KEYWORD(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
  case ("o", s) => T_OP(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
  case ("str", s) => T_STRING(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
  case ("p", s) => T_PAREN(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
  case ("s", _) => T_SEMI
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
  case ("i", s) => T_ID(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
  case ("n", s) => T_NUM(s.toInt)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
// Tokenise
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
def tokenise(s: String) : List[Token] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
  lexing_simp(WHILE_REGS, s).collect(token)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
// Q2 Tests
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
lex_simp(NTIMES("a", 3), "aaa".toList)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
lex_simp(NTIMES(("a" | ONE), 3), "aa".toList)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
// Q3 Programs
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
val prog1 = """write "Fib";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
read n;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
minus1 := 0;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
minus2 := 1;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
while n > 0 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
temp := minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
minus2 := minus1 + minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
minus1 := temp;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
n := n - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
write "Result";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
write minus2"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
val prog2 = """start := 1000;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
x := start;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
y := start;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
z := start;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
while 0 < x do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
while 0 < y do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
while 0 < z do { z := z - 1 };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
z := start;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   286
y := y - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
y := start;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   289
x := x - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   290
}"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   291
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
val prog3 = """write "Input n please";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   293
read n;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
write "The factors of n are";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
f := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   296
while n != 1 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   297
while (n / f) * f == n do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   298
write f;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   299
n := n / f
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   300
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   301
f := f + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   302
}"""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   303
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   304
println(tokenise(prog1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   305
println(tokenise(prog2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   306
println(tokenise(prog3))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   307
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   308
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   309
println("MY TESTS")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   310
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   311
println(lex_simp("x" $ OPTIONAL("a"), "a".toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   312
println(lex_simp("x" $ OPTIONAL("a"), "".toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   313
println(lex_simp("x" $ NTIMES(OPTIONAL("a"),4), "aa".toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   314
println(lex_simp("x" $ OPTIONAL("aa"), "aa".toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   315
println(lex_simp("x" $ OPTIONAL("aa"), "".toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   316
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   317
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   318
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   319
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   320
//===================
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   321
println("Fib")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   322
println(tokenise(os.read(os.pwd / "fib.while")))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   323
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   324
println("Factors")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   325
println(tokenise(os.read(os.pwd / "factors.while")))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   326
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   327
println("Loops")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   328
println(tokenise(os.read(os.pwd / "loops.while")))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   329
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   330
println("Collatz")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   331
println(tokenise(os.read(os.pwd / "collatz.while")))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   332
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   333
println("Collatz 2")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   334
println(tokenise(os.read(os.pwd / "collatz2.while")))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   335
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   336
println("Primes")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   337
println(tokenise(os.read(os.pwd / "primes.while")))