progs/tokenise.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Oct 2019 02:09:48 +0100
changeset 644 b4f5714485e1
parent 642 064afa8fc1d9
child 645 30943d5491b6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     1
// A simple tokeniser based on the Sulzmann & Lu algorithm
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     2
//=========================================================
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     3
// 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     4
// call with 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     5
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     6
//     scala tokenise.scala fib.while
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     7
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     8
//     scala tokenise.scala loops.while
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    10
object Tokenise {
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
import scala.language.implicitConversions    
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import scala.language.reflectiveCalls
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// regular expressions including records
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
abstract class Rexp 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case object ZERO extends Rexp
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case object ONE extends Rexp
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case class CHAR(c: Char) extends Rexp
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class STAR(r: Rexp) extends Rexp 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case class RECD(x: String, r: Rexp) extends Rexp
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
// values  
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
abstract class Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case object Empty extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case class Chr(c: Char) extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
case class Sequ(v1: Val, v2: Val) extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case class Left(v: Val) extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case class Right(v: Val) extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class Stars(vs: List[Val]) extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case class Rec(x: String, v: Val) extends Val
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
   
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
// some convenience for typing in regular expressions
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
def charlist2rexp(s : List[Char]): Rexp = s match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  case Nil => ONE
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case c::Nil => CHAR(c)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
implicit def string2rexp(s : String) : Rexp = 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  charlist2rexp(s.toList)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
implicit def RexpOps(r: Rexp) = new {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  def | (s: Rexp) = ALT(r, s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  def % = STAR(r)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  def ~ (s: Rexp) = SEQ(r, s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
implicit def stringOps(s: String) = new {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  def | (r: Rexp) = ALT(s, r)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  def | (r: String) = ALT(s, r)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  def % = STAR(s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  def ~ (r: Rexp) = SEQ(s, r)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  def ~ (r: String) = SEQ(s, r)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  def $ (r: Rexp) = RECD(s, r)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def nullable(r: Rexp) : Boolean = r match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  case ZERO => false
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  case ONE => true
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  case CHAR(_) => false
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case STAR(_) => true
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  case RECD(_, r1) => nullable(r1)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
def der(c: Char, r: Rexp) : Rexp = r match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  case ZERO => ZERO
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  case ONE => ZERO
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  case CHAR(d) => if (c == d) ONE else ZERO
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case SEQ(r1, r2) => 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
    else SEQ(der(c, r1), r2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  case STAR(r) => SEQ(der(c, r), STAR(r))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  case RECD(_, r1) => der(c, r1)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
// extracts a string from value
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
def flatten(v: Val) : String = v match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  case Empty => ""
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  case Chr(c) => c.toString
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  case Left(v) => flatten(v)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  case Right(v) => flatten(v)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  case Stars(vs) => vs.map(flatten).mkString
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  case Rec(_, v) => flatten(v)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
// extracts an environment from a value;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
// used for tokenise a string
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
def env(v: Val) : List[(String, String)] = v match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  case Empty => Nil
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  case Chr(c) => Nil
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  case Left(v) => env(v)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case Right(v) => env(v)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  case Sequ(v1, v2) => env(v1) ::: env(v2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  case Stars(vs) => vs.flatMap(env)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case Rec(x, v) => (x, flatten(v))::env(v)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// The Injection Part of the lexer
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
def mkeps(r: Rexp) : Val = r match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  case ONE => Empty
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  case ALT(r1, r2) => 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  case STAR(r) => Stars(Nil)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case RECD(x, r) => Rec(x, mkeps(r))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  case (CHAR(d), Empty) => Chr(c) 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
// some "rectification" functions for simplification
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
def F_ID(v: Val): Val = v
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  case Right(v) => Right(f2(v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
  case Left(v) => Left(f1(v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  (v:Val) => Sequ(f1(Empty), f2(v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  (v:Val) => Sequ(f1(v), f2(Empty))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
def F_RECD(f: Val => Val) = (v:Val) => v match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  case Rec(x, v) => Rec(x, f(v))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
def F_ERROR(v: Val): Val = throw new Exception("error")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
def simp(r: Rexp): (Rexp, Val => Val) = r match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  case ALT(r1, r2) => {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
    val (r1s, f1s) = simp(r1)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
    val (r2s, f2s) = simp(r2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
    (r1s, r2s) match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
      case (_, ZERO) => (r1s, F_LEFT(f1s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
    }
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
  }
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  case SEQ(r1, r2) => {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
    val (r1s, f1s) = simp(r1)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
    val (r2s, f2s) = simp(r2)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
    (r1s, r2s) match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
      case (ZERO, _) => (ZERO, F_ERROR)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
      case (_, ZERO) => (ZERO, F_ERROR)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
    }
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  }
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  case RECD(x, r1) => {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
    val (r1s, f1s) = simp(r1)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
    (RECD(x, r1s), F_RECD(f1s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  }
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  case r => (r, F_ID)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
// lexing functions including simplification
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  case Nil => if (nullable(r)) mkeps(r) else 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
    { throw new Exception("lexing error") } 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  case c::cs => {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
    val (r_simp, f_simp) = simp(der(c, r))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  }
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
def lexing_simp(r: Rexp, s: String) = 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  env(lex_simp(r, s.toList))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
// The Lexing Rules for the Fun Language
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
def PLUS(r: Rexp) = r ~ r.%
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
def Range(s : List[Char]) : Rexp = s match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
  case Nil => ZERO
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
  case c::Nil => CHAR(c)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
  case c::s => ALT(CHAR(c), Range(s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
def RANGE(s: String) = Range(s.toList)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
val DIGIT = RANGE("0123456789")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
val ID = SYM ~ (SYM | DIGIT).% 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
val NUM = PLUS(DIGIT)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
val SEMI: Rexp = ";"
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
val WHITESPACE = PLUS(" " | "\n" | "\t")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
val RPAREN: Rexp = "{"
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
val LPAREN: Rexp = "}"
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
val WHILE_REGS = (("k" $ KEYWORD) | 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
                  ("i" $ ID) | 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
                  ("o" $ OP) | 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
                  ("n" $ NUM) | 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
                  ("s" $ SEMI) | 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
                  ("str" $ STRING) |
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
                  ("p" $ (LPAREN | RPAREN)) | 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
                  ("w" $ WHITESPACE)).%
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
// Generating tokens for the WHILE language
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
   227
// and serialising them into a .tks file
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
import java.io._
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
abstract class Token extends Serializable 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
case object T_SEMI extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
case object T_LPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
case object T_RPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
case class T_ID(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
case class T_OP(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
case class T_NUM(n: Int) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
case class T_KWD(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
case class T_STR(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
val token : PartialFunction[(String, String), Token] = {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  case ("s", _) => T_SEMI
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
  case ("p", "{") => T_LPAREN
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  case ("p", "}") => T_RPAREN
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  case ("i", s) => T_ID(s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  case ("o", s) => T_OP(s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
  case ("n", s) => T_NUM(s.toInt)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  case ("k", s) => T_KWD(s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  case ("str", s) => T_STR(s)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
def tokenise(s: String) : List[Token] = 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  lexing_simp(WHILE_REGS, s).collect(token)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
def serialise[T](fname: String, data: T) = {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
  val out = new ObjectOutputStream(new FileOutputStream(fname))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  out.writeObject(data)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
  out.close
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
def main(args: Array[String]) = {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
   263
  val fname = args(0)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
   264
  val file = io.Source.fromFile(fname).mkString
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
   265
  val tks = fname.stripSuffix(".while") ++ ".tks"
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
   266
  serialise(tks, tokenise(file))
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
}