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