RegexExplosionPlot/evilForBsimp.sc
changeset 666 6da4516ea87d
parent 545 333013923c5a
equal deleted inserted replaced
665:3bedbdce3a3b 666:6da4516ea87d
   357   case (NTIMES(n, r), Sequ(v1, Ntime(vs))) => Ntime(inj(r, c, v1)::vs)
   357   case (NTIMES(n, r), Sequ(v1, Ntime(vs))) => Ntime(inj(r, c, v1)::vs)
   358   case (OPTIONAL(r), v) => Optionall(inj(r, c, v))
   358   case (OPTIONAL(r), v) => Optionall(inj(r, c, v))
   359   case (NOT(r), Nots(s)) => Nots(c.toString ++ s)
   359   case (NOT(r), Nots(s)) => Nots(c.toString ++ s)
   360   case (ANYCHAR, Empty) => Chr(c)
   360   case (ANYCHAR, Empty) => Chr(c)
   361 }
   361 }
       
   362 
       
   363 
       
   364 def print_lexer(r: Rexp, s: List[Char]): Val = s match{
       
   365   case Nil => {println("mkeps!"); println(r); val v = mkeps(r); println(v); v}
       
   366   case c::cs => { 
       
   367     val derc = der(c, r);
       
   368     println("derivative is: "+ derc);
       
   369     val v = print_lexer(derc, cs); println("Injection!");println("r: "+r); println("c: "+c); println("v:"+v); inj(r, c, v ) }
       
   370 }
       
   371 val r = STAR("a"|"aa") ~ ("c")
       
   372 val v = print_lexer(r, "aac".toList)
       
   373 println(v)
   362 
   374 
   363 // some "rectification" functions for simplification
   375 // some "rectification" functions for simplification
   364 
   376 
   365 
   377 
   366 
   378 
   732   case (v, Nil) => v
   744   case (v, Nil) => v
   733   case _ => throw new Exception("Not decodable")
   745   case _ => throw new Exception("Not decodable")
   734 }
   746 }
   735 
   747 
   736 
   748 
   737 
   749 def blexing(r: Rexp, s: String) : Option[Val] = {
   738 def blexing_simp(r: Rexp, s: String) : Val = {
   750     val bit_code = blex(internalise(r), s.toList)
       
   751     bit_code match {
       
   752       case Some(bits) => Some(decode(r, bits))
       
   753       case None => None
       
   754     }
       
   755 }
       
   756 
       
   757 def blexing_simp(r: Rexp, s: String) : Option[Val] = {
   739     val bit_code = blex_simp(internalise(r), s.toList)
   758     val bit_code = blex_simp(internalise(r), s.toList)
   740     decode(r, bit_code)
   759     bit_code match {
   741 }
   760       case Some(bits) => Some(decode(r, bits))
   742 def simpBlexer(r: Rexp, s: String) : Val = {
   761       case None => None
   743   Try(blexing_simp(r, s)).getOrElse(Failure)
   762     }
   744 }
   763 }
       
   764 //def simpBlexer(r: Rexp, s: String) : Val = {
       
   765 //  Try(blexing_simp(r, s)).getOrElse(Failure)
       
   766 //}
   745 
   767 
   746 def strong_blexing_simp(r: Rexp, s: String) : Val = {
   768 def strong_blexing_simp(r: Rexp, s: String) : Val = {
   747   decode(r, strong_blex_simp(internalise(r), s.toList))
   769   decode(r, strong_blex_simp(internalise(r), s.toList))
   748 }
   770 }
   749 
   771 
   852       case STAR(STAR(r)) => "(..)*"// ++ shortRexpOutput(r) ++ "]*"
   874       case STAR(STAR(r)) => "(..)*"// ++ shortRexpOutput(r) ++ "]*"
   853       case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
   875       case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
   854       //case RTOP => "RTOP"
   876       //case RTOP => "RTOP"
   855     }
   877     }
   856 
   878 
   857 
   879   def blex(r: ARexp, s: List[Char]) : Option[Bits]= s match {
   858   def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
       
   859       case Nil => {
   880       case Nil => {
   860         if (bnullable(r)) {
   881         if (bnullable(r)) {
   861           val bits = mkepsBC(r)
   882           val bits = mkepsBC(r)
   862           bits
   883           Some(bits)
   863         }
   884         }
   864         else 
   885         else 
   865           throw new Exception("Not matched")
   886           None
       
   887           //throw new Exception("Not matched")
       
   888       }
       
   889       case c::cs => {
       
   890         val der_res = bder(c,r)
       
   891         //val simp_res = bsimp(der_res)  
       
   892         blex(der_res, cs)      
       
   893       }
       
   894   }
       
   895 
       
   896   def blex_simp(r: ARexp, s: List[Char]) : Option[Bits]= s match {
       
   897       case Nil => {
       
   898         if (bnullable(r)) {
       
   899           val bits = mkepsBC(r)
       
   900           Some(bits)
       
   901         }
       
   902         else 
       
   903           None
       
   904           //throw new Exception("Not matched")
   866       }
   905       }
   867       case c::cs => {
   906       case c::cs => {
   868         val der_res = bder(c,r)
   907         val der_res = bder(c,r)
   869         val simp_res = bsimp(der_res)  
   908         val simp_res = bsimp(der_res)  
   870         blex_simp(simp_res, cs)      
   909         blex_simp(simp_res, cs)      
  1123     !boolList.exists(b => b == false)
  1162     !boolList.exists(b => b == false)
  1124   }
  1163   }
  1125 
  1164 
  1126 
  1165 
  1127 }
  1166 }
  1128 small()
  1167 //small()
  1129 // generator_test()
  1168 // generator_test()
  1130 
  1169 
  1131 // 1
  1170 // 1
  1132 
  1171 
  1133 // SEQ(SEQ(SEQ(ONE,CHAR('b')),STAR(CHAR('b'))),
  1172 // SEQ(SEQ(SEQ(ONE,CHAR('b')),STAR(CHAR('b'))),
  1136 // SEQ(CHAR('a'),SEQ(ALTS(CHAR('b'),ZERO),SEQ(ONE,CHAR('b'))))),ONE)))),ONE))
  1175 // SEQ(CHAR('a'),SEQ(ALTS(CHAR('b'),ZERO),SEQ(ONE,CHAR('b'))))),ONE)))),ONE))
  1137 
  1176 
  1138 
  1177 
  1139 // Sequ(Sequ(Sequ(Empty,Chr(b)),Stars(List(Chr(b), Chr(b)))),Sequ(Right(Stars(List(Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty)), Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty))))),Empty))
  1178 // Sequ(Sequ(Sequ(Empty,Chr(b)),Stars(List(Chr(b), Chr(b)))),Sequ(Right(Stars(List(Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty)), Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty))))),Empty))
  1140 // Sequ(Sequ(Sequ(Empty,Chr(b)),Stars(List(Chr(b), Chr(b)))),Sequ(Right(Stars(List(Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty)), Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty))))),Empty))
  1179 // Sequ(Sequ(Sequ(Empty,Chr(b)),Stars(List(Chr(b), Chr(b)))),Sequ(Right(Stars(List(Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty)), Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty))))),Empty))
       
  1180 
       
  1181 
       
  1182 //def time[R](block: => R): R = {
       
  1183 //    val t0 = System.nanoTime()
       
  1184 //    val result = block    // call-by-name
       
  1185 //    val t1 = System.nanoTime()
       
  1186 //    println(" " + (t1 - t0)/1e9 + "")
       
  1187 //    result
       
  1188 //}
       
  1189 //val astarstarB  = STAR(STAR("a"))~("b")
       
  1190 //val data_points_x_axis = List.range(0, 60000, 3000)
       
  1191 //for (i <- data_points_x_axis) {print(i); time { blexing(astarstarB, ("a"*i)) } }
       
  1192 ////println(data_points_x_axis.zip( List(0,1,3,4)))
       
  1193 //for(i <- List.range(0, 40000, 3000)) print(i + " ")
       
  1194