725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// A simple lexer inspired by work of Sulzmann & Lu
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//==================================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//
|
827
|
4 |
// call with
|
|
5 |
//
|
|
6 |
// amm lex.sc
|
|
7 |
//
|
|
8 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
// regular expressions including records
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case class RECD(x: String, r: Rexp) extends Rexp
|
787
|
19 |
// records for extracting strings or tokens
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
// values
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
abstract class Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
case object Empty extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
case class Chr(c: Char) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
case class Sequ(v1: Val, v2: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
case class Left(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
case class Right(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case class Stars(vs: List[Val]) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
case class Rec(x: String, v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
// some convenience for typing in regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
def charlist2rexp(s : List[Char]): Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case Nil => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
}
|
959
|
38 |
|
|
39 |
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
|
787
|
41 |
val HELLO : Rexp = "hello"
|
|
42 |
|
936
|
43 |
extension (r: Rexp) {
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
def | (s: Rexp) = ALT(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
def % = STAR(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
def ~ (s: Rexp) = SEQ(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
948
|
49 |
// to use & for records, instead of $ which had
|
|
50 |
// its precedence be changed in Scala 3
|
936
|
51 |
extension (s: String) {
|
948
|
52 |
def & (r: Rexp) = RECD(s, r)
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
|
787
|
55 |
val TEST = ("ab" | "ba").%
|
|
56 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
def nullable(r: Rexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case ZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
case ONE => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
case CHAR(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
case STAR(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
case RECD(_, r1) => nullable(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
def der(c: Char, r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
case ZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
case ONE => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
case CHAR(d) => if (c == d) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
case SEQ(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
else SEQ(der(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
case STAR(r) => SEQ(der(c, r), STAR(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
case RECD(_, r1) => der(c, r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
|
742
|
80 |
// extracts a string from a value
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
def flatten(v: Val) : String = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
case Empty => ""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
case Chr(c) => c.toString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
case Left(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
case Right(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
case Stars(vs) => vs.map(flatten).mkString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
case Rec(_, v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
// extracts an environment from a value;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
// used for tokenising a string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
def env(v: Val) : List[(String, String)] = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case Empty => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
case Chr(c) => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
case Left(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
case Right(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
case Sequ(v1, v2) => env(v1) ::: env(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
case Stars(vs) => vs.flatMap(env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
case Rec(x, v) => (x, flatten(v))::env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
// The injection and mkeps part of the lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
//===========================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
def mkeps(r: Rexp) : Val = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
case ONE => Empty
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
case ALT(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
case STAR(r) => Stars(Nil)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
case RECD(x, r) => Rec(x, mkeps(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
case (CHAR(d), Empty) => Chr(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
|
844
|
128 |
// lexing functions without simplification
|
786
|
129 |
def lex(r: Rexp, s: List[Char]) : Val = s match {
|
|
130 |
case Nil => if (nullable(r)) mkeps(r) else
|
|
131 |
{ throw new Exception("lexing error") }
|
|
132 |
case c::cs => inj(r, c, lex(der(c, r), cs))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
|
786
|
135 |
def lexing(r: Rexp, s: String) =
|
|
136 |
env(lex(r, s.toList))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
|
852
|
138 |
println(lex(("ab" | "a") ~ (ONE | "b"), "ab".toList))
|
|
139 |
|
|
140 |
println(lex(STAR("aa" | "a"), "aaa".toList))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
// The Lexing Rules for the WHILE Language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
def PLUS(r: Rexp) = r ~ r.%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
def Range(s : List[Char]) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
case Nil => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
case c::s => ALT(CHAR(c), Range(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
def RANGE(s: String) = Range(s.toList)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
|
800
|
153 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
val DIGIT = RANGE("0123456789")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
val ID = SYM ~ (SYM | DIGIT).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
val NUM = PLUS(DIGIT)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
val SEMI: Rexp = ";"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
|
846
|
160 |
val WHITESPACE = PLUS(" " | "\n" | "\t" | "\r")
|
787
|
161 |
val RPAREN: Rexp = "}"
|
|
162 |
val LPAREN: Rexp = "{"
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
|
948
|
166 |
val WHILE_REGS = (("k" & KEYWORD) |
|
|
167 |
("i" & ID) |
|
|
168 |
("o" & OP) |
|
|
169 |
("n" & NUM) |
|
|
170 |
("s" & SEMI) |
|
|
171 |
("str" & STRING) |
|
|
172 |
("p" & (LPAREN | RPAREN)) |
|
|
173 |
("w" & WHITESPACE)).%
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
|
786
|
175 |
val KY : Rexp = "if" | "read" | "write"
|
|
176 |
val WH : Rexp = " " | "\n"
|
|
177 |
|
948
|
178 |
val TRIV_REGS = (("k" & KY) |
|
|
179 |
("w" & WHITESPACE)).%
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
// Two Simple While Tests
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
//========================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
|
959
|
184 |
//@arg(doc = "small tests")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
def small() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
|
786
|
188 |
val prog0 = """if"""
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
println(s"test: $prog0")
|
786
|
190 |
println(lexing(WHILE_REGS, prog0))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
|
786
|
192 |
val prog1 = """iffoo"""
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
println(s"test: $prog1")
|
786
|
194 |
println(lexing(WHILE_REGS, prog1))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
|
786
|
196 |
val prog2 = """read n; write n"""
|
|
197 |
println(s"test: $prog2")
|
|
198 |
println(lexing(WHILE_REGS, prog2))
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
|