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