solutions/cw3/lexer.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 22 Nov 2024 12:42:07 +0000
changeset 974 0cb4bf2469d1
parent 961 c0600f8b6427
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
     1
// Lexer from CW2
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
     2
//================
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
     3
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     4
//> using toolkit 0.4.0
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     5
//> using file project.sc
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     6
import project.*
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
abstract class Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case object ZERO extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
case object ONE extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
case class CHAR(c: Char) extends Rexp
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    13
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    14
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    15
case class STAR(r: Rexp) extends Rexp
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case class RECD(x: String, r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class RANGE(s: Set[Char]) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class PLUS(r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class OPTIONAL(r: Rexp) extends Rexp
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    21
case class NTIMES(r: Rexp, n: Int) extends Rexp
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
// Values
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
abstract class Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case object Empty extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class Chr(c: Char) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class Sequ(v1: Val, v2: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class Left(v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
case class Right(v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
case class Stars(vs: List[Val]) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
case class Rec(x: String, v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    34
// Convenience for typing
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
def charlist2rexp(s : List[Char]): Rexp = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  case Nil => ONE
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
  case c::Nil => CHAR(c)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    41
implicit def string2rexp(s : String) : Rexp =
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  charlist2rexp(s.toList)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    44
extension (r: Rexp) {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
  def | (s: Rexp) = ALT(r, s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  def % = STAR(r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  def ~ (s: Rexp) = SEQ(r, s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    50
extension (s: String) {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
  def | (r: Rexp) = ALT(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  def | (r: String) = ALT(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
  def % = STAR(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  def ~ (r: Rexp) = SEQ(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  def ~ (r: String) = SEQ(s, r)
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    56
  infix def $ (r: Rexp) = RECD(s, r)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
// nullable
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
def nullable(r: Rexp) : Boolean = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case ZERO => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  case ONE => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
  case CHAR(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case STAR(_) => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case RECD(_, r1) => nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  case RANGE(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  case PLUS(r1) => nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  case OPTIONAL(_) => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  case NTIMES(r1, i) => if (i == 0) true else nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
// der
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
def der(c: Char, r: Rexp) : Rexp = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  case ZERO => ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  case ONE => ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  case CHAR(d) => if (c == d) ONE else ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    81
  case SEQ(r1, r2) =>
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
    else SEQ(der(c, r1), r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  case RECD(_, r1) => der(c, r1)
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    87
  case RANGE(s) => if (s.contains(c)) ONE else ZERO
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
  case PLUS(r1) => SEQ(der(c, r1), STAR(r1))
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    89
  case OPTIONAL(r1) => der(c, r1)
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    90
  case NTIMES(r, i) =>
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
    if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
// Flatten
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
def flatten(v: Val) : String = v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  case Empty => ""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  case Chr(c) => c.toString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  case Left(v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  case Right(v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  case Stars(vs) => vs.map(flatten).mkString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
  case Rec(_, v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
// Env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
def env(v: Val) : List[(String, String)] = v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  case Empty => Nil
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case Chr(c) => Nil
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case Left(v) => env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  case Right(v) => env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  case Sequ(v1, v2) => env(v1) ::: env(v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  case Stars(vs) => vs.flatMap(env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
  case Rec(x, v) => (x, flatten(v))::env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
// Mkeps
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
def mkeps(r: Rexp) : Val = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  case ONE => Empty
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   119
  case ALT(r1, r2) =>
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case STAR(r) => Stars(Nil)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  case RECD(x, r) => Rec(x, mkeps(r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  case PLUS(r) => Stars(List(mkeps(r)))   // the first copy must match the empty string
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   126
  case OPTIONAL(r) => if (nullable(r)) Stars(List(mkeps(r))) else Stars(Nil)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case NTIMES(r, i) => Stars(List.fill(i)(mkeps(r)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
// Inj
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   138
  case (CHAR(d), Empty) => Chr(c)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  case (RANGE(_), Empty) => Chr(c)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   143
  case (OPTIONAL(r), v1) => Stars(List(inj(r, c, v1)))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
// Rectification functions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
def F_ID(v: Val): Val = v
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  case Right(v) => Right(f2(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  case Left(v) => Left(f1(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
}
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   158
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) =
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  (v:Val) => Sequ(f1(Empty), f2(v))
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   160
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) =
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
  (v:Val) => Sequ(f1(v), f2(Empty))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
def F_RECD(f: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
  case Rec(x, v) => Rec(x, f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
def F_ERROR(v: Val): Val = throw new Exception("error")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
// Simp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
def simp(r: Rexp): (Rexp, Val => Val) = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  case ALT(r1, r2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
    val (r1s, f1s) = simp(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
    val (r2s, f2s) = simp(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
    (r1s, r2s) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
      case (_, ZERO) => (r1s, F_LEFT(f1s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   176
                else (ALT (r1s, r2s), F_ALT(f1s, f2s))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  case SEQ(r1, r2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
    val (r1s, f1s) = simp(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
    val (r2s, f2s) = simp(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
    (r1s, r2s) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
      case (ZERO, _) => (ZERO, F_ERROR)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
      case (_, ZERO) => (ZERO, F_ERROR)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  case r => (r, F_ID)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
// Lex
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   195
  case Nil => if (nullable(r)) mkeps(r) else
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   196
    { throw new Exception("lexing error") }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  case c::cs => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
    val (r_simp, f_simp) = simp(der(c, r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
// Language specific code
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   206
val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip"
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||"
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   208
val LET: Rexp = RANGE(('A' to 'Z').toSet ++ ('a' to 'z').toSet)
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   209
val SYM : Rexp = RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':', ')', '('))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
val PARENS : Rexp = "(" | "{" | ")" | "}"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
val SEMI : Rexp = ";"
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   212
val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
val DIGIT : Rexp = RANGE(('0' to '9').toSet)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   215
val STRING : Rexp = "\"" ~ (LET | SYM | DIGIT | " " | "\\n").% ~ "\""
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
val ID : Rexp = LET ~ (LET | "_" | DIGIT).%
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%)
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
   218
val EOL : Rexp = "\n" | "\r\n"
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   219
val COMMENT : Rexp = "//" ~ (LET | SYM | PARENS | " " | DIGIT).% ~ EOL
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   221
val WHILE_REGS = (("k" $ KEYWORD) |
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   222
                  ("o" $ OP) |
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
                  ("str" $ STRING) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
                  ("p" $ PARENS) |
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   225
                  ("s" $ SEMI) |
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   226
                  ("w" $ WHITESPACE) |
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   227
                  ("i" $ ID) |
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
                  ("n" $ NUM) |
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   229
                  ("c" $ COMMENT)).%
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   230
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   231
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   233
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   234
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   235
def escapedChar(ch: Char): String = ch match {
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   236
  case '\b' => "\\b"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   237
  case '\t' => "\\t"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   238
  case '\n' => "\\n"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   239
  case '\f' => "\\f"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   240
  case '\r' => "\\r"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   241
  case '"'  => "\\\""
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   242
  case '\'' => "\\\'"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   243
  case '\\' => "\\\\"
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   244
  case _    => if (ch.isControl) "\\0" + Integer.toOctalString(ch.toInt)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   245
               else              String.valueOf(ch)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   246
}
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   247
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   248
def esc(s: String): String = "\"" + escapeImpl(s) + "\""
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   249
def escapeImpl(s: String): String = s.flatMap(escapedChar)
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   250
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   251
/*
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   252
def esc(raw: String): String = {
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   253
  import scala.reflect.runtime.universe._
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   254
  Literal(Constant(raw)).toString
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   255
}
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   256
*/
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   257
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   258
def escape(tks: List[(String, String)]) =
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   259
  tks.map{ case (s1, s2) => (s1, s2)}
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   260
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   261
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   262
// Tokens
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   263
abstract class Token extends Serializable
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
case class T_KEYWORD(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
case class T_OP(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
case class T_STRING(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
case class T_PAREN(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
case object T_SEMI extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
case class T_ID(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
case class T_NUM(n: Int) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
val token : PartialFunction[(String, String), Token] = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
  case ("k", s) => T_KEYWORD(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
  case ("o", s) => T_OP(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
  case ("str", s) => T_STRING(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
  case ("p", s) => T_PAREN(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
  case ("s", _) => T_SEMI
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
  case ("i", s) => T_ID(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
  case ("n", s) => T_NUM(s.toInt)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
// Tokenise
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   283
def tokenise(s: String) = //: List[Token] =
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   284
  escape(lexing_simp(WHILE_REGS, s)).collect(token)
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   285
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   286
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   287
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   288
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   289
println(tokenise(os.read(os.pwd / "primes.while")))