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