progs/token.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 05 Dec 2017 13:49:47 +0000
changeset 542 37a3db7cd655
parent 529 5c28e4134ee1
child 549 352d15782d35
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
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     4
abstract class Rexp 
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
     5
case object ZERO extends Rexp
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
     6
case object ONE extends Rexp
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
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
     8
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
     9
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
    10
case class STAR(r: Rexp) extends Rexp 
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
    11
case class RECD[A](f: String => A, r: Rexp) extends Rexp
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    13
abstract class Val
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    14
case object Empty extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    15
case class Chr(c: Char) extends Val
520
2849c305b12d updated
cu
parents: 450
diff changeset
    16
case class Sequ(v1: Val, v2: Val) extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    17
case class Left(v: Val) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    18
case class Right(v: Val) extends Val
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    19
case class Stars(vs: List[Val]) extends Val
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
    20
case class Rec[A](f: String => A, v: Val) extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    21
   
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    22
// some convenience for typing in regular expressions
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    23
def charlist2rexp(s : List[Char]): Rexp = s match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    24
  case Nil => ONE
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  case c::Nil => CHAR(c)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  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
    27
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
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
    29
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
implicit def RexpOps(r: Rexp) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def | (s: Rexp) = ALT(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def % = STAR(r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def ~ (s: Rexp) = SEQ(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
}
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
implicit def stringOps(s: String) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def | (r: Rexp) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def | (r: String) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def % = STAR(s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def ~ (r: Rexp) = SEQ(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def ~ (r: String) = SEQ(s, r)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    42
  def $ (r: Rexp) = RECD(s, r)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    45
// nullable function: tests whether the regular 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    46
// expression can recognise the empty string
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
def nullable (r: Rexp) : Boolean = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    48
  case ZERO => false
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    49
  case ONE => true
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  case CHAR(_) => false
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  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
    52
  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
    53
  case STAR(_) => true
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    54
  case RECD(_, r1) => nullable(r1)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    57
// 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
    58
def der (c: Char, r: Rexp) : Rexp = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    59
  case ZERO => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    60
  case ONE => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    61
  case CHAR(d) => if (c == d) ONE else ZERO
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  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
    63
  case SEQ(r1, r2) => 
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
    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
    65
    else SEQ(der(c, r1), r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  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
    67
  case RECD(_, r1) => der(c, r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    68
}
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
// 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
    71
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
    72
  case Nil => r
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    73
  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
    74
}
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
// extracts a string from value
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    77
def flatten(v: Val) : String = v match {
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    78
  case Empty => ""
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    79
  case Chr(c) => c.toString
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    80
  case Left(v) => flatten(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    81
  case Right(v) => flatten(v)
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
    82
  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    83
  case Stars(vs) => vs.map(flatten).mkString
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    84
  case Rec(_, v) => flatten(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    85
}
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
// extracts an environment from a value
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
    88
def env[A](v: Val) : List[A] = v match {
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    89
  case Empty => Nil
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    90
  case Chr(c) => Nil
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    91
  case Left(v) => env(v)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    92
  case Right(v) => env(v)
520
2849c305b12d updated
cu
parents: 450
diff changeset
    93
  case Sequ(v1, v2) => env(v1) ::: env(v2)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    94
  case Stars(vs) => vs.flatMap(env)
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
    95
  case Rec(f, v) => ((f:String => A)(flatten(v)))::env(v)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    96
}
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
// injection part
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    99
def mkeps(r: Rexp) : Val = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   100
  case ONE => Empty
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   101
  case ALT(r1, r2) => 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   102
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
520
2849c305b12d updated
cu
parents: 450
diff changeset
   103
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   104
  case STAR(r) => Stars(Nil)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   105
  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
   106
}
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
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   109
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   110
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
2849c305b12d updated
cu
parents: 450
diff changeset
   111
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
2849c305b12d updated
cu
parents: 450
diff changeset
   112
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
2849c305b12d updated
cu
parents: 450
diff changeset
   113
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   114
  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
   115
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   116
  case (CHAR(d), Empty) => Chr(c) 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   117
  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
   118
}
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
// main lexing function (produces a value)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   121
def lex(r: Rexp, s: List[Char]) : Val = s match {
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   122
  case Nil => if (nullable(r)) mkeps(r) 
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   123
              else throw new Exception("Not matched")
352
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
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   129
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   130
lexing(("ab" | "a") ~ ("b" | ONE), "ab")
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   131
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   132
// some "rectification" functions for simplification
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   133
def F_ID(v: Val): Val = v
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   134
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
   135
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
   136
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
   137
  case Right(v) => Right(f2(v))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   138
  case Left(v) => Left(f1(v))
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(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   141
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   142
}
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   143
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   144
  (v:Val) => Sequ(f1(Empty), f2(v))
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   145
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   146
  (v:Val) => Sequ(f1(v), f2(Empty))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   147
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
   148
  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
   149
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   150
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
   151
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   152
// simplification of regular expressions returning also an
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   153
// rectification function; no simplification under STAR 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   154
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
   155
  case ALT(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   156
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   157
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   158
    (r1s, r2s) match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   159
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   160
      case (_, ZERO) => (r1s, F_LEFT(f1s))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   161
      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
   162
                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
   163
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   164
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   165
  case SEQ(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   166
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   167
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   168
    (r1s, r2s) match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   169
      case (ZERO, _) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   170
      case (_, ZERO) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   171
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   172
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   173
      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
   174
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   175
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   176
  case RECD(x, r1) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   177
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   178
    (RECD(x, r1s), F_RECD(f1s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   179
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   180
  case r => (r, F_ID)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   181
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   182
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   183
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
   184
  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
   185
  case c::cs => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   186
    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
   187
    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
   188
  }
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   191
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
   192
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   193
lexing_simp(("a" | "ab") ~ ("b" | ""), "ab")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   194
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   195
// Lexing Rules for a Small While Language
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   196
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   197
def PLUS(r: Rexp) = r ~ r.%
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   198
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   199
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
   200
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
   201
val ID = SYM ~ (SYM | DIGIT).% 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   202
val NUM = PLUS(DIGIT)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   203
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
   204
val SEMI: Rexp = ";"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   205
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   206
val WHITESPACE = PLUS(" " | "\n" | "\t")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   207
val RPAREN: Rexp = ")"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   208
val LPAREN: Rexp = "("
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   209
val BEGIN: Rexp = "{"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   210
val END: Rexp = "}"
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   211
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   212
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   213
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   214
val WHILE_REGS = (((s) => Token $ KEYWORD) | 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   215
                  ("i" $ ID) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   216
                  ("o" $ OP) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   217
                  ("n" $ NUM) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   218
                  ("s" $ SEMI) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   219
                  ("str" $ STRING) |
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   220
                  ("p" $ (LPAREN | RPAREN)) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   221
                  ("b" $ (BEGIN | END)) | 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   222
                  ("w" $ WHITESPACE)).%
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   223
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   224
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   225
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   226
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   227
// extracts an environment from a value
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   228
def env(v: Val) : List[Token] = v match {
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   229
  case Empty => Nil
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   230
  case Chr(c) => Nil
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   231
  case Left(v) => env(v)
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   232
  case Right(v) => env(v)
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   233
  case Sequ(v1, v2) => env(v1) ::: env(v2)
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   234
  case Stars(vs) => vs.flatMap(env)
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   235
  case Rec(f, v) => (f(flatten(v)))::env(v)
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   236
}
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   237
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   238
//   Testing
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
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   241
def time[T](code: => T) = {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   242
  val start = System.nanoTime()
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   243
  val result = code
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   244
  val end = System.nanoTime()
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   245
  println((end - start)/1.0e9)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   246
  result
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
val r1 = ("a" | "ab") ~ ("bcd" | "c")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   250
println(lexing(r1, "abcd"))
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 r2 = ("" | "a") ~ ("ab" | "b")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   253
println(lexing(r2, "ab"))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   254
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   255
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   256
// Two Simple While Tests
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   257
//========================
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   258
println("prog0 test")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   259
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   260
val prog0 = """read n"""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   261
println(env(lexing_simp(WHILE_REGS, prog0)))
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
println("prog1 test")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   264
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   265
val prog1 = """read  n; write (n)"""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   266
println(env(lexing_simp(WHILE_REGS, prog1)))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   267
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   268
521
95af9beb4b7f updated
cu
parents: 520
diff changeset
   269
// Bigger Test
95af9beb4b7f updated
cu
parents: 520
diff changeset
   270
//=============
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   271
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   272
val prog2 = """
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   273
write "fib";
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   274
read n;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   275
minus1 := 0;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   276
minus2 := 1;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   277
while n > 0 do {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   278
  temp := minus2;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   279
  minus2 := minus1 + minus2;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   280
  minus1 := temp;
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   281
  n := n - 1
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   282
};
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   283
write "result";
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   284
write minus2
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   285
"""
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   286
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   287
println("Tokens")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   288
println(env(lexing_simp(WHILE_REGS, prog2)))
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   289
println(env(lexing_simp(WHILE_REGS, prog2)).filterNot{_._1 == "w"}.mkString("\n"))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   290
521
95af9beb4b7f updated
cu
parents: 520
diff changeset
   291
// some more timing tests with
95af9beb4b7f updated
cu
parents: 520
diff changeset
   292
// i copies of the program
95af9beb4b7f updated
cu
parents: 520
diff changeset
   293
95af9beb4b7f updated
cu
parents: 520
diff changeset
   294
for (i <- 1 to 21 by 10) {
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   295
  print(i.toString + ":  ")
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   296
  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
   297
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   299
abstract class Token
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   300
case class KeyToken(s: String) extends Token
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   301
case class IdToken(s: String) extends Token
165
66b699c80479 tuned progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 164
diff changeset
   302
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   303
list[(STRING, STRING)]=> List(TOKEN)