|
1 package Handouts |
|
2 |
|
3 object Posix { |
|
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(v: Val) extends Val |
|
19 case class Right(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 def charlist2rexp(s: List[Char]): Rexp = s match { |
|
24 case Nil => EMPTY |
|
25 case c :: Nil => CHAR(c) |
|
26 case c :: s => SEQ(CHAR(c), charlist2rexp(s)) |
|
27 } //> charlist2rexp: (s: List[Char])Handouts.Posix.Rexp |
|
28 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList) |
|
29 //> string2rexp: (s: String)Handouts.Posix.Rexp |
|
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 } //> RexpOps: (r: Handouts.Posix.Rexp)AnyRef{def |(s: Handouts.Posix.Rexp): Hand |
|
36 //| outs.Posix.ALT; def %: Handouts.Posix.STAR; def ~(s: Handouts.Posix.Rexp): |
|
37 //| Handouts.Posix.SEQ} |
|
38 |
|
39 implicit def stringOps(s: String) = new { |
|
40 def |(r: Rexp) = ALT(s, r) |
|
41 def |(r: String) = ALT(s, r) |
|
42 def % = STAR(s) |
|
43 def ~(r: Rexp) = SEQ(s, r) |
|
44 def ~(r: String) = SEQ(s, r) |
|
45 def $(r: Rexp) = RECD(s, r) |
|
46 } //> stringOps: (s: String)AnyRef{def |(r: Handouts.Posix.Rexp): Handouts.Posix. |
|
47 //| ALT; def |(r: String): Handouts.Posix.ALT; def %: Handouts.Posix.STAR; def |
|
48 //| ~(r: Handouts.Posix.Rexp): Handouts.Posix.SEQ; def ~(r: String): Handouts.P |
|
49 //| osix.SEQ; def $(r: Handouts.Posix.Rexp): Handouts.Posix.RECD} |
|
50 |
|
51 // size of a regular expressions - for testing purposes |
|
52 def size(r: Rexp): Int = r match { |
|
53 case NULL => 1 |
|
54 case EMPTY => 1 |
|
55 case CHAR(_) => 1 |
|
56 case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
57 case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
58 case STAR(r) => 1 + size(r) |
|
59 case RECD(_, r) => 1 + size(r) |
|
60 } //> size: (r: Handouts.Posix.Rexp)Int |
|
61 |
|
62 // nullable function: tests whether the regular |
|
63 // expression can recognise the empty string |
|
64 def nullable(r: Rexp): Boolean = r match { |
|
65 case NULL => false |
|
66 case EMPTY => true |
|
67 case CHAR(_) => false |
|
68 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
69 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
70 case STAR(_) => true |
|
71 case RECD(_, r1) => nullable(r1) |
|
72 } //> nullable: (r: Handouts.Posix.Rexp)Boolean |
|
73 |
|
74 // derivative of a regular expression w.r.t. a character |
|
75 def der(c: Char, r: Rexp): Rexp = r match { |
|
76 case NULL => NULL |
|
77 case EMPTY => NULL |
|
78 case CHAR(d) => if (c == d) EMPTY else NULL |
|
79 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
80 case SEQ(r1, r2) => |
|
81 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
82 else SEQ(der(c, r1), r2) |
|
83 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
84 case RECD(_, r1) => der(c, r1) |
|
85 } //> der: (c: Char, r: Handouts.Posix.Rexp)Handouts.Posix.Rexp |
|
86 |
|
87 // derivative w.r.t. a string (iterates der) |
|
88 def ders(s: List[Char], r: Rexp): Rexp = s match { |
|
89 case Nil => r |
|
90 case c :: s => ders(s, der(c, r)) |
|
91 } //> ders: (s: List[Char], r: Handouts.Posix.Rexp)Handouts.Posix.Rexp |
|
92 |
|
93 // extracts a string from value |
|
94 def flatten(v: Val): String = v match { |
|
95 case Void => "" |
|
96 case Chr(c) => c.toString |
|
97 case Left(v) => flatten(v) |
|
98 case Right(v) => flatten(v) |
|
99 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
100 case Stars(vs) => vs.map(flatten).mkString |
|
101 case Rec(_, v) => flatten(v) |
|
102 } //> flatten: (v: Handouts.Posix.Val)String |
|
103 |
|
104 // extracts an environment from a value |
|
105 def env(v: Val): List[(String, String)] = v match { |
|
106 case Void => Nil |
|
107 case Chr(c) => Nil |
|
108 case Left(v) => env(v) |
|
109 case Right(v) => env(v) |
|
110 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
111 case Stars(vs) => vs.flatMap(env) |
|
112 case Rec(x, v) => (x, flatten(v)) :: env(v) |
|
113 } //> env: (v: Handouts.Posix.Val)List[(String, String)] |
|
114 |
|
115 def mkeps(r: Rexp): Val = r match { |
|
116 case EMPTY => Void |
|
117 case ALT(r1, r2) => |
|
118 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
119 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
120 case STAR(r) => Stars(Nil) |
|
121 case RECD(x, r) => Rec(x, mkeps(r)) |
|
122 } //> mkeps: (r: Handouts.Posix.Rexp)Handouts.Posix.Val |
|
123 |
|
124 def inj(r: Rexp, c: Char, v: Val): Val = (r, v) match { |
|
125 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1) :: vs) |
|
126 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
127 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
128 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
129 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
130 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
131 case (CHAR(d), Void) => Chr(d) |
|
132 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
133 } //> inj: (r: Handouts.Posix.Rexp, c: Char, v: Handouts.Posix.Val)Handouts.Posix |
|
134 //| .Val |
|
135 |
|
136 // main lexing function (produces a value) |
|
137 def lex(r: Rexp, s: List[Char]): Val = s match { |
|
138 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
139 case c :: cs => inj(r, c, lex(der(c, r), cs)) |
|
140 } //> lex: (r: Handouts.Posix.Rexp, s: List[Char])Handouts.Posix.Val |
|
141 |
|
142 def lexing(r: Rexp, s: String): Val = lex(r, s.toList) |
|
143 //> lexing: (r: Handouts.Posix.Rexp, s: String)Handouts.Posix.Val |
|
144 |
|
145 val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).% |
|
146 //> r : Handouts.Posix.STAR = STAR(ALT(RECD(1,CHAR(a)),ALT(RECD(2,CHAR(b)),REC |
|
147 //| D(3,SEQ(CHAR(a),CHAR(b)))))) |
|
148 env(lexing(r, "ba")) //> res0: List[(String, String)] = List((2,b), (1,a)) |
|
149 |
|
150 val r1 = "a" | "b" //> r1 : Handouts.Posix.ALT = ALT(CHAR(a),CHAR(b)) |
|
151 lexing(r1, "a") //> res1: Handouts.Posix.Val = Left(Chr(a)) |
|
152 |
|
153 // Lexing Rules for a Small While Language |
|
154 |
|
155 def PLUS(r: Rexp) = r ~ r.% //> PLUS: (r: Handouts.Posix.Rexp)Handouts.Posix.SEQ |
|
156 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" |
|
157 //> SYM : Handouts.Posix.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT |
|
158 //| (ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR( |
|
159 //| c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)) |
|
160 //| ,CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CH |
|
161 //| AR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z)) |
|
162 val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
|
163 //> DIGIT : Handouts.Posix.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),C |
|
164 //| HAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR |
|
165 //| (9)) |
|
166 val ID = SYM ~ (SYM | DIGIT).% //> ID : Handouts.Posix.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT( |
|
167 //| ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CH |
|
168 //| AR(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR( |
|
169 //| k)),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)) |
|
170 //| ,CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z)),STAR(ALT(AL |
|
171 //| T(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(A |
|
172 //| LT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)) |
|
173 //| ,CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CH |
|
174 //| AR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR( |
|
175 //| w)),CHAR(x)),CHAR(y)),CHAR(z)),ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0), |
|
176 //| CHAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHA |
|
177 //| R(9))))) |
|
178 val NUM = PLUS(DIGIT) //> NUM : Handouts.Posix.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0) |
|
179 //| ,CHAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CH |
|
180 //| AR(9)),STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),CHAR(1)),CHAR(2)),C |
|
181 //| HAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR(9)))) |
|
182 val KEYWORD: Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
|
183 //> KEYWORD : Handouts.Posix.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CH |
|
184 //| AR(s),SEQ(CHAR(k),SEQ(CHAR(i),CHAR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i |
|
185 //| ),SEQ(CHAR(l),CHAR(e)))))),SEQ(CHAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ( |
|
186 //| CHAR(t),SEQ(CHAR(h),SEQ(CHAR(e),CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHA |
|
187 //| R(s),CHAR(e))))),SEQ(CHAR(r),SEQ(CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w |
|
188 //| ),SEQ(CHAR(r),SEQ(CHAR(i),SEQ(CHAR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r), |
|
189 //| SEQ(CHAR(u),CHAR(e))))),SEQ(CHAR(f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHA |
|
190 //| R(e)))))) |
|
191 val SEMI: Rexp = ";" //> SEMI : Handouts.Posix.Rexp = CHAR(;) |
|
192 val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
|
193 //> OP : Handouts.Posix.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ |
|
194 //| (CHAR(:),CHAR(=)),SEQ(CHAR(=),CHAR(=))),CHAR(-)),CHAR(+)),CHAR(*)),SEQ(CHAR |
|
195 //| (!),CHAR(=))),CHAR(<)),CHAR(>)),SEQ(CHAR(<),CHAR(=))),SEQ(CHAR(>),CHAR(=))) |
|
196 //| ,CHAR(%)),CHAR(/)) |
|
197 val WHITESPACE = PLUS(" " | "\n" | "\t") //> WHITESPACE : Handouts.Posix.SEQ = SEQ(ALT(ALT(CHAR( ),CHAR( |
|
198 //| )),CHAR( )),STAR(ALT(ALT(CHAR( ),CHAR( |
|
199 //| )),CHAR( )))) |
|
200 val RPAREN: Rexp = ")" //> RPAREN : Handouts.Posix.Rexp = CHAR()) |
|
201 val LPAREN: Rexp = "(" //> LPAREN : Handouts.Posix.Rexp = CHAR(() |
|
202 val BEGIN: Rexp = "{" //> BEGIN : Handouts.Posix.Rexp = CHAR({) |
|
203 val END: Rexp = "}" //> END : Handouts.Posix.Rexp = CHAR(}) |
|
204 |
|
205 /* |
|
206 * val WHILE_REGS = (("k" $ KEYWORD) | |
|
207 ("i" $ ID) | |
|
208 ("o" $ OP) | |
|
209 ("n" $ NUM) | |
|
210 ("s" $ SEMI) | |
|
211 ("p" $ (LPAREN | RPAREN)) | |
|
212 ("b" $ (BEGIN | END)) | |
|
213 ("w" $ WHITESPACE)).% |
|
214 */ |
|
215 |
|
216 val WHILE_REGS = (KEYWORD | |
|
217 ID | |
|
218 OP | |
|
219 NUM | |
|
220 SEMI | |
|
221 LPAREN | RPAREN | |
|
222 BEGIN | END | |
|
223 WHITESPACE).% //> WHILE_REGS : Handouts.Posix.STAR = STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(AL |
|
224 //| T(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CHAR(s),SEQ(CHAR(k),SEQ(CHAR(i),C |
|
225 //| HAR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i),SEQ(CHAR(l),CHAR(e)))))),SEQ( |
|
226 //| CHAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(CHAR(t),SEQ(CHAR(h),SEQ(CHAR(e) |
|
227 //| ,CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e))))),SEQ(CHAR(r),SE |
|
228 //| Q(CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w),SEQ(CHAR(r),SEQ(CHAR(i),SEQ(C |
|
229 //| HAR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),SEQ(CHAR(u),CHAR(e))))),SEQ(CHA |
|
230 //| R(f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e)))))),SEQ(ALT(ALT(ALT(ALT(A |
|
231 //| LT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT( |
|
232 //| ALT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR( |
|
233 //| h)),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)) |
|
234 //| ,CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CH |
|
235 //| AR(y)),CHAR(z)),STAR(AL |
|
236 //| Output exceeds cutoff limit. |
|
237 |
|
238 // Some Tests |
|
239 //============ |
|
240 |
|
241 def time[T](code: => T) = { |
|
242 val start = System.nanoTime() |
|
243 val result = code |
|
244 val end = System.nanoTime() |
|
245 println((end - start) / 1.0e9) |
|
246 result |
|
247 } //> time: [T](code: => T)T |
|
248 |
|
249 val abc = List('a', 'b', 'c') //> abc : List[Char] = List(a, b, c) |
|
250 val nullRexp = null //> nullRexp : Null = null |
|
251 val myRexp = charlist2rexp(abc) //> myRexp : Handouts.Posix.Rexp = SEQ(CHAR(a),SEQ(CHAR(b),CHAR(c))) |
|
252 val myRexp2 = string2rexp("FahadAusaf") //> myRexp2 : Handouts.Posix.Rexp = SEQ(CHAR(F),SEQ(CHAR(a),SEQ(CHAR(h),SEQ(CH |
|
253 //| AR(a),SEQ(CHAR(d),SEQ(CHAR(A),SEQ(CHAR(u),SEQ(CHAR(s),SEQ(CHAR(a),CHAR(f))) |
|
254 //| ))))))) |
|
255 |
|
256 RexpOps(myRexp2) //> res2: AnyRef{def |(s: Handouts.Posix.Rexp): Handouts.Posix.ALT; def %: Hand |
|
257 //| outs.Posix.STAR; def ~(s: Handouts.Posix.Rexp): Handouts.Posix.SEQ} = Hando |
|
258 //| uts.Posix$$anonfun$main$1$$anon$1@6ee3572b |
|
259 |
|
260 stringOps("Fahad") //> res3: AnyRef{def |(r: Handouts.Posix.Rexp): Handouts.Posix.ALT; def |(r: St |
|
261 //| ring): Handouts.Posix.ALT; def %: Handouts.Posix.STAR; def ~(r: Handouts.Po |
|
262 //| six.Rexp): Handouts.Posix.SEQ; def ~(r: String): Handouts.Posix.SEQ; def $( |
|
263 //| r: Handouts.Posix.Rexp): Handouts.Posix.RECD} = Handouts.Posix$$anonfun$mai |
|
264 //| n$1$$anon$2@f7000ed |
|
265 |
|
266 size(myRexp2) //> res4: Int = 19 |
|
267 nullable(nullRexp) //> scala.MatchError: null |
|
268 //| at Handouts.Posix$$anonfun$main$1.nullable$1(Handouts.Posix.scala:58) |
|
269 //| at Handouts.Posix$$anonfun$main$1.apply$mcV$sp(Handouts.Posix.scala:200) |
|
270 //| |
|
271 //| at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe |
|
272 //| cute$1.apply$mcV$sp(WorksheetSupport.scala:76) |
|
273 //| at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W |
|
274 //| orksheetSupport.scala:65) |
|
275 //| at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor |
|
276 //| ksheetSupport.scala:75) |
|
277 //| at Handouts.Posix$.main(Handouts.Posix.scala:21) |
|
278 //| at Handouts.Posix.main(Handouts.Posix.scala) |
|
279 |
|
280 val newRexp = der('a', myRexp) |
|
281 |
|
282 //this is some crap |
|
283 } |