194 val (r_simp, f_simp) = simp(der(c, r)) |
194 val (r_simp, f_simp) = simp(der(c, r)) |
195 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
195 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
196 } |
196 } |
197 } |
197 } |
198 |
198 |
|
199 def ders_simp(cs: List[Char], r: Rexp) : Rexp = cs match { |
|
200 case Nil => r |
|
201 case c::cs => ders_simp(cs, simp(der(c, r))._1) |
|
202 } |
|
203 |
199 def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList)) |
204 def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList)) |
200 |
205 |
201 // Language specific code |
206 // Language specific code |
202 val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip" |
207 val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip" |
203 val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||" |
208 val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||" |
344 case ONE => 1 |
349 case ONE => 1 |
345 case CHAR(_) => 1 |
350 case CHAR(_) => 1 |
346 case ALT(r1, r2) => 1 + size(r1) + size (r2) |
351 case ALT(r1, r2) => 1 + size(r1) + size (r2) |
347 case SEQ(r1, r2) => 1 + size(r1) + size (r2) |
352 case SEQ(r1, r2) => 1 + size(r1) + size (r2) |
348 case STAR(r1) => 1 + size(r1) |
353 case STAR(r1) => 1 + size(r1) |
|
354 case PLUS(r1) => 1 + size(r1) |
349 case NTIMES(r1, n) => 1 + size(r1) |
355 case NTIMES(r1, n) => 1 + size(r1) |
|
356 case RECD(_, r1) => 1 + size(r1) |
|
357 case RANGE(_) => 1 |
350 } |
358 } |
351 |
359 |
352 val reg = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
360 val reg = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
353 size(reg) // 5 |
361 size(reg) // 5 |
354 size(der('a', reg)) // 12 |
362 size(der('a', reg)) // 12 |
355 size(simp(der('a', reg))._1) // 8 |
363 size(simp(der('a', reg))._1) // 8 |
356 size(simp(der('a', der('a', reg)))._1) // 8 |
364 size(simp(der('a', der('a', reg)))._1) // 8 |
357 |
365 |
|
366 |
|
367 |
|
368 // python tests |
|
369 println(size(WHILE_REGS)) |
|
370 println(size(ders_simp("r".toList, WHILE_REGS))) |
|
371 println(size(ID)) |
|
372 println(size(ders_simp("read".toList, ID))) |