progs/scala/re.scala
changeset 38 b48939cca0cf
parent 34 33065bde3bbd
child 41 4f8a9ed0f26d
equal deleted inserted replaced
37:d3eda1846087 38:b48939cca0cf
   213 val RPAREN: Rexp = ")"
   213 val RPAREN: Rexp = ")"
   214 val LPAREN: Rexp = "("
   214 val LPAREN: Rexp = "("
   215 val BEGIN: Rexp = "{"
   215 val BEGIN: Rexp = "{"
   216 val END: Rexp = "}"
   216 val END: Rexp = "}"
   217 
   217 
   218 /*
   218 
   219  * val WHILE_REGS = (("k" $ KEYWORD) | 
   219 val WHILE_REGS = (("k" $ KEYWORD) | 
   220                   ("i" $ ID) | 
   220                   ("i" $ ID) | 
   221                   ("o" $ OP) | 
   221                   ("o" $ OP) | 
   222                   ("n" $ NUM) | 
   222                   ("n" $ NUM) | 
   223                   ("s" $ SEMI) | 
   223                   ("s" $ SEMI) | 
   224                   ("p" $ (LPAREN | RPAREN)) | 
   224                   ("p" $ (LPAREN | RPAREN)) | 
   225                   ("b" $ (BEGIN | END)) | 
   225                   ("b" $ (BEGIN | END)) | 
   226                   ("w" $ WHITESPACE)).%
   226                   ("w" $ WHITESPACE)).%
   227 */
   227 
   228 
   228 /*
   229 val WHILE_REGS = (KEYWORD | 
   229 val WHILE_REGS = (KEYWORD | 
   230                   ID | 
   230                   ID | 
   231                   OP | 
   231                   OP | 
   232                   NUM | 
   232                   NUM | 
   233                   SEMI | 
   233                   SEMI | 
   234                   LPAREN | RPAREN | 
   234                   LPAREN | RPAREN | 
   235                   BEGIN | END | 
   235                   BEGIN | END | 
   236                   WHITESPACE).%
   236                   WHITESPACE).%
   237 
   237 */
   238 
   238 
   239 // Some Tests
   239 // Some Tests
   240 //============
   240 //============
   241 
   241 
   242 def time[T](code: => T) = {
   242 def time[T](code: => T) = {
   245   val end = System.nanoTime()
   245   val end = System.nanoTime()
   246   println((end - start)/1.0e9)
   246   println((end - start)/1.0e9)
   247   result
   247   result
   248 }
   248 }
   249 
   249 
       
   250 // Two Simple Tests
       
   251 //===================
       
   252 println("prog0 test")
       
   253 
   250 val prog0 = """read n"""
   254 val prog0 = """read n"""
   251 env (lexing(WHILE_REGS, prog0))
   255 println(env(lexing_simp(WHILE_REGS, prog0)))
   252 
   256 
   253 println("Next test")
   257 println("prog1 test")
   254 /*
   258 
   255 val prog1 = """read  n; write (n)"""
   259 val prog1 = """read  n; write (n)"""
   256 env (lexing(WHILE_REGS, prog1))
   260 println(env(lexing_simp(WHILE_REGS, prog1)))
   257 
   261 
       
   262 
       
   263 // Big Test
       
   264 //==========
   258 val prog2 = """
   265 val prog2 = """
   259 i := 2;
   266 i := 2;
   260 max := 100;
   267 max := 100;
   261 while i < max do {
   268 while i < max do {
   262   isprime := 1;
   269   isprime := 1;
   266     j := j + 1
   273     j := j + 1
   267   };
   274   };
   268   if isprime == 1 then write i else skip;
   275   if isprime == 1 then write i else skip;
   269   i := i + 1
   276   i := i + 1
   270 }"""
   277 }"""
   271 lexing_acc(WHILE_REGS, prog2)
   278 
   272 
   279 println("Tokens")
   273 for (i <- 1 to 228 by 1) {
   280 println(env(lexing_simp(WHILE_REGS, prog2)))
       
   281 
       
   282 for (i <- 1 to 80) {
   274   print(i.toString + ":  ")
   283   print(i.toString + ":  ")
   275   time(lexing_acc(WHILE_REGS, prog2 * i))
   284   time(lexing_simp(WHILE_REGS, prog2 * i))
   276 }
   285 }
   277 
   286 
   278 for (i <- 1 to 100 by 10) {
       
   279   print(i.toString + ":  ")
       
   280   time(lexing(WHILE_REGS, prog2 * i))
       
   281 }
       
   282 */