|
1 import scala.language.implicitConversions |
|
2 import scala.language.reflectiveCalls |
|
3 import scala.annotation.tailrec |
|
4 |
|
5 abstract class Rexp |
|
6 case object NULL extends Rexp |
|
7 case object EMPTY extends Rexp |
|
8 case class CHAR(c: Char) extends Rexp |
|
9 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
10 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
11 case class STAR(r: Rexp) extends Rexp |
|
12 case class RECD(x: String, r: Rexp) extends Rexp |
|
13 |
|
14 abstract class Val |
|
15 case object Void extends Val |
|
16 case class Chr(c: Char) extends Val |
|
17 case class Sequ(v1: Val, v2: Val) extends Val |
|
18 case class Left(n: Int, v: Val) extends Val |
|
19 case class Right(n: Int, v: Val) extends Val |
|
20 case class Stars(vs: List[Val]) extends Val |
|
21 case class Rec(x: String, v: Val) extends Val |
|
22 |
|
23 // some convenience for typing in regular expressions |
|
24 def charlist2rexp(s : List[Char]): Rexp = s match { |
|
25 case Nil => EMPTY |
|
26 case c::Nil => CHAR(c) |
|
27 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
28 } |
|
29 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
|
30 |
|
31 implicit def RexpOps(r: Rexp) = new { |
|
32 def | (s: Rexp) = ALT(r, s) |
|
33 def % = STAR(r) |
|
34 def ~ (s: Rexp) = SEQ(r, s) |
|
35 } |
|
36 |
|
37 implicit def stringOps(s: String) = new { |
|
38 def | (r: Rexp) = ALT(s, r) |
|
39 def | (r: String) = ALT(s, r) |
|
40 def % = STAR(s) |
|
41 def ~ (r: Rexp) = SEQ(s, r) |
|
42 def ~ (r: String) = SEQ(s, r) |
|
43 def $ (r: Rexp) = RECD(s, r) |
|
44 } |
|
45 |
|
46 // size of a regular expressions - for testing purposes |
|
47 def size(r: Rexp) : Int = r match { |
|
48 case NULL => 1 |
|
49 case EMPTY => 1 |
|
50 case CHAR(_) => 1 |
|
51 case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
52 case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
53 case STAR(r) => 1 + size(r) |
|
54 case RECD(_, r) => 1 + size(r) |
|
55 } |
|
56 |
|
57 // nullable function: tests whether the regular |
|
58 // expression can recognise the empty string |
|
59 def nullable (r: Rexp) : Boolean = r match { |
|
60 case NULL => false |
|
61 case EMPTY => true |
|
62 case CHAR(_) => false |
|
63 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
64 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
65 case STAR(_) => true |
|
66 case RECD(_, r1) => nullable(r1) |
|
67 } |
|
68 |
|
69 // derivative of a regular expression w.r.t. a character |
|
70 def der (c: Char, r: Rexp) : Rexp = r match { |
|
71 case NULL => NULL |
|
72 case EMPTY => NULL |
|
73 case CHAR(d) => if (c == d) EMPTY else NULL |
|
74 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
75 case SEQ(r1, r2) => |
|
76 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
77 else SEQ(der(c, r1), r2) |
|
78 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
79 case RECD(_, r1) => der(c, r1) |
|
80 } |
|
81 |
|
82 // derivative w.r.t. a string (iterates der) |
|
83 def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
84 case Nil => r |
|
85 case c::s => ders(s, der(c, r)) |
|
86 } |
|
87 |
|
88 // extracts a string from value |
|
89 def flatten(v: Val) : String = v match { |
|
90 case Void => "" |
|
91 case Chr(c) => c.toString |
|
92 case Left(n, v) => flatten(v) |
|
93 case Right(n, v) => flatten(v) |
|
94 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
95 case Stars(vs) => vs.map(flatten).mkString |
|
96 case Rec(_, v) => flatten(v) |
|
97 } |
|
98 |
|
99 // extracts an environment from a value |
|
100 def env(v: Val) : List[(String, String)] = v match { |
|
101 case Void => Nil |
|
102 case Chr(c) => Nil |
|
103 case Left(n, v) => env(v) |
|
104 case Right(n, v) => env(v) |
|
105 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
106 case Stars(vs) => vs.flatMap(env) |
|
107 case Rec(x, v) => (x, flatten(v))::env(v) |
|
108 } |
|
109 |
|
110 def left_inc(v: Val) = v match { |
|
111 case Left(v, n) => Left(v, n + 1) |
|
112 case v => Left(v, 1) |
|
113 } |
|
114 |
|
115 def right_inc(v: Val) = v match { |
|
116 case Right(v, n) => Right(v, n + 1) |
|
117 case v => Right(v, 1) |
|
118 } |
|
119 |
|
120 def mkeps(r: Rexp) : Val = r match { |
|
121 case EMPTY => Void |
|
122 case ALT(r1, r2) => |
|
123 if (nullable(r1)) left_inc(mkeps(r1)) else right_inc(mkeps(r2)) |
|
124 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
125 case STAR(r) => Stars(Nil) |
|
126 case RECD(x, r) => Rec(x, mkeps(r)) |
|
127 } |
|
128 |
|
129 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
130 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
131 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
132 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
133 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
134 case (ALT(r1, r2), Left(v1), 1) => Left(inj(r1, c, v1), 1) |
|
135 case (ALT(r1, r2), Left(v1), n) => inc_left(inj(r1, c, Left(v1, n - 1))) |
|
136 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
137 case (CHAR(d), Void) => Chr(d) |
|
138 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
139 } |
|
140 |
|
141 |
|
142 // main lexing function (produces a value) |
|
143 def lex(r: Rexp, s: List[Char]) : Val = s match { |
|
144 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
145 case c::cs => inj(r, c, lex(der(c, r), cs)) |
|
146 } |
|
147 |
|
148 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
|
149 |
|
150 |
|
151 // Lexing Rules for a Small While Language |
|
152 |
|
153 def PLUS(r: Rexp) = r ~ r.% |
|
154 val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" |
|
155 val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
|
156 val ID = SYM ~ (SYM | DIGIT).% |
|
157 val NUM = PLUS(DIGIT) |
|
158 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
|
159 val SEMI: Rexp = ";" |
|
160 val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
|
161 val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
162 val RPAREN: Rexp = ")" |
|
163 val LPAREN: Rexp = "(" |
|
164 val BEGIN: Rexp = "{" |
|
165 val END: Rexp = "}" |
|
166 |
|
167 /* |
|
168 * val WHILE_REGS = (("k" $ KEYWORD) | |
|
169 ("i" $ ID) | |
|
170 ("o" $ OP) | |
|
171 ("n" $ NUM) | |
|
172 ("s" $ SEMI) | |
|
173 ("p" $ (LPAREN | RPAREN)) | |
|
174 ("b" $ (BEGIN | END)) | |
|
175 ("w" $ WHITESPACE)).% |
|
176 */ |
|
177 |
|
178 val WHILE_REGS = (KEYWORD | |
|
179 ID | |
|
180 OP | |
|
181 NUM | |
|
182 SEMI | |
|
183 LPAREN | RPAREN | |
|
184 BEGIN | END | |
|
185 WHITESPACE).% |
|
186 |
|
187 |
|
188 // Some Tests |
|
189 //============ |
|
190 |
|
191 def time[T](code: => T) = { |
|
192 val start = System.nanoTime() |
|
193 val result = code |
|
194 val end = System.nanoTime() |
|
195 println((end - start)/1.0e9) |
|
196 result |
|
197 } |
|
198 |
|
199 val prog0 = """read n""" |
|
200 env (lexing_simp(WHILE_REGS, prog0)) |
|
201 |
|
202 println("Next test") |
|
203 /* |
|
204 val prog1 = """read n; write (n)""" |
|
205 env (lexing_simp(WHILE_REGS, prog1)) |
|
206 |
|
207 val prog2 = """ |
|
208 i := 2; |
|
209 max := 100; |
|
210 while i < max do { |
|
211 isprime := 1; |
|
212 j := 2; |
|
213 while (j * j) <= i + 1 do { |
|
214 if i % j == 0 then isprime := 0 else skip; |
|
215 j := j + 1 |
|
216 }; |
|
217 if isprime == 1 then write i else skip; |
|
218 i := i + 1 |
|
219 }""" |
|
220 lexing_acc(WHILE_REGS, prog2) |
|
221 |
|
222 for (i <- 1 to 228 by 1) { |
|
223 print(i.toString + ": ") |
|
224 time(lexing_acc(WHILE_REGS, prog2 * i)) |
|
225 } |
|
226 |
|
227 for (i <- 1 to 100 by 10) { |
|
228 print(i.toString + ": ") |
|
229 time(lexing_simp(WHILE_REGS, prog2 * i)) |
|
230 } |
|
231 */ |