Attic/token.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 07 Sep 2020 12:18:07 +0100
changeset 754 1c9a23304b85
parent 641 progs/token.scala@1062a9512e79
permissions -rw-r--r--
ammonite
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
641
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A simple lexer inspired by work of Sulzmann & Lu
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================================
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.language.implicitConversions    
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import scala.language.reflectiveCalls
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// regular expressions including records
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
abstract class Rexp 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case object ZERO extends Rexp
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case object ONE extends Rexp
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case class CHAR(c: Char) extends Rexp
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
case class STAR(r: Rexp) extends Rexp 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case class RECD(x: String, r: Rexp) extends Rexp
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// values  
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
abstract class Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case object Empty extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class Chr(c: Char) extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class Sequ(v1: Val, v2: Val) extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case class Left(v: Val) extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case class Right(v: Val) extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class Stars(vs: List[Val]) extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class Rec(x: String, v: Val) extends Val
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
   
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// some convenience for typing in regular expressions
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
def charlist2rexp(s : List[Char]): Rexp = s match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case Nil => ONE
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case c::Nil => CHAR(c)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
implicit def string2rexp(s : String) : Rexp = 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  charlist2rexp(s.toList)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
implicit def RexpOps(r: Rexp) = new {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  def | (s: Rexp) = ALT(r, s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  def % = STAR(r)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  def ~ (s: Rexp) = SEQ(r, s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
implicit def stringOps(s: String) = new {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  def | (r: Rexp) = ALT(s, r)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  def | (r: String) = ALT(s, r)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  def % = STAR(s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  def ~ (r: Rexp) = SEQ(s, r)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  def ~ (r: String) = SEQ(s, r)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  def $ (r: Rexp) = RECD(s, r)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
def nullable(r: Rexp) : Boolean = r match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case ZERO => false
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  case ONE => true
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  case CHAR(_) => false
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  case STAR(_) => true
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  case RECD(_, r1) => nullable(r1)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def der(c: Char, r: Rexp) : Rexp = r match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case ZERO => ZERO
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case ONE => ZERO
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case CHAR(d) => if (c == d) ONE else ZERO
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  case SEQ(r1, r2) => 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
    else SEQ(der(c, r1), r2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  case STAR(r) => SEQ(der(c, r), STAR(r))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  case RECD(_, r1) => der(c, r1)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// extracts a string from value
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
def flatten(v: Val) : String = v match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  case Empty => ""
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  case Chr(c) => c.toString
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  case Left(v) => flatten(v)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  case Right(v) => flatten(v)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  case Stars(vs) => vs.map(flatten).mkString
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  case Rec(_, v) => flatten(v)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
// extracts an environment from a value;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
// used for tokenise a string
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
def env(v: Val) : List[(String, String)] = v match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  case Empty => Nil
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  case Chr(c) => Nil
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  case Left(v) => env(v)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  case Right(v) => env(v)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  case Sequ(v1, v2) => env(v1) ::: env(v2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  case Stars(vs) => vs.flatMap(env)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
  case Rec(x, v) => (x, flatten(v))::env(v)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
// The Injection Part of the lexer
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
def mkeps(r: Rexp) : Val = r match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  case ONE => Empty
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case ALT(r1, r2) => 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  case STAR(r) => Stars(Nil)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  case RECD(x, r) => Rec(x, mkeps(r))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  case (CHAR(d), Empty) => Chr(c) 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
// some "rectification" functions for simplification
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
def F_ID(v: Val): Val = v
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  case Right(v) => Right(f2(v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  case Left(v) => Left(f1(v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  (v:Val) => Sequ(f1(Empty), f2(v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  (v:Val) => Sequ(f1(v), f2(Empty))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
def F_RECD(f: Val => Val) = (v:Val) => v match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  case Rec(x, v) => Rec(x, f(v))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
def F_ERROR(v: Val): Val = throw new Exception("error")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
def simp(r: Rexp): (Rexp, Val => Val) = r match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  case ALT(r1, r2) => {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
    val (r1s, f1s) = simp(r1)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
    val (r2s, f2s) = simp(r2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    (r1s, r2s) match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
      case (_, ZERO) => (r1s, F_LEFT(f1s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
    }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  case SEQ(r1, r2) => {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
    val (r1s, f1s) = simp(r1)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
    val (r2s, f2s) = simp(r2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
    (r1s, r2s) match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
      case (ZERO, _) => (ZERO, F_ERROR)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
      case (_, ZERO) => (ZERO, F_ERROR)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
    }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  case RECD(x, r1) => {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
    val (r1s, f1s) = simp(r1)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
    (RECD(x, r1s), F_RECD(f1s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  case r => (r, F_ID)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
// lexing functions including simplification
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  case Nil => if (nullable(r)) mkeps(r) else 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
    { throw new Exception("lexing error") } 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  case c::cs => {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
    val (r_simp, f_simp) = simp(der(c, r))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
def lexing_simp(r: Rexp, s: String) = 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  env(lex_simp(r, s.toList))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
// The Lexing Rules for the Fun Language
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
def PLUS(r: Rexp) = r ~ r.%
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
def Range(s : List[Char]) : Rexp = s match {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  case Nil => ZERO
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  case c::Nil => CHAR(c)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  case c::s => ALT(CHAR(c), Range(s))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
def RANGE(s: String) = Range(s.toList)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
val DIGIT = RANGE("0123456789")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
val ID = SYM ~ (SYM | DIGIT).% 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
val NUM = PLUS(DIGIT)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
val SEMI: Rexp = ";"
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
val WHITESPACE = PLUS(" " | "\n" | "\t")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
val RPAREN: Rexp = "{"
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
val LPAREN: Rexp = "}"
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
val WHILE_REGS = (("k" $ KEYWORD) | 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
                  ("i" $ ID) | 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
                  ("o" $ OP) | 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
                  ("n" $ NUM) | 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
                  ("s" $ SEMI) | 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
                  ("str" $ STRING) |
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
                  ("p" $ (LPAREN | RPAREN)) | 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
                  ("w" $ WHITESPACE)).%
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
// Two Simple While Tests
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
//========================
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
println("test: read n")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
val prog0 = """read n"""
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
println(lexing_simp(WHILE_REGS, prog0))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
println("test: read  n; write n ")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
val prog1 = """read  n; write n"""
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
println(lexing_simp(WHILE_REGS, prog1))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
// Bigger Tests
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
//==============
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
// escapes strings and prints them out as "", "\n" and so on
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
def esc(raw: String): String = {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  import scala.reflect.runtime.universe._
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  Literal(Constant(raw)).toString
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
def escape(tks: List[(String, String)]) =
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  tks.map{ case (s1, s2) => (s1, esc(s2))}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
val prog2 = """
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
write "Fib";
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
read n;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
minus1 := 0;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
minus2 := 1;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
while n > 0 do {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
  temp := minus2;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  minus2 := minus1 + minus2;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
  minus1 := temp;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  n := n - 1
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
};
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
write "Result";
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
write minus2
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
"""
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
println("Tokens for Fib")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n"))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
val prog3 = """
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
start := 1000;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
x := start;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
y := start;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
z := start;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
while 0 < x do {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
 while 0 < y do {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
  while 0 < z do {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
    z := z - 1
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
  };
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
  z := start;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
  y := y - 1
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
 };     
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
 y := start;
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
 x := x - 1
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
"""
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
println("Tokens for Loops")
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n"))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
// The tokens for the WHILE language
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
import java.io._
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
abstract class Token extends Serializable 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
case object T_SEMI extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
case object T_LPAREN extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
case object T_RPAREN extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
case class T_ID(s: String) extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
case class T_OP(s: String) extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
case class T_NUM(n: Int) extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
case class T_KWD(s: String) extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
case class T_STR(s: String) extends Token
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
val token : PartialFunction[(String, String), Token] = {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
  case ("s", _) => T_SEMI
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  case ("p", "{") => T_LPAREN
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
  case ("p", "}") => T_RPAREN
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
  case ("i", s) => T_ID(s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
  case ("o", s) => T_OP(s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
  case ("n", s) => T_NUM(s.toInt)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
  case ("k", s) => T_KWD(s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  case ("str", s) => T_STR(s)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
}
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
def tokenise(s: String) : List[Token] = 
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
  lexing_simp(WHILE_REGS, s).collect(token)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
tokenise(prog2)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
tokenise(prog3)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
object Lexer extends App {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
  val fname = "/tmp/nflx"
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  def serialise(tks: List[Token]) = {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
    val out = new ObjectOutputStream(new FileOutputStream(fname))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
    out.writeObject(tks)
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
    out.close
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
  }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  def deserialise() : List[Token] = {
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
    val in = new ObjectInputStream(new FileInputStream(fname))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
    val tks = in.readObject.asInstanceOf[List[Token]]
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
    in.close
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
    tks
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
  }
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
  serialise(tokenise(prog2))
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
  println(deserialise())
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
1062a9512e79 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
}