progs/token.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 16 Oct 2015 08:42:21 +0100
changeset 352 1e1b0fe66107
parent 165 66b699c80479
child 354 86b2aeae3e98
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     1
import scala.language.implicitConversions    
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     3
import scala.annotation.tailrec   
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     5
abstract class Rexp 
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object NULL extends Rexp
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case object EMPTY extends Rexp
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    12
case class RECD(x: String, r: Rexp) extends Rexp
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    14
abstract class Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    15
case object Void extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    16
case class Chr(c: Char) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    17
case class Sequ(v1: Val, v2: Val) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    18
case class Left(v: Val) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    19
case class Right(v: Val) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    20
case class Stars(vs: List[Val]) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    21
case class Rec(x: String, v: Val) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    22
   
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    23
// some convenience for typing in regular expressions
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    24
def charlist2rexp(s : List[Char]): Rexp = s match {
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  case Nil => EMPTY
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  case c::Nil => CHAR(c)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
implicit def RexpOps(r: Rexp) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def | (s: Rexp) = ALT(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def % = STAR(r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  def ~ (s: Rexp) = SEQ(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
implicit def stringOps(s: String) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def | (r: Rexp) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def | (r: String) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def % = STAR(s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def ~ (r: Rexp) = SEQ(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def ~ (r: String) = SEQ(s, r)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    43
  def $ (r: Rexp) = RECD(s, r)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    46
// nullable function: tests whether the regular 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    47
// expression can recognise the empty string
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
def nullable (r: Rexp) : Boolean = r match {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  case NULL => false
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  case EMPTY => true
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  case CHAR(_) => false
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case STAR(_) => true
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    55
  case RECD(_, r1) => nullable(r1)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    58
// derivative of a regular expression w.r.t. a character
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
def der (c: Char, r: Rexp) : Rexp = r match {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case NULL => NULL
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    61
  case EMPTY => NULL
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  case CHAR(d) => if (c == d) EMPTY else NULL
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  case SEQ(r1, r2) => 
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    else SEQ(der(c, r1), r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  case STAR(r) => SEQ(der(c, r), STAR(r))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    68
  case RECD(_, r1) => der(c, r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    69
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    70
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    71
// derivative w.r.t. a string (iterates der)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    72
def ders (s: List[Char], r: Rexp) : Rexp = s match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    73
  case Nil => r
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    74
  case c::s => ders(s, der(c, r))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    75
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    76
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    77
// extracts a string from value
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    78
def flatten(v: Val) : String = v match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    79
  case Void => ""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    80
  case Chr(c) => c.toString
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    81
  case Left(v) => flatten(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    82
  case Right(v) => flatten(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    83
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    84
  case Stars(vs) => vs.map(flatten).mkString
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    85
  case Rec(_, v) => flatten(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    86
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    87
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    88
// extracts an environment from a value
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    89
def env(v: Val) : List[(String, String)] = v match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    90
  case Void => Nil
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    91
  case Chr(c) => Nil
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    92
  case Left(v) => env(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    93
  case Right(v) => env(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    94
  case Sequ(v1, v2) => env(v1) ::: env(v2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    95
  case Stars(vs) => vs.flatMap(env)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    96
  case Rec(x, v) => (x, flatten(v))::env(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    97
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    98
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    99
// injection part
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   100
def mkeps(r: Rexp) : Val = r match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   101
  case EMPTY => Void
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   102
  case ALT(r1, r2) => 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   103
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   104
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   105
  case STAR(r) => Stars(Nil)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   106
  case RECD(x, r) => Rec(x, mkeps(r))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   110
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   111
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   112
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   113
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   114
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   115
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   116
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   117
  case (CHAR(d), Void) => Chr(c) 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   118
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   119
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   120
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   121
// main lexing function (produces a value)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   122
def lex(r: Rexp, s: List[Char]) : Val = s match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   123
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   124
  case c::cs => inj(r, c, lex(der(c, r), cs))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   125
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   126
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   127
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   128
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   129
// some "rectification" functions for simplification
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   130
def F_ID(v: Val): Val = v
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   131
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   132
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   133
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   134
  case Right(v) => Right(f2(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   135
  case Left(v) => Left(f1(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   136
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   137
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   138
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   139
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   140
def F_SEQ_Void1(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(Void), f2(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   141
def F_SEQ_Void2(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(v), f2(Void))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   142
def F_RECD(f: Val => Val) = (v:Val) => v match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   143
  case Rec(x, v) => Rec(x, f(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   144
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   145
def F_ERROR(v: Val): Val = throw new Exception("error")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   146
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   147
// simplification of regular expressions returning also an
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   148
// rectification function; no simplification under STAR 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   149
def simp(r: Rexp): (Rexp, Val => Val) = r match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   150
  case ALT(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   151
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   152
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   153
    (r1s, r2s) match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   154
      case (NULL, _) => (r2s, F_RIGHT(f2s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   155
      case (_, NULL) => (r1s, F_LEFT(f1s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   156
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   157
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   158
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   159
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   160
  case SEQ(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   161
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   162
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   163
    (r1s, r2s) match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   164
      case (NULL, _) => (NULL, F_ERROR)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   165
      case (_, NULL) => (NULL, F_ERROR)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   166
      case (EMPTY, _) => (r2s, F_SEQ_Void1(f1s, f2s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   167
      case (_, EMPTY) => (r1s, F_SEQ_Void2(f1s, f2s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   168
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   169
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   170
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   171
  case RECD(x, r1) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   172
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   173
    (RECD(x, r1s), F_RECD(f1s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   174
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   175
  case r => (r, F_ID)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   176
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   177
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   178
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   179
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   180
  case c::cs => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   181
    val (r_simp, f_simp) = simp(der(c, r))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   182
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
  }
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   186
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   187
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   188
lexing_simp(("a" + "ab") | ("b" + ""), "ab")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   189
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   190
// Lexing Rules for a Small While Language
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   191
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   192
def PLUS(r: Rexp) = r ~ r.%
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   193
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   194
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   195
val ID = SYM ~ (SYM | DIGIT).% 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   196
val NUM = PLUS(DIGIT)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   197
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   198
val SEMI: Rexp = ";"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   199
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   200
val WHITESPACE = PLUS(" " | "\n" | "\t")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   201
val RPAREN: Rexp = ")"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   202
val LPAREN: Rexp = "("
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   203
val BEGIN: Rexp = "{"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   204
val END: Rexp = "}"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   205
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   206
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   207
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   208
val WHILE_REGS = (("k" $ KEYWORD) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   209
                  ("i" $ ID) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   210
                  ("o" $ OP) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   211
                  ("n" $ NUM) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   212
                  ("s" $ SEMI) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   213
                  ("str" $ STRING) |
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   214
                  ("p" $ (LPAREN | RPAREN)) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   215
                  ("b" $ (BEGIN | END)) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   216
                  ("w" $ WHITESPACE)).%
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   217
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   218
//   Testing
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   219
//============
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   220
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   221
def time[T](code: => T) = {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   222
  val start = System.nanoTime()
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   223
  val result = code
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   224
  val end = System.nanoTime()
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   225
  println((end - start)/1.0e9)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   226
  result
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   227
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   228
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   229
val r1 = ("a" | "ab") ~ ("bcd" | "c")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   230
println(lexing(r1, "abcd"))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   231
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   232
val r2 = ("" | "a") ~ ("ab" | "b")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   233
println(lexing(r2, "ab"))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   234
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   235
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   236
// Two Simple While Tests
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   237
//========================
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   238
println("prog0 test")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   239
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   240
val prog0 = """read n"""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   241
println(env(lexing_simp(WHILE_REGS, prog0)))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   242
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   243
println("prog1 test")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   244
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   245
val prog1 = """read  n; write (n)"""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   246
println(env(lexing_simp(WHILE_REGS, prog1)))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   247
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   248
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   249
// Big Test
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   250
//==========
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   251
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   252
val prog2 = """
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   253
write "fib";
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   254
read n;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   255
minus1 := 0;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   256
minus2 := 1;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   257
while n > 0 do {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   258
  temp := minus2;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   259
  minus2 := minus1 + minus2;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   260
  minus1 := temp;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   261
  n := n - 1
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   262
};
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   263
write "result";
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   264
write minus2
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   265
"""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   266
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   267
println("Tokens")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   268
println(env(lexing_simp(WHILE_REGS, prog2)))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   269
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   270
for (i <- 1 to 80) {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   271
  print(i.toString + ":  ")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   272
  time(lexing_simp(WHILE_REGS, prog2 * i))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
165
66b699c80479 tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 164
diff changeset
   275