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