solutions/cw3/lexer.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 30 Oct 2023 12:58:45 +0000
changeset 949 61797848eede
parent 920 7af2eea19646
child 959 64ec1884d860
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
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
// Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
abstract class Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
case object ZERO extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
case object ONE extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case class CHAR(c: Char) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
case class STAR(r: Rexp) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
case class RECD(x: String, r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
case class RANGE(s: Set[Char]) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case class PLUS(r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case class OPTIONAL(r: Rexp) extends Rexp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class NTIMES(r: Rexp, n: Int) extends Rexp 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// Values
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
abstract class Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case object Empty extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case class Chr(c: Char) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case class Sequ(v1: Val, v2: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class Left(v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class Right(v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class Stars(vs: List[Val]) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class Rec(x: String, v: Val) extends Val
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    31
// Convenience for typing
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
def charlist2rexp(s : List[Char]): Rexp = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  case Nil => ONE
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  case c::Nil => CHAR(c)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
implicit def string2rexp(s : String) : Rexp = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  charlist2rexp(s.toList)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    41
extension (r: Rexp) {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  def | (s: Rexp) = ALT(r, s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  def % = STAR(r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  def ~ (s: Rexp) = SEQ(r, s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    47
extension (s: String) {
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  def | (r: Rexp) = ALT(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  def | (r: String) = ALT(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  def % = STAR(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
  def ~ (r: Rexp) = SEQ(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  def ~ (r: String) = SEQ(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
  def $ (r: Rexp) = RECD(s, r)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
// nullable
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
def nullable(r: Rexp) : Boolean = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case ZERO => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case ONE => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  case CHAR(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
  case STAR(_) => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  case RECD(_, r1) => nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case RANGE(_) => false
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  case PLUS(r1) => nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case OPTIONAL(_) => true
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  case NTIMES(r1, i) => if (i == 0) true else nullable(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
// der
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
def der(c: Char, r: Rexp) : Rexp = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  case ZERO => ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  case ONE => ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
  case CHAR(d) => if (c == d) ONE else ZERO
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  case SEQ(r1, r2) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
    else SEQ(der(c, r1), r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  case RECD(_, r1) => der(c, r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  case RANGE(s) => if (s.contains(c)) ONE else ZERO 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  case PLUS(r1) => SEQ(der(c, r1), STAR(r1))
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    86
  case OPTIONAL(r1) => der(c, r1)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  case NTIMES(r, i) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
    if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// Flatten
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
def flatten(v: Val) : String = v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  case Empty => ""
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
  case Chr(c) => c.toString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  case Left(v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  case Right(v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  case Stars(vs) => vs.map(flatten).mkString
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  case Rec(_, v) => flatten(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
// Env
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
def env(v: Val) : List[(String, String)] = v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case Empty => Nil
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case Chr(c) => Nil
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case Left(v) => env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  case Right(v) => env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case Sequ(v1, v2) => env(v1) ::: env(v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case Stars(vs) => vs.flatMap(env)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  case Rec(x, v) => (x, flatten(v))::env(v)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
// Mkeps
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
def mkeps(r: Rexp) : Val = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
  case ONE => Empty
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  case ALT(r1, r2) => 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case STAR(r) => Stars(Nil)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case RECD(x, r) => Rec(x, mkeps(r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  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
   123
  case OPTIONAL(r) => if (nullable(r)) Stars(List(mkeps(r))) else Stars(Nil)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  case NTIMES(r, i) => Stars(List.fill(i)(mkeps(r)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
// Inj
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  case (CHAR(d), Empty) => Chr(c) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  case (RANGE(_), Empty) => Chr(c)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  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
   140
  case (OPTIONAL(r), v1) => Stars(List(inj(r, c, v1)))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
// Rectification functions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
def F_ID(v: Val): Val = v
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  case Right(v) => Right(f2(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
  case Left(v) => Left(f1(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
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_Empty1(f1: Val => Val, f2: Val => Val) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  (v:Val) => Sequ(f1(Empty), f2(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
  (v:Val) => Sequ(f1(v), f2(Empty))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
def F_RECD(f: Val => Val) = (v:Val) => v match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
  case Rec(x, v) => Rec(x, f(v))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
def F_ERROR(v: Val): Val = throw new Exception("error")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
// Simp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
def simp(r: Rexp): (Rexp, Val => Val) = r match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  case ALT(r1, r2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
    val (r1s, f1s) = simp(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    val (r2s, f2s) = simp(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
    (r1s, r2s) match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
      case (_, ZERO) => (r1s, F_LEFT(f1s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  case SEQ(r1, r2) => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
    val (r1s, f1s) = simp(r1)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
    val (r2s, f2s) = simp(r2)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
    (r1s, r2s) match {
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 (_, ZERO) => (ZERO, F_ERROR)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
  case r => (r, F_ID)
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
// Lex
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
  case Nil => if (nullable(r)) mkeps(r) else 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
    { throw new Exception("lexing error") } 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
  case c::cs => {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
    val (r_simp, f_simp) = simp(der(c, r))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
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
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
// Language specific code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
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
   204
val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||"
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   205
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
   206
val SYM : Rexp = RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':', ')', '('))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
val PARENS : Rexp = "(" | "{" | ")" | "}"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
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
   209
val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
val DIGIT : Rexp = RANGE(('0' to '9').toSet)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   212
val STRING : Rexp = "\"" ~ (LET | SYM | DIGIT | " " | "\\n").% ~ "\""
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
val ID : Rexp = LET ~ (LET | "_" | DIGIT).%
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
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
   215
val EOL : Rexp = "\n" | "\r\n"
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   216
val COMMENT : Rexp = "//" ~ (LET | SYM | PARENS | " " | DIGIT).% ~ EOL 
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
val WHILE_REGS = (("k" $ KEYWORD) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
                  ("o" $ OP) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
                  ("str" $ STRING) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
                  ("p" $ PARENS) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
                  ("s" $ SEMI) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
                  ("w" $ WHITESPACE) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
                  ("i" $ ID) | 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
                  ("n" $ NUM) |
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
		  ("c" $ COMMENT)).%
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   228
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   229
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   230
def esc(raw: String): String = {
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   231
  import scala.reflect.runtime.universe._
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   232
  Literal(Constant(raw)).toString
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
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   235
def escape(tks: List[(String, String)]) =
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   236
  tks.map{ case (s1, s2) => (s1, esc(s2))}
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   237
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   238
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   239
// Tokens
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
abstract class Token extends Serializable 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
case class T_KEYWORD(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
case class T_OP(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
case class T_STRING(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
case class T_PAREN(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
case object T_SEMI extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
case class T_ID(s: String) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
case class T_NUM(n: Int) extends Token
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
val token : PartialFunction[(String, String), Token] = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
  case ("k", s) => T_KEYWORD(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
  case ("o", s) => T_OP(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
  case ("str", s) => T_STRING(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
  case ("p", s) => T_PAREN(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
  case ("s", _) => T_SEMI
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
  case ("i", s) => T_ID(s)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
  case ("n", s) => T_NUM(s.toInt)
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
// Tokenise
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
def tokenise(s: String) : List[Token] = 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
  lexing_simp(WHILE_REGS, s).collect(token)