thys2/blexer1.sc
changeset 409 f71df68776bb
parent 403 6291181fad07
child 412 48876e1092f1
equal deleted inserted replaced
408:01d1285b08ed 409:f71df68776bb
   359           case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
   359           case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
   360       }
   360       }
   361       case AALTS(bs1, rs) => {
   361       case AALTS(bs1, rs) => {
   362             val rs_simp = rs.map(bsimp(_))
   362             val rs_simp = rs.map(bsimp(_))
   363             val flat_res = flats(rs_simp)
   363             val flat_res = flats(rs_simp)
   364             val dist_res = strongDistinctBy(flat_res)//distinctBy(flat_res, erase)
   364             val dist_res = distinctBy(flat_res, erase)//strongDB(flat_res)//distinctBy(flat_res, erase)
   365             dist_res match {
   365             dist_res match {
   366               case Nil => AZERO
   366               case Nil => AZERO
   367               case s :: Nil => fuse(bs1, s)
   367               case s :: Nil => fuse(bs1, s)
   368               case rs => AALTS(bs1, rs)  
   368               case rs => AALTS(bs1, rs)  
   369             }
   369             }
   370           
   370           
   371       }
   371       }
   372       case r => r
   372       case r => r
   373     }
   373     }
   374   }
   374   }
       
   375   def strongBsimp(r: ARexp): ARexp =
       
   376   {
       
   377     r match {
       
   378       case ASEQ(bs1, r1, r2) => (strongBsimp(r1), strongBsimp(r2)) match {
       
   379           case (AZERO, _) => AZERO
       
   380           case (_, AZERO) => AZERO
       
   381           case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
       
   382           case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
       
   383       }
       
   384       case AALTS(bs1, rs) => {
       
   385             val rs_simp = rs.map(strongBsimp(_))
       
   386             val flat_res = flats(rs_simp)
       
   387             val dist_res = strongDB(flat_res)//distinctBy(flat_res, erase)
       
   388             dist_res match {
       
   389               case Nil => AZERO
       
   390               case s :: Nil => fuse(bs1, s)
       
   391               case rs => AALTS(bs1, rs)  
       
   392             }
       
   393           
       
   394       }
       
   395       case r => r
       
   396     }
       
   397   }
       
   398 
   375   def bders (s: List[Char], r: ARexp) : ARexp = s match {
   399   def bders (s: List[Char], r: ARexp) : ARexp = s match {
   376     case Nil => r
   400     case Nil => r
   377     case c::s => bders(s, bder(c, r))
   401     case c::s => bders(s, bder(c, r))
   378   }
   402   }
   379 
   403 
   485       else distinctByWithAcc(xs, f, res::acc, x::accB)
   509       else distinctByWithAcc(xs, f, res::acc, x::accB)
   486     }
   510     }
   487   } 
   511   } 
   488 
   512 
   489 
   513 
   490   def strongDistinctBy(xs: List[ARexp], 
   514   def strongDB(xs: List[ARexp], 
   491                        acc1: List[Rexp] = Nil, 
   515                        acc1: List[Rexp] = Nil, 
   492                        acc2 : List[(List[Rexp], Rexp)] = Nil): List[ARexp] = xs match {
   516                        acc2 : List[(List[Rexp], Rexp)] = Nil): List[ARexp] = xs match {
   493     case Nil => Nil
   517     case Nil => Nil
   494     case (x::xs) => 
   518     case (x::xs) => 
   495         if(acc1.contains(erase(x)))
   519         if(acc1.contains(erase(x)))
   496             strongDistinctBy(xs, acc1, acc2)
   520             strongDB(xs, acc1, acc2)
   497         else{
   521         else{
   498             x match {
   522             x match {
   499                 case ASTAR(bs0, r0) => 
   523                 case ASTAR(bs0, r0) => 
   500                     val headList : List[ARexp] = List[ARexp](AONE(Nil))
   524                     val headList : List[ARexp] = List[ARexp](AONE(Nil))
   501                     val i = acc2.indexWhere(
   525                     val i = acc2.indexWhere(
   502                         r2stl => {val (r2s, tl) = r2stl; tl == erase(r0) } 
   526                         r2stl => {val (r2s, tl) = r2stl; tl == erase(r0) } 
   503                     )
   527                     )
   504                     if(i == -1){ 
   528                     if(i == -1){ 
   505                         x::strongDistinctBy(
   529                         x::strongDB(
   506                             xs, erase(x)::acc1, (ONE::Nil, erase(r0))::acc2
   530                             xs, erase(x)::acc1, (ONE::Nil, erase(r0))::acc2
   507                         )
   531                         )
   508                     }
   532                     }
   509                     else{
   533                     else{
   510                         val headListAlready = acc2(i)
   534                         val headListAlready = acc2(i)
   511                         val (newHeads, oldHeadsUpdated) = 
   535                         val (newHeads, oldHeadsUpdated) = 
   512                                 distinctByWithAcc(headList, erase, headListAlready._1)
   536                                 distinctByWithAcc(headList, erase, headListAlready._1)
   513                         newHeads match{
   537                         newHeads match{
   514                             case newHead::Nil =>
   538                             case newHead::Nil =>
   515                                 ASTAR(bs0, r0) :: 
   539                                 ASTAR(bs0, r0) :: 
   516                                 strongDistinctBy(xs, erase(x)::acc1, 
   540                                 strongDB(xs, erase(x)::acc1, 
   517                                 acc2.updated(i, (oldHeadsUpdated, headListAlready._2)) )//TODO: acc2 already contains headListAlready
   541                                 acc2.updated(i, (oldHeadsUpdated, headListAlready._2)) )//TODO: acc2 already contains headListAlready
   518                             case Nil =>
   542                             case Nil =>
   519                                 strongDistinctBy(xs, erase(x)::acc1, 
   543                                 strongDB(xs, erase(x)::acc1, 
   520                                 acc2)
   544                                 acc2)
   521                         }
   545                         }
   522                     }                
   546                     }                
   523                 case ASEQ(bs, r1, ASTAR(bs0, r0)) => 
   547                 case ASEQ(bs, r1, ASTAR(bs0, r0)) => 
   524                     val headList = breakHead(r1)
   548                     val headList = breakHead(r1)
   525                     val i = acc2.indexWhere(
   549                     val i = acc2.indexWhere(
   526                         r2stl => {val (r2s, tl) = r2stl; tl == erase(r0) } 
   550                         r2stl => {val (r2s, tl) = r2stl; tl == erase(r0) } 
   527                     )
   551                     )
   528                     if(i == -1){ 
   552                     if(i == -1){ 
   529                         x::strongDistinctBy(
   553                         x::strongDB(
   530                             xs, erase(x)::acc1, (headList.map(erase(_)), erase(r0))::acc2
   554                             xs, erase(x)::acc1, (headList.map(erase(_)), erase(r0))::acc2
   531                         )
   555                         )
   532                     }
   556                     }
   533                     else{
   557                     else{
   534                         val headListAlready = acc2(i)
   558                         val headListAlready = acc2(i)
   535                         val (newHeads, oldHeadsUpdated) = 
   559                         val (newHeads, oldHeadsUpdated) = 
   536                                 distinctByWithAcc(headList, erase, headListAlready._1)
   560                                 distinctByWithAcc(headList, erase, headListAlready._1)
   537                         newHeads match{
   561                         newHeads match{
   538                             case newHead::Nil =>
   562                             case newHead::Nil =>
   539                                 ASEQ(bs, newHead, ASTAR(bs0, r0)) :: 
   563                                 ASEQ(bs, newHead, ASTAR(bs0, r0)) :: 
   540                                 strongDistinctBy(xs, erase(x)::acc1, 
   564                                 strongDB(xs, erase(x)::acc1, 
   541                                 acc2.updated(i, (oldHeadsUpdated, headListAlready._2)) )//TODO: acc2 already contains headListAlready
   565                                 acc2.updated(i, (oldHeadsUpdated, headListAlready._2)) )//TODO: acc2 already contains headListAlready
   542                             case Nil =>
   566                             case Nil =>
   543                                 strongDistinctBy(xs, erase(x)::acc1, 
   567                                 strongDB(xs, erase(x)::acc1, 
   544                                 acc2)
   568                                 acc2)
   545                             case hds => val AALTS(bsp, rsp) = r1
   569                             case hds => val AALTS(bsp, rsp) = r1
   546                                 ASEQ(bs, AALTS(bsp, hds), ASTAR(bs0, r0)) ::
   570                                 ASEQ(bs, AALTS(bsp, hds), ASTAR(bs0, r0)) ::
   547                                 strongDistinctBy(xs, erase(x)::acc1,
   571                                 strongDB(xs, erase(x)::acc1,
   548                                 acc2.updated(i, (oldHeadsUpdated, headListAlready._2)))
   572                                 acc2.updated(i, (oldHeadsUpdated, headListAlready._2)))
   549                         }
   573                         }
   550                     }
   574                     }
   551                 case rPrime => x::strongDistinctBy(xs, erase(x)::acc1, acc2)    
   575                 case rPrime => x::strongDB(xs, erase(x)::acc1, acc2)    
   552             }
   576             }
   553                 
   577                 
   554         }
   578         }
   555     
   579     
   556 }
   580 }
   584 
   608 
   585   def asize(a: ARexp) = size(erase(a))
   609   def asize(a: ARexp) = size(erase(a))
   586 
   610 
   587 
   611 
   588 // @arg(doc = "small tests")
   612 // @arg(doc = "small tests")
   589 val STARREG = ("a" | "aa").%
   613 val STARREG = ((STAR("a") | STAR("aa") ).%).%
   590 
   614 
   591 @main
   615 @main
   592 def small() = {
   616 def small() = {
   593 
   617 
   594   val prog0 = """aaa"""
   618   val prog0 = """aaaaaaaaa"""
   595   println(s"test: $prog0")
   619   println(s"test: $prog0")
   596 //   println(lexing_simp(NOTREG, prog0))
   620 //   println(lexing_simp(NOTREG, prog0))
   597 //   val v = lex_simp(NOTREG, prog0.toList)
   621 //   val v = lex_simp(NOTREG, prog0.toList)
   598 //   println(v)
   622 //   println(v)
   599 
   623