955
|
1 |
// A simple lexer inspired by work of Sulzmann $ Lu
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//==================================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
// Call the test cases with
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
// amm lexer.sc small
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
// amm lexer.sc fib
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
// amm lexer.sc loops
|
793
|
9 |
// amm lexer.sc email
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
// amm lexer.sc all
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
// regular expressions including records
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class RECD(x: String, r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
// records for extracting strings or tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
// values
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
abstract class Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
case object Empty extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case class Chr(c: Char) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
case class Sequ(v1: Val, v2: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
case class Left(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
case class Right(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
case class Stars(vs: List[Val]) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
case class Rec(x: String, v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
// some convenience for typing in regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
def charlist2rexp(s : List[Char]): Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case Nil => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
}
|
947
|
42 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
implicit def string2rexp(s : String) : Rexp =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
charlist2rexp(s.toList)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|
955
|
46 |
|
936
|
47 |
extension (s: String) {
|
955
|
48 |
def $ (r: Rexp) = RECD(s, r)
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
943
|
51 |
extension (r: Rexp) {
|
|
52 |
def | (s: Rexp) = ALT(r, s)
|
|
53 |
def % = STAR(r)
|
|
54 |
def ~ (s: Rexp) = SEQ(r, s)
|
|
55 |
}
|
|
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)
|
960
|
62 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
725
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 |
|
742
|
79 |
// extracts a string from a value
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
def flatten(v: Val) : String = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
case Empty => ""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
case Chr(c) => c.toString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
case Left(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
case Right(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
case Stars(vs) => vs.map(flatten).mkString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
case Rec(_, v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
}
|
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 |
// extracts an environment from a value;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
// used for tokenising a string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
def env(v: Val) : List[(String, String)] = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
case Empty => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case Chr(c) => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
case Left(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
case Right(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
case Sequ(v1, v2) => env(v1) ::: env(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
case Stars(vs) => vs.flatMap(env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
case Rec(x, v) => (x, flatten(v))::env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
}
|
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 |
// The injection and mkeps part of the lexer
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
//===========================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
def mkeps(r: Rexp) : Val = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
case ONE => Empty
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
case ALT(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
case STAR(r) => Stars(Nil)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
case RECD(x, r) => Rec(x, mkeps(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case (CHAR(d), Empty) => Chr(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
|
943
|
127 |
|
|
128 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
// some "rectification" functions for simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
def F_ID(v: Val): Val = v
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
case Right(v) => Right(f2(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
case Left(v) => Left(f1(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
(v:Val) => Sequ(f1(Empty), f2(v))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
(v:Val) => Sequ(f1(v), f2(Empty))
|
826
|
144 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
def F_ERROR(v: Val): Val = throw new Exception("error")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
// simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
def simp(r: Rexp): (Rexp, Val => Val) = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
case ALT(r1, r2) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
val (r1s, f1s) = simp(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
val (r2s, f2s) = simp(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
(r1s, r2s) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
case (ZERO, _) => (r2s, F_RIGHT(f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
case (_, ZERO) => (r1s, F_LEFT(f1s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
else (ALT (r1s, r2s), F_ALT(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
case SEQ(r1, r2) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
val (r1s, f1s) = simp(r1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
val (r2s, f2s) = simp(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
(r1s, r2s) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
case (ZERO, _) => (ZERO, F_ERROR)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
case (_, ZERO) => (ZERO, F_ERROR)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
case r => (r, F_ID)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
// lexing functions including simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
case Nil => if (nullable(r)) mkeps(r) else
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
{ throw new Exception("lexing error") }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
case c::cs => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
val (r_simp, f_simp) = simp(der(c, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
inj(r, c, f_simp(lex_simp(r_simp, cs)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
def lexing_simp(r: Rexp, s: String) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
env(lex_simp(r, s.toList))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
// The Lexing Rules for the WHILE Language
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
def PLUS(r: Rexp) = r ~ r.%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
def Range(s : List[Char]) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
case Nil => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
case c::s => ALT(CHAR(c), Range(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
def RANGE(s: String) = Range(s.toList)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
|
800
|
198 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
val DIGIT = RANGE("0123456789")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
val ID = SYM ~ (SYM | DIGIT).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
val NUM = PLUS(DIGIT)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
val SEMI: Rexp = ";"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
|
846
|
205 |
val WHITESPACE = PLUS(" " | "\n" | "\t" | "\r")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
val RPAREN: Rexp = "{"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
val LPAREN: Rexp = "}"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
|
955
|
211 |
val WHILE_REGS = (("k" $ KEYWORD) |
|
|
212 |
("i" $ ID) |
|
|
213 |
("o" $ OP) |
|
|
214 |
("n" $ NUM) |
|
|
215 |
("s" $ SEMI) |
|
|
216 |
("str" $ STRING) |
|
|
217 |
("p" $ (LPAREN | RPAREN)) |
|
|
218 |
("w" $ WHITESPACE)).%
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
// Two Simple While Tests
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
//========================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
|
827
|
224 |
@arg(doc = "small tests")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
def small() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
val prog0 = """read n"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
println(s"test: $prog0")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
println(lexing_simp(WHILE_REGS, prog0))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
val prog1 = """read n; write n"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
println(s"test: $prog1")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
println(lexing_simp(WHILE_REGS, prog1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
// Bigger Tests
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
//==============
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
// escapes strings and prints them out as "", "\n" and so on
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
def esc(raw: String): String = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
import scala.reflect.runtime.universe._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
Literal(Constant(raw)).toString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
def escape(tks: List[(String, String)]) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
247 |
tks.map{ case (s1, s2) => (s1, esc(s2))}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
248 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
249 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
250 |
val prog2 = """
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
251 |
write "Fib";
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
252 |
read n;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
253 |
minus1 := 0;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
254 |
minus2 := 1;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
255 |
while n > 0 do {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
256 |
temp := minus2;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
minus2 := minus1 + minus2;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
258 |
minus1 := temp;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
259 |
n := n - 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
260 |
};
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
261 |
write "Result";
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
262 |
write minus2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
263 |
"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
264 |
|
827
|
265 |
@arg(doc = "Fibonacci test")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
266 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
def fib() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
println("lexing fib program")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
272 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
273 |
val prog3 = """
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
274 |
start := 1000;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
275 |
x := start;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
276 |
y := start;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
277 |
z := start;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
278 |
while 0 < x do {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
279 |
while 0 < y do {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
280 |
while 0 < z do {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
281 |
z := z - 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
282 |
};
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
283 |
z := start;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
284 |
y := y - 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
285 |
};
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
286 |
y := start;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
287 |
x := x - 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
288 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
289 |
"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
290 |
|
827
|
291 |
@arg(doc = "Loops test")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
292 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
293 |
def loops() = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
294 |
println("lexing Loops")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
295 |
println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
296 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
297 |
|
827
|
298 |
@arg(doc = "Email Test")
|
793
|
299 |
@main
|
|
300 |
def email() = {
|
|
301 |
val lower = "abcdefghijklmnopqrstuvwxyz"
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
302 |
|
793
|
303 |
val NAME = RECD("name", PLUS(RANGE(lower ++ "_.-")))
|
|
304 |
val DOMAIN = RECD("domain", PLUS(RANGE(lower ++ "-")))
|
|
305 |
val RE = RANGE(lower ++ ".")
|
|
306 |
val TOPLEVEL = RECD("top", (RE ~ RE) |
|
|
307 |
(RE ~ RE ~ RE) |
|
|
308 |
(RE ~ RE ~ RE ~ RE) |
|
|
309 |
(RE ~ RE ~ RE ~ RE ~ RE) |
|
|
310 |
(RE ~ RE ~ RE ~ RE ~ RE ~ RE))
|
|
311 |
|
|
312 |
val EMAIL = NAME ~ "@" ~ DOMAIN ~ "." ~ TOPLEVEL
|
|
313 |
|
|
314 |
println(lexing_simp(EMAIL, "christian.urban@kcl.ac.uk"))
|
|
315 |
}
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
316 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
317 |
|
827
|
318 |
@arg(doc = "All tests.")
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
319 |
@main
|
793
|
320 |
def all() = { small(); fib() ; loops() ; email() }
|
792
|
321 |
|
|
322 |
|
|
323 |
|
944
|
324 |
/*
|
|
325 |
// Ammonite still provides
|
|
326 |
//
|
|
327 |
// scala.reflect.runtime.universe._
|
|
328 |
//
|
|
329 |
// which has been removed in Scala 3.
|
|
330 |
//
|
945
|
331 |
// for escaping strings in Scala 3 use this equivalent code
|
|
332 |
|
944
|
333 |
import scala.quoted._
|
827
|
334 |
|
944
|
335 |
def escapeImpl(raw: Expr[String])(using Quotes): Expr[String] = {
|
|
336 |
import quotes.reflect.*
|
|
337 |
Literal(StringConstant(raw.show)).asExprOf[String]
|
|
338 |
}
|
827
|
339 |
|
944
|
340 |
inline def esc(inline raw: String): String = ${escapeImpl('{raw})}
|
827
|
341 |
|
944
|
342 |
val s = "abc\ndef" // not escaped
|
|
343 |
val e = esc("abc\ndef") // escaped
|
|
344 |
|
|
345 |
println(s)
|
|
346 |
println(e)
|
|
347 |
*/ |