thys2/zre8.sc
author Chengsong
Thu, 07 Apr 2022 21:31:29 +0100
changeset 480 574749f5190b
parent 403 6291181fad07
permissions -rw-r--r--
a few more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
395
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     1
// package zre 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     2
//Zre5: eliminated mems table
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     3
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     4
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     5
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     6
import scala.collection.mutable.{Map => MMap}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     7
import scala.collection.mutable.{ArrayBuffer => MList}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     8
//import pprint._
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
     9
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    10
import scala.util.Try
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    11
import pprint._
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    12
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    13
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    14
abstract class Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    15
case object Empty extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    16
case class Chr(c: Char) extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    17
case class Sequ(v1: Val, v2: Val) extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    18
case class Left(v: Val) extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    19
case class Right(v: Val) extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    20
case class Stars(vs: List[Val]) extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    21
case object DummyFilling extends Val
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    22
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    23
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    24
// abstract class Rexp {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    25
//      def equals(other: Rexp) : Boolean = this.eq(other)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    26
// }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    27
abstract class Rexp
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    28
case object ZERO extends Rexp                    // matches nothing
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    29
case object ONE extends Rexp                     // matches an empty string
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    30
case class CHAR(c: Char) extends Rexp            // matches a character c
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    31
case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    32
case class AL1(r1: Rexp) extends Rexp
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    33
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    34
case class STAR(r: Rexp) extends Rexp
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    35
case object RTOP extends Rexp
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    36
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    37
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    38
//Seq a b --> Seq Seqa Seqb
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    39
//Seq a b --> Sequ chra chrb
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    40
//ALT r1 r2 --> mALT
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    41
//         AltC L   AltC R
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    42
var cyclicPreventionList : Set[Int]= Set()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    43
abstract class Ctx(var starIters: Int = 0){
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    44
    //starIters = 0
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    45
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    46
case object RootC extends Ctx
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    47
case class SeqC( mForMyself:  Mem, processedSibling: List[Val], unpSibling: List[Rexp]) extends Ctx 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    48
case class AltC( mForMyself:  Mem, wrapper: Val => Val) extends Ctx
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    49
case class StarC(mForMyself:  Mem, vs: List[Val], inside: Rexp) extends Ctx
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    50
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    51
case class Mem(var parents: MList[Ctx], var result : MList[(Int, Val)])
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    52
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    53
//AltC(Mem(RootC::Nil, Map()))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    54
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    55
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    56
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    57
type Zipper = (Val, Mem)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    58
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    59
var mems : MMap[(Int, Rexp), Mem] = MMap()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    60
        //start pos, original regex --> result entry
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    61
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    62
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    63
var pos : Int = 0
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    64
403
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    65
  //size: of a Aregx for testing purposes 
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    66
  def size(r: Rexp) : Int = r match {
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    67
    case ZERO => 1
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    68
    case ONE => 1
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    69
    case CHAR(_) => 1
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    70
    case SEQ(r1, r2) => 1 + size(r1) + size(r2)
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    71
    case ALT(r1, r2) => 1 + List(r1, r2).map(size).sum
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    72
    case STAR(r) => 1 + size(r)
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    73
  }
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    74
395
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    75
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    76
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    77
//input ..................
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    78
//          ^       ^
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    79
//          p       q
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    80
//          r
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    81
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    82
//parse r[p...q] --> v
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    83
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    84
//(a+aa)*
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    85
//aaa
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    86
//[R(Sequ(a, a)), vs]
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    87
//[L(a), L(a), vs]
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    88
def check_before_down(c: Ctx, r: Rexp, starIters: Int = 0) : List[Zipper] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    89
    c.starIters = starIters
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    90
    mems.get((pos, r)) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    91
        case Some(m) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    92
            // c match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    93
            //     case StarC(m, vs, inside) => vs.length
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
    94
            // }
403
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    95
            val idx = m.parents.lastIndexWhere(c0 => c0.starIters <= c.starIters)
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    96
            // if(m.parents.size == 1){
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    97
            //     println("third parent added")
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    98
            //     println(simpleCtxDisplay(c))
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
    99
            //     println("the other parents")
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   100
            //     m.parents.foreach(c00 => println(c00.starIters))
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   101
            //     println(idx + 1)
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   102
            //     println("c's star iters: "+ c.starIters)
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   103
            //     println(s"the others' star iters: ${m.parents(0).starIters}")
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   104
            // }
395
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   105
            m.parents.insert(idx + 1, c)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   106
            //m.parents = m.parents:::List(c)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   107
            m.result.find(endPos_value => endPos_value._1 == pos) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   108
                // case Some((i, v)) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   109
                //   original_up(v, c, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   110
                case None => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   111
                  List()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   112
            }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   113
        case None => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   114
            val m = Mem(MList(c), MList.empty[(Int, Val)])
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   115
            mems = mems + ((pos, r) -> m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   116
            original_down(r, m, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   117
    }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   118
    // val m = Mem(c::Nil, MList.empty[(Int, Val)])
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   119
    // original_down(r, m, d)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   120
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   121
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   122
//mems  pstart r  --> m parents [(pend, vres), ...]
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   123
//aaa
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   124
//012
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   125
//seq a a 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   126
//0 a~a --> m ... [(2, Sequ a a)]
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   127
        // c match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   128
        //     case StarC(mst, vst, rst) => print(s"StarC $vst\t")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   129
        //     case SeqC(mse, pr, unp) => print(s"SeqC $unp\t")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   130
        //     case AltC(mal, w) => print(s"AltC ${w(Empty)}\t")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   131
        //     case RootC => print("Root")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   132
        // }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   133
def reorderCtx(cs: List[Ctx]): List[Ctx] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   134
    Nil
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   135
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   136
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   137
def mem_up(vres: Val, m: Mem, starIters : Int = 0) : List[Zipper] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   138
    m.result += (pos -> vres)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   139
    //m.parents = m.parents.reverse
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   140
          
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   141
    // if(m.parents.size > 1){//println()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   142
    //     println()  
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   143
    //     println("each of the contexts")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   144
    //     m.parents.reverse.foreach (c =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   145
    //         println(simpleCtxDisplay(c))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   146
    //     )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   147
    //     println("after distinctCtx")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   148
    //     distinctCtx(m.parents.reverse).foreach(c =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   149
    //         println(simpleCtxDisplay(c))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   150
    //     )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   151
    //     //println(s"vs is $vss")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   152
    
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   153
    // }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   154
    //.distinctBy(zipBackToRegex(_))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   155
    //TODO: too many arraybuffer->list conversions
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   156
    (m.parents).distinctBy(zipBackToRegex(_)).flatMap((c: Ctx) =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   157
        original_up(vres, c, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   158
    ).toList
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   159
    // m.parents.reverse.flatMap((c: Ctx) =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   160
    //     original_up(vres, c, rec_depth)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   161
    // )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   162
    // original_up(vres, m.parents.last, rec_depth)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   163
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   164
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   165
def original_down(r: Rexp, m: Mem, starIters: Int = 0) : List[Zipper] = (r, m) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   166
    case (CHAR(b), m) => {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   167
        if (input(pos) == b) {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   168
            List((Chr(b), m)) 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   169
        }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   170
        else 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   171
            Nil
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   172
    }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   173
    case (ONE, m) => Nil//mem_up(Empty, m, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   174
    case (SEQ(r1, r2), m) =>  
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   175
        // if(nullable(r1)){
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   176
        //     val mprime = Mem(AltC(m, x => x )::Nil, MList.empty[(Int, Val)])
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   177
        //     check_before_down(SeqC(mprime, Nil, List(r2)), r1, starIters) :::
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   178
        //     check_before_down(SeqC(mprime, mkeps(r1)::Nil, Nil), r2, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   179
        // }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   180
        // else
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   181
            check_before_down(SeqC(m, Nil, List(r2)), r1, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   182
    case (ALT(r1, r2), m) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   183
        check_before_down(AltC(m, Left(_)), r1, starIters) ::: 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   184
        check_before_down(AltC(m, Right(_)), r2, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   185
    case (STAR(r0), m) =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   186
        check_before_down(StarC(m, Nil, r0), r0, starIters) :::
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   187
        mem_up(Stars(Nil), m, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   188
    case (_, _) => throw new Exception("original down unexpected r or m")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   189
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   190
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   191
def original_up(v: Val, c: Ctx, starIters: Int = 0) : List[Zipper] = 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   192
{
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   193
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   194
(v, c) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   195
    case (v, SeqC(m, v1::Nil, Nil)) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   196
        mem_up(Sequ(v1, v), m, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   197
    case (v, SeqC(m, vs, u1::Nil)) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   198
        check_before_down(SeqC(m, v::vs, Nil), u1, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   199
    case (v, AltC(m, wrap)) => m.result.find(tup2 => tup2._1 == pos) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   200
        case Some( (i, vPrime)  ) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   201
            m.result += (i -> wrap(v))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   202
            Nil
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   203
        case None => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   204
            mem_up(wrap(v), m, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   205
    } //mem_up(AL1(v), par)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   206
    //case (v, StarC(m, vs, r0)) => throw new Exception("why not hit starC")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   207
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   208
    case (v, RootC) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   209
        Nil
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   210
    case (v, StarC(m, vs, r0) ) => //mem_up(Stars(v::vs), m, starIters) //::: 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   211
        check_before_down(StarC(m, v::vs, r0), r0, starIters + 1) :::
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   212
        mem_up(Stars((v::vs).reverse), m, starIters)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   213
    case (_, _) => throw new Exception("hit unexpected context")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   214
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   215
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   216
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   217
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   218
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   219
def derive(p: Int, z: Zipper) : List[Zipper] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   220
    pos = p
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   221
    //println(s"z's actual size is ${actualZipperSize(z::Nil)}")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   222
    
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   223
    z match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   224
        case (v, m) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   225
            
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   226
            mem_up(v, m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   227
        case _ => throw new Exception("error")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   228
    }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   229
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   230
//let e' = Seq([]) in 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   231
//
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   232
def init_zipper(r: Rexp) : Zipper = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   233
    val m_top = Mem(MList(RootC), MList.empty[(Int, Val)])
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   234
    val c_top = SeqC(m_top, Nil, r::Nil)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   235
    val m_r = Mem(MList(c_top), MList.empty[(Int, Val)])
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   236
    println(s"initial zipper is (Empty, $m_r)")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   237
    (Empty, m_r)//TODO: which val should we start with? Maybe Empty, maybe doesn't matter
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   238
    // val dummyRexp = ONE
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   239
    // val dummyMem = Mem()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   240
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   241
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   242
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   243
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   244
def plug_convert(v: Val, c: Ctx) : List[Val] = 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   245
{
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   246
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   247
c match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   248
    case RootC => List(v)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   249
    //TODO: non-binary Seq requires ps.rev
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   250
    case SeqC(m, ps::Nil, Nil) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   251
        plug_mem(Sequ(ps, v), m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   252
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   253
    //TODO: un not nullable--partial values?
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   254
    case SeqC(m, Nil, un::Nil) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   255
        if(nullable(un))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   256
            plug_mem(Sequ(v, mkeps(un)), m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   257
        else
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   258
            Nil
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   259
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   260
    //TODO: when multiple results stored in m, which one to choose?
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   261
    case AltC(m, wrap) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   262
        plug_mem(wrap(v), m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   263
    case StarC(m, vs, r0) => plug_mem(Stars((v::vs).reverse), m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   264
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   265
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   266
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   267
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   268
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   269
var cnt = 0;
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   270
def plug_mem(v: Val, m: Mem) : List[Val] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   271
    m.result += (pos -> v)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   272
    //TODO: eliminate arraybuffer->list conversion
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   273
    m.parents.flatMap({c =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   274
        plug_convert(v, c)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   275
    }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   276
    ).toList
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   277
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   278
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   279
def plug_all(zs: List[Zipper]) : List[Val] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   280
    zs.flatMap(z => plug_mem(z._1, z._2))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   281
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   282
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   283
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   284
def mkeps(r: Rexp) : Val = r match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   285
  case ONE => Empty
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   286
  case ALT(r1, r2) => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   287
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   288
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   289
  case _ => DummyFilling
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   290
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   291
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   292
def nullable(r: Rexp) : Boolean = r match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   293
  case ZERO => false
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   294
  case ONE => true
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   295
  case CHAR(_) => false
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   296
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   297
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   298
  case _ => false
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   299
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   300
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   301
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   302
val tokList : List[Char] = "aab".toList
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   303
var input : List[Char] = tokList
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   304
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   305
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   306
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   307
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   308
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   309
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   310
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   311
def lexRecurse(zs: List[Zipper], index: Int) : List[Zipper] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   312
    if(index <  input.length )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   313
        lexRecurse(zs.flatMap(z => derive(index, z) ), index + 1)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   314
    else 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   315
        zs
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   316
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   317
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   318
def lex(r: Rexp, s: String) : List[Zipper] = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   319
    input = s.toList
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   320
    
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   321
    lexRecurse(init_zipper(r)::Nil,  0)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   322
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   323
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   324
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   325
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   326
implicit def charlist2rexp(s: List[Char]): Rexp = s match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   327
    case Nil => ONE
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   328
    case c::Nil => CHAR(c)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   329
    case c::cs => SEQ(CHAR(c), charlist2rexp(cs))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   330
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   331
implicit def string2Rexp(s: String) : Rexp = charlist2rexp(s.toList)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   332
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   333
implicit def RexpOps(r: Rexp) = new {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   334
    def | (s: Rexp) = ALT(r, s)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   335
    def ~ (s: Rexp) = SEQ(r, s)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   336
    def % = STAR(r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   337
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   338
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   339
implicit def stringOps(s: String) = new {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   340
    def | (r: Rexp) = ALT(s, r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   341
    def | (r: String) = ALT(s, r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   342
    def ~ (r: Rexp) = SEQ(s, r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   343
    def ~ (r: String) = SEQ(s, r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   344
    def % = STAR(s)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   345
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   346
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   347
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   348
//derive(0, init_zipper(re0))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   349
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   350
// println(re1s.length)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   351
// mems.foreach(a => println(a))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   352
// val re1sPlugged = plug_all(re1s)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   353
// re1sPlugged.foreach(zipper => {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   354
//                         println(zipper); 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   355
//                         println("delimit") 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   356
//                         })
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   357
                
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   358
// mems.clear()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   359
// println(mems)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   360
// println(re0)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   361
// val re2s = lex(re0, "aab")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   362
// val re2sPlugged = plug_all(re2s)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   363
// re2sPlugged.foreach(v => {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   364
//         val Sequ(Empty, vp) = v
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   365
//         println(vp)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   366
//     }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   367
// )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   368
// val re0 = SEQ(ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('a'))), 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   369
// ALT(SEQ(CHAR('a'), CHAR('b')), SEQ(CHAR('b'), CHAR('c')) )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   370
// )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   371
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   372
// val (rgraph, re0root) = makeGraphFromObject(re0)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   373
// val asciir = GraphLayout.renderGraph(rgraph)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   374
// println("printing out re0")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   375
// println(asciir)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   376
// val re1s = lex(re0, "aabc")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   377
 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   378
def actualZipperSize(zs: List[Zipper]) : Int = zs match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   379
    case Nil => 0
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   380
    case z::zs1 => countParents(z._2) + actualZipperSize(zs1)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   381
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   382
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   383
def countParents(m: Mem) : Int = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   384
    m.parents.map(c => countGrandParents(c)).sum
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   385
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   386
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   387
def countGrandParents(c: Ctx) : Int = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   388
    c match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   389
        case RootC => 1
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   390
        case SeqC(m, pr, unp) => countParents(m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   391
        case AltC(m, w) => countParents(m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   392
        case StarC(m, _, _) => countParents(m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   393
    }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   394
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   395
//(a+aa)* \a --> (1 + a)(a+aa)* --> (a+aa)* + (1+a)(a+aa)*
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   396
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   397
//a(a+aa)* + 1(a+aa)* + (a+aa)*
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   398
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   399
//a~(a + aa)* \a -> 1 ~ (a + aa)* 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   400
//va <-----> m --> SeqC(m1, pr, "a") --> AltC(m4, Right)--> StarC(m2, vs, "a" + "aa") --> SeqC(m) ---> Root
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   401
//           ^
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   402
//           ---> AltC(m4, Left) 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   403
def zipBackToRegex(c: Ctx, r: Rexp = ONE) : Rexp = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   404
    c match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   405
        case RootC => r
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   406
        case SeqC(m, pr, Nil) => zipBackToRegex(m.parents.head, r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   407
        case SeqC(m, pr, unp::Nil) => zipBackToRegex(m.parents.head, SEQ(r, unp))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   408
        case AltC(m, w) => zipBackToRegex(m.parents.head, r)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   409
        case StarC(m, vs, r0) => zipBackToRegex(m.parents.head, SEQ(r, STAR(r0)))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   410
    }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   411
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   412
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   413
def zipperSimp(z: Zipper) : Unit = z match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   414
    case (v, m) => //m.parents = m.parents.distinctBy(c => zipBackToRegex(c))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   415
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   416
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   417
def distinctCtx(cs: List[Ctx]) : List[Ctx] = cs.distinctBy(c => zipBackToRegex(c))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   418
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   419
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   420
def simpleCtxDisplay(c: Ctx, indent : Int = 0) : String = c match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   421
    case SeqC(m, pr, unp) => "Sc[m:" ++ printMem(m, indent + 1) ++ 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   422
        "pr:" ++ pr.map(v => shortValOutput(v)).mkString(", ") ++ " unp:" ++ unp.map(r2 => shortRexpOutput(r2)).mkString(", ") ++ "]"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   423
    case AltC(m, w) =>
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   424
        w(Empty) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   425
            case Left(_) => s"Ac(m:${printMem(m, indent + 1)}, Left(_))"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   426
            case Right(_) => s"Ac(m:${printMem(m, indent + 1)}, Right(_))"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   427
            case Empty => s"Ac(m:${printMem(m, indent + 1)}, id)"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   428
        } 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   429
    case StarC(m, vs, r0) => s"StarC[m:" ++ printMem(m, indent + 1) ++ 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   430
        "vs:" ++ vs.map(v => shortValOutput(v)).mkString(", ") ++ " r0: " ++ shortRexpOutput(r0)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   431
    case RootC => "Root"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   432
    //case AL1(r) => s"(+${shortRexpOutput(r)})"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   433
    //case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   434
    //case RTOP => "RTOP"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   435
  }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   436
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   437
def printMem(m: Mem, indent: Int = 0) : String = {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   438
   "M(par:" ++
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   439
   m.parents.map(c => simpleCtxDisplay(c, indent + 1)).mkString("(",",", ")")  ++
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   440
  (", results:")  ++
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   441
  (for(iRexp <- m.result) 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   442
    yield iRexp match {case (p: Int, v: Val) => s"$p->${shortValOutput(v)}"}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   443
  ).mkString("(",",", ")")  ++ 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   444
   ")" 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   445
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   446
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   447
def shortRexpOutput(r: Rexp) : String = r match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   448
    case CHAR(c) => c.toString
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   449
    case ONE => "1"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   450
    case ZERO => "0"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   451
    case SEQ(r1, r2) => "[" ++ shortRexpOutput(r1) ++ "~" ++ shortRexpOutput(r2) ++ "]"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   452
    case ALT(r1, r2) => "(" ++ shortRexpOutput(r1) ++ "+" ++ shortRexpOutput(r2) ++ ")"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   453
    case STAR(r) => "[" ++ shortRexpOutput(r) ++ "]*"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   454
    //case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   455
    case RTOP => "RTOP"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   456
  }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   457
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   458
def shortValOutput(v: Val) : String = v match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   459
    case Left(v) => "L(" ++ shortValOutput(v) ++ ")"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   460
    case Right(v) => "R(" ++ shortValOutput(v) ++ ")"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   461
    case Empty => "e"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   462
    case Sequ(v1, v2) => "[" ++ shortValOutput(v1) ++ "~" ++ shortValOutput(v2) ++ "]"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   463
    case Chr(a) => a.toString
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   464
    case Stars(vs) => "Stars" ++ vs.map(shortValOutput(_)).mkString("[", ",", "]")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   465
    case _ => "???"
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   466
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   467
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   468
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   469
//def crystalizeZipper
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   470
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   471
for(i <- 1 to 2) {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   472
    mems.clear()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   473
println(s"there are $i number of a's")
403
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   474
val re1 = ("a" | "ab" ) ~ ("c" | "bc")//(("a" | "b") ~ "c" | ("b" | "e") ~ "c" ) ~ "f"
6291181fad07 blexer1 for size bound with strongDB
Chengsong
parents: 395
diff changeset
   475
val re1Lexed = lex(re1, "abc")
395
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   476
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   477
//drawZippers(re1Lexed)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   478
println("size of actual zipper (including memoized contexts")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   479
println(actualZipperSize(re1Lexed))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   480
//println(re1Lexed)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   481
//re1Lexed.foreach(zipperSimp(_))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   482
//println(actualZipperSize(re1S))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   483
val re1resPlugged = plug_all(re1Lexed)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   484
//println(actualZipperSize(re1Lexed))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   485
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   486
println("value extracted")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   487
re1resPlugged.foreach(v => {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   488
        val Sequ(Empty, vp) = v
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   489
        println(vp)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   490
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   491
)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   492
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   493
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   494
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   495
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   496
mems.clear()
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   497
val re2 = SEQ(ONE, "a")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   498
val re2res = lex(re2, "a")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   499
//lex(1~a, "a") --> lexRecurse((1v, m  (SeqC(m (RootC, Nil), Nil, [1~a] ) )))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   500
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   501
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   502
println(re2res)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   503
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   504
val re2resPlugged = plug_all(re2res)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   505
re2resPlugged.foreach(v => {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   506
        val Sequ(Empty, vp) = v
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   507
        println(vp)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   508
}
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   509
)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   510
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   511
// println("remaining regex")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   512
// println(re1ss.flatMap(z => zipBackMem(z._2)))
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   513
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   514
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   515
// val re1ssPlugged = plug_all(re1ss)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   516
// println("each of the values")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   517
// re1ssPlugged.foreach(v => {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   518
//         //val Sequ(Empty, vp) = v
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   519
//         //println(vp)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   520
//         println(v)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   521
//     }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   522
// )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   523
// println(mems.size)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   524
//println(mems)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   525
//mems.map({case (ir, m) => if (ir._1 == 1 && ir._2 == CHAR('b')) println(printMem(m)) })
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   526
// println("Mkeps + inj:")
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   527
// println(
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   528
//     mems.get((0, re1)) match {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   529
//         case Some(m) => printMem(m)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   530
//         case None => ""
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   531
//     }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   532
//     )
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   533
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   534
// println(re1sPlugged)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   535
//drawZippers(re1s, plugOrNot = false)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   536
// re1s.foreach{
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   537
//   re1 => 
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   538
//   {
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   539
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   540
//     drawZippers(derive(1, re1), plugOrNot = true)
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   541
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   542
//   }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   543
// }
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   544
5bffeacdf17e preserves!
Chengsong
parents:
diff changeset
   545