solutions/cw2/lexer.sc
changeset 897 904de68a27a4
parent 894 02ef5c3abc51
child 919 53f08d873e09
equal deleted inserted replaced
896:b7a6436c7758 897:904de68a27a4
   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 = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||"
   219                   ("p" $ PARENS) |
   224                   ("p" $ PARENS) |
   220                   ("s" $ SEMI) | 
   225                   ("s" $ SEMI) | 
   221                   ("w" $ WHITESPACE) | 
   226                   ("w" $ WHITESPACE) | 
   222                   ("i" $ ID) | 
   227                   ("i" $ ID) | 
   223                   ("n" $ NUM) |
   228                   ("n" $ NUM) |
   224 		  ("c" $ COMMENT)).%
   229 		              ("c" $ COMMENT)).%
   225 
   230 
   226 def esc(raw: String): String = {
   231 def esc(raw: String): String = {
   227   import scala.reflect.runtime.universe._
   232   import scala.reflect.runtime.universe._
   228   Literal(Constant(raw)).toString
   233   Literal(Constant(raw)).toString
   229 }
   234 }
   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)))