thys2/zre7.sc
author Chengsong
Mon, 17 Jan 2022 20:51:03 +0000
changeset 390 d7f0153f5770
child 391 549257d0b8b2
permissions -rw-r--r--
zre
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
390
Chengsong
parents:
diff changeset
     1
// package zre 
Chengsong
parents:
diff changeset
     2
//Zre5: eliminated mems table
Chengsong
parents:
diff changeset
     3
Chengsong
parents:
diff changeset
     4
Chengsong
parents:
diff changeset
     5
Chengsong
parents:
diff changeset
     6
import scala.collection.mutable.{Map => MMap}
Chengsong
parents:
diff changeset
     7
import scala.collection.mutable.{ArrayBuffer => MList}
Chengsong
parents:
diff changeset
     8
//import pprint._
Chengsong
parents:
diff changeset
     9
Chengsong
parents:
diff changeset
    10
import scala.util.Try
Chengsong
parents:
diff changeset
    11
Chengsong
parents:
diff changeset
    12
Chengsong
parents:
diff changeset
    13
Chengsong
parents:
diff changeset
    14
abstract class Val
Chengsong
parents:
diff changeset
    15
case object Empty extends Val
Chengsong
parents:
diff changeset
    16
case class Chr(c: Char) extends Val
Chengsong
parents:
diff changeset
    17
case class Sequ(v1: Val, v2: Val) extends Val
Chengsong
parents:
diff changeset
    18
case class Left(v: Val) extends Val
Chengsong
parents:
diff changeset
    19
case class Right(v: Val) extends Val
Chengsong
parents:
diff changeset
    20
case class Stars(vs: List[Val]) extends Val
Chengsong
parents:
diff changeset
    21
case object DummyFilling extends Val
Chengsong
parents:
diff changeset
    22
Chengsong
parents:
diff changeset
    23
Chengsong
parents:
diff changeset
    24
// abstract class Rexp {
Chengsong
parents:
diff changeset
    25
//      def equals(other: Rexp) : Boolean = this.eq(other)
Chengsong
parents:
diff changeset
    26
// }
Chengsong
parents:
diff changeset
    27
abstract class Rexp
Chengsong
parents:
diff changeset
    28
case object ZERO extends Rexp                    // matches nothing
Chengsong
parents:
diff changeset
    29
case object ONE extends Rexp                     // matches an empty string
Chengsong
parents:
diff changeset
    30
case class CHAR(c: Char) extends Rexp            // matches a character c
Chengsong
parents:
diff changeset
    31
case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
Chengsong
parents:
diff changeset
    32
case class AL1(r1: Rexp) extends Rexp
Chengsong
parents:
diff changeset
    33
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
Chengsong
parents:
diff changeset
    34
case class STAR(r: Rexp) extends Rexp
Chengsong
parents:
diff changeset
    35
case object RTOP extends Rexp
Chengsong
parents:
diff changeset
    36
Chengsong
parents:
diff changeset
    37
Chengsong
parents:
diff changeset
    38
//Seq a b --> Seq Seqa Seqb
Chengsong
parents:
diff changeset
    39
//Seq a b --> Sequ chra chrb
Chengsong
parents:
diff changeset
    40
//ALT r1 r2 --> mALT
Chengsong
parents:
diff changeset
    41
//         AltC L   AltC R
Chengsong
parents:
diff changeset
    42
var cyclicPreventionList : Set[Int]= Set()
Chengsong
parents:
diff changeset
    43
abstract class Ctx   
Chengsong
parents:
diff changeset
    44
case object RootC extends Ctx
Chengsong
parents:
diff changeset
    45
case class SeqC( mForMyself:  Mem, processedSibling: List[Val], unpSibling: List[Rexp]) extends Ctx
Chengsong
parents:
diff changeset
    46
case class AltC( mForMyself:  Mem, wrapper: Val => Val) extends Ctx
Chengsong
parents:
diff changeset
    47
case class StarC(mForMyself:  Mem, vs: List[Val], inside: Rexp) extends Ctx
Chengsong
parents:
diff changeset
    48
Chengsong
parents:
diff changeset
    49
case class Mem(var parents: List[Ctx], var result : MList[(Int, Val)])
Chengsong
parents:
diff changeset
    50
Chengsong
parents:
diff changeset
    51
//AltC(Mem(RootC::Nil, Map()))
Chengsong
parents:
diff changeset
    52
Chengsong
parents:
diff changeset
    53
Chengsong
parents:
diff changeset
    54
Chengsong
parents:
diff changeset
    55
type Zipper = (Val, Mem)
Chengsong
parents:
diff changeset
    56
Chengsong
parents:
diff changeset
    57
var mems : MMap[(Int, Rexp), Mem] = MMap()
Chengsong
parents:
diff changeset
    58
        //start pos, original regex --> result entry
Chengsong
parents:
diff changeset
    59
Chengsong
parents:
diff changeset
    60
Chengsong
parents:
diff changeset
    61
var pos : Int = 0
Chengsong
parents:
diff changeset
    62
Chengsong
parents:
diff changeset
    63
Chengsong
parents:
diff changeset
    64
Chengsong
parents:
diff changeset
    65
//input ..................
Chengsong
parents:
diff changeset
    66
//          ^       ^
Chengsong
parents:
diff changeset
    67
//          p       q
Chengsong
parents:
diff changeset
    68
//          r
Chengsong
parents:
diff changeset
    69
Chengsong
parents:
diff changeset
    70
//parse r[p...q] --> v
Chengsong
parents:
diff changeset
    71
Chengsong
parents:
diff changeset
    72
Chengsong
parents:
diff changeset
    73
def check_before_down(c: Ctx, r: Rexp, d: Int = 0) : List[Zipper] = {
Chengsong
parents:
diff changeset
    74
    mems.get((pos, r)) match {
Chengsong
parents:
diff changeset
    75
        case Some(m) => 
Chengsong
parents:
diff changeset
    76
            m.parents = c::Nil//c::m.parents
Chengsong
parents:
diff changeset
    77
            m.result.find(tup2 => tup2._1 == pos) match {
Chengsong
parents:
diff changeset
    78
                // case Some((i, v)) => 
Chengsong
parents:
diff changeset
    79
                //   original_up(v, c, d)
Chengsong
parents:
diff changeset
    80
                case None => 
Chengsong
parents:
diff changeset
    81
                  List()
Chengsong
parents:
diff changeset
    82
            }
Chengsong
parents:
diff changeset
    83
        case None => 
Chengsong
parents:
diff changeset
    84
            val m = Mem(c::Nil, MList.empty[(Int, Val)])
Chengsong
parents:
diff changeset
    85
            mems = mems + ((pos, r) -> m)
Chengsong
parents:
diff changeset
    86
            original_down(r, m, d)
Chengsong
parents:
diff changeset
    87
    }
Chengsong
parents:
diff changeset
    88
}
Chengsong
parents:
diff changeset
    89
Chengsong
parents:
diff changeset
    90
Chengsong
parents:
diff changeset
    91
Chengsong
parents:
diff changeset
    92
def mem_up(vres: Val, m: Mem, rec_depth : Int = 0) : List[Zipper] = {
Chengsong
parents:
diff changeset
    93
    m.result += (pos -> vres)
Chengsong
parents:
diff changeset
    94
    m.parents.flatMap((c: Ctx) =>
Chengsong
parents:
diff changeset
    95
        original_up(vres, c, rec_depth)
Chengsong
parents:
diff changeset
    96
    )
Chengsong
parents:
diff changeset
    97
}
Chengsong
parents:
diff changeset
    98
Chengsong
parents:
diff changeset
    99
def original_down(r: Rexp, m: Mem, d: Int = 0) : List[Zipper] = (r, m) match {
Chengsong
parents:
diff changeset
   100
    case (CHAR(b), m) => {
Chengsong
parents:
diff changeset
   101
        if (input(pos) == b) {
Chengsong
parents:
diff changeset
   102
            List((Chr(b), m)) 
Chengsong
parents:
diff changeset
   103
        }
Chengsong
parents:
diff changeset
   104
        else 
Chengsong
parents:
diff changeset
   105
            Nil
Chengsong
parents:
diff changeset
   106
    }
Chengsong
parents:
diff changeset
   107
    case (ONE, m) => mem_up(Empty, m, d + 1)
Chengsong
parents:
diff changeset
   108
    case (SEQ(r1, r2), m) =>
Chengsong
parents:
diff changeset
   109
          
Chengsong
parents:
diff changeset
   110
        val mprime = Mem(AltC(m, x => x )::Nil, MList.empty[(Int, Val)])
Chengsong
parents:
diff changeset
   111
         
Chengsong
parents:
diff changeset
   112
        check_before_down(SeqC(mprime, Nil, List(r2)), r1, d)
Chengsong
parents:
diff changeset
   113
    case (ALT(r1, r2), m) => 
Chengsong
parents:
diff changeset
   114
         
Chengsong
parents:
diff changeset
   115
        check_before_down(AltC(m, Left(_)), r1, d) ::: 
Chengsong
parents:
diff changeset
   116
        check_before_down(AltC(m, Right(_)), r2, d)
Chengsong
parents:
diff changeset
   117
    case (STAR(r0), m) =>
Chengsong
parents:
diff changeset
   118
         
Chengsong
parents:
diff changeset
   119
        check_before_down(StarC(m, Nil, r0), r0, d)
Chengsong
parents:
diff changeset
   120
    case (_, _) => throw new Exception("original down unexpected r or m")
Chengsong
parents:
diff changeset
   121
}
Chengsong
parents:
diff changeset
   122
Chengsong
parents:
diff changeset
   123
def original_up(v: Val, c: Ctx, d: Int = 0) : List[Zipper] = 
Chengsong
parents:
diff changeset
   124
{
Chengsong
parents:
diff changeset
   125
Chengsong
parents:
diff changeset
   126
(v, c) match {
Chengsong
parents:
diff changeset
   127
    case (v, SeqC(m, v1::Nil, Nil)) => 
Chengsong
parents:
diff changeset
   128
        mem_up(Sequ(v1, v), m, d + 1)
Chengsong
parents:
diff changeset
   129
    case (v, SeqC(m, Nil, u1::Nil)) => 
Chengsong
parents:
diff changeset
   130
        check_before_down(SeqC(m, v::Nil, Nil), u1, d)
Chengsong
parents:
diff changeset
   131
    case (v, AltC(m, wrap)) => m.result.find(tup2 => tup2._1 == pos) match {
Chengsong
parents:
diff changeset
   132
        case Some( (i, vPrime)  ) => 
Chengsong
parents:
diff changeset
   133
            m.result += (i -> wrap(v))
Chengsong
parents:
diff changeset
   134
            Nil
Chengsong
parents:
diff changeset
   135
        case None => 
Chengsong
parents:
diff changeset
   136
            mem_up(wrap(v), m, d + 1)
Chengsong
parents:
diff changeset
   137
    } //mem_up(AL1(r), par)
Chengsong
parents:
diff changeset
   138
    //case (v, StarC(m, vs, r0)) => throw new Exception("why not hit starC")
Chengsong
parents:
diff changeset
   139
Chengsong
parents:
diff changeset
   140
    case (v, RootC) => 
Chengsong
parents:
diff changeset
   141
        Nil
Chengsong
parents:
diff changeset
   142
    case (v, StarC(m, vs, r0) ) => mem_up(Stars(v::vs), m, d + 1) ::: 
Chengsong
parents:
diff changeset
   143
        check_before_down(StarC(m, v::vs, r0), r0, d)
Chengsong
parents:
diff changeset
   144
    case (_, _) => throw new Exception("hit unexpected context")
Chengsong
parents:
diff changeset
   145
}
Chengsong
parents:
diff changeset
   146
Chengsong
parents:
diff changeset
   147
}
Chengsong
parents:
diff changeset
   148
Chengsong
parents:
diff changeset
   149
Chengsong
parents:
diff changeset
   150
def derive(p: Int, z: Zipper) : List[Zipper] = {
Chengsong
parents:
diff changeset
   151
    pos = p
Chengsong
parents:
diff changeset
   152
    z match {
Chengsong
parents:
diff changeset
   153
        case (v, m) => mem_up(v, m)
Chengsong
parents:
diff changeset
   154
        case _ => throw new Exception("error")
Chengsong
parents:
diff changeset
   155
    }
Chengsong
parents:
diff changeset
   156
}
Chengsong
parents:
diff changeset
   157
//let e' = Seq([]) in 
Chengsong
parents:
diff changeset
   158
//
Chengsong
parents:
diff changeset
   159
def init_zipper(r: Rexp) : Zipper = {
Chengsong
parents:
diff changeset
   160
    val m_top = Mem(RootC::Nil, MList.empty[(Int, Val)])
Chengsong
parents:
diff changeset
   161
    val c_top = SeqC(m_top, Nil, r::Nil)
Chengsong
parents:
diff changeset
   162
    val m_r = Mem(c_top::Nil, MList.empty[(Int, Val)])
Chengsong
parents:
diff changeset
   163
    println(s"initial zipper is (ZERO, $m_r)")
Chengsong
parents:
diff changeset
   164
    (Empty, m_r)//TODO: which val should we start with? Maybe Empty, maybe doesn't matter
Chengsong
parents:
diff changeset
   165
    // val dummyRexp = ONE
Chengsong
parents:
diff changeset
   166
    // val dummyMem = Mem()
Chengsong
parents:
diff changeset
   167
Chengsong
parents:
diff changeset
   168
}
Chengsong
parents:
diff changeset
   169
Chengsong
parents:
diff changeset
   170
Chengsong
parents:
diff changeset
   171
def plug_convert(v: Val, c: Ctx) : List[Val] = 
Chengsong
parents:
diff changeset
   172
{
Chengsong
parents:
diff changeset
   173
Chengsong
parents:
diff changeset
   174
c match {
Chengsong
parents:
diff changeset
   175
    case RootC => List(v)
Chengsong
parents:
diff changeset
   176
    //TODO: non-binary Seq requires ps.rev
Chengsong
parents:
diff changeset
   177
    case SeqC(m, ps::Nil, Nil) => 
Chengsong
parents:
diff changeset
   178
        plug_mem(Sequ(ps, v), m)
Chengsong
parents:
diff changeset
   179
Chengsong
parents:
diff changeset
   180
    //TODO: un not nullable--partial values?
Chengsong
parents:
diff changeset
   181
    case SeqC(m, Nil, un::Nil) => 
Chengsong
parents:
diff changeset
   182
        if(nullable(un))
Chengsong
parents:
diff changeset
   183
            plug_mem(Sequ(v, mkeps(un)), m)
Chengsong
parents:
diff changeset
   184
        else
Chengsong
parents:
diff changeset
   185
            Nil
Chengsong
parents:
diff changeset
   186
Chengsong
parents:
diff changeset
   187
    //TODO: when multiple results stored in m, which one to choose?
Chengsong
parents:
diff changeset
   188
    case AltC(m, wrap) => 
Chengsong
parents:
diff changeset
   189
        plug_mem(wrap(v), m)
Chengsong
parents:
diff changeset
   190
    case StarC(m, vs, r0) => plug_mem(Stars(v::vs), m)
Chengsong
parents:
diff changeset
   191
}
Chengsong
parents:
diff changeset
   192
Chengsong
parents:
diff changeset
   193
}
Chengsong
parents:
diff changeset
   194
Chengsong
parents:
diff changeset
   195
Chengsong
parents:
diff changeset
   196
var cnt = 0;
Chengsong
parents:
diff changeset
   197
def plug_mem(v: Val, m: Mem) : List[Val] = {
Chengsong
parents:
diff changeset
   198
    m.result += (pos -> v)
Chengsong
parents:
diff changeset
   199
    m.parents.flatMap({c =>
Chengsong
parents:
diff changeset
   200
        plug_convert(v, c)
Chengsong
parents:
diff changeset
   201
    }
Chengsong
parents:
diff changeset
   202
    )
Chengsong
parents:
diff changeset
   203
}
Chengsong
parents:
diff changeset
   204
Chengsong
parents:
diff changeset
   205
def plug_all(zs: List[Zipper]) : List[Val] = {
Chengsong
parents:
diff changeset
   206
    zs.flatMap(z => plug_mem(z._1, z._2))
Chengsong
parents:
diff changeset
   207
}
Chengsong
parents:
diff changeset
   208
Chengsong
parents:
diff changeset
   209
Chengsong
parents:
diff changeset
   210
def mkeps(r: Rexp) : Val = r match {
Chengsong
parents:
diff changeset
   211
  case ONE => Empty
Chengsong
parents:
diff changeset
   212
  case ALT(r1, r2) => 
Chengsong
parents:
diff changeset
   213
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
Chengsong
parents:
diff changeset
   214
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
Chengsong
parents:
diff changeset
   215
  case _ => DummyFilling
Chengsong
parents:
diff changeset
   216
}
Chengsong
parents:
diff changeset
   217
Chengsong
parents:
diff changeset
   218
def nullable(r: Rexp) : Boolean = r match {
Chengsong
parents:
diff changeset
   219
  case ZERO => false
Chengsong
parents:
diff changeset
   220
  case ONE => true
Chengsong
parents:
diff changeset
   221
  case CHAR(_) => false
Chengsong
parents:
diff changeset
   222
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Chengsong
parents:
diff changeset
   223
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Chengsong
parents:
diff changeset
   224
  case _ => false
Chengsong
parents:
diff changeset
   225
}
Chengsong
parents:
diff changeset
   226
Chengsong
parents:
diff changeset
   227
Chengsong
parents:
diff changeset
   228
val tokList : List[Char] = "aab".toList
Chengsong
parents:
diff changeset
   229
var input : List[Char] = tokList
Chengsong
parents:
diff changeset
   230
Chengsong
parents:
diff changeset
   231
Chengsong
parents:
diff changeset
   232
Chengsong
parents:
diff changeset
   233
Chengsong
parents:
diff changeset
   234
Chengsong
parents:
diff changeset
   235
Chengsong
parents:
diff changeset
   236
Chengsong
parents:
diff changeset
   237
def lexRecurse(zs: List[Zipper], index: Int) : List[Zipper] = {
Chengsong
parents:
diff changeset
   238
    if(index <  input.length )
Chengsong
parents:
diff changeset
   239
        lexRecurse(zs.flatMap(z => derive(index, z) ), index + 1)
Chengsong
parents:
diff changeset
   240
    else 
Chengsong
parents:
diff changeset
   241
        zs
Chengsong
parents:
diff changeset
   242
}
Chengsong
parents:
diff changeset
   243
Chengsong
parents:
diff changeset
   244
def lex(r: Rexp, s: String) : List[Zipper] = {
Chengsong
parents:
diff changeset
   245
    input = s.toList
Chengsong
parents:
diff changeset
   246
    
Chengsong
parents:
diff changeset
   247
    lexRecurse(init_zipper(r)::Nil,  0)
Chengsong
parents:
diff changeset
   248
}
Chengsong
parents:
diff changeset
   249
Chengsong
parents:
diff changeset
   250
Chengsong
parents:
diff changeset
   251
Chengsong
parents:
diff changeset
   252
implicit def charlist2rexp(s: List[Char]): Rexp = s match {
Chengsong
parents:
diff changeset
   253
    case Nil => ONE
Chengsong
parents:
diff changeset
   254
    case c::Nil => CHAR(c)
Chengsong
parents:
diff changeset
   255
    case c::cs => SEQ(CHAR(c), charlist2rexp(cs))
Chengsong
parents:
diff changeset
   256
}
Chengsong
parents:
diff changeset
   257
implicit def string2Rexp(s: String) : Rexp = charlist2rexp(s.toList)
Chengsong
parents:
diff changeset
   258
Chengsong
parents:
diff changeset
   259
implicit def RexpOps(r: Rexp) = new {
Chengsong
parents:
diff changeset
   260
    def | (s: Rexp) = ALT(r, s)
Chengsong
parents:
diff changeset
   261
    def ~ (s: Rexp) = SEQ(r, s)
Chengsong
parents:
diff changeset
   262
    def % = STAR(r)
Chengsong
parents:
diff changeset
   263
}
Chengsong
parents:
diff changeset
   264
Chengsong
parents:
diff changeset
   265
implicit def stringOps(s: String) = new {
Chengsong
parents:
diff changeset
   266
    def | (r: Rexp) = ALT(s, r)
Chengsong
parents:
diff changeset
   267
    def | (r: String) = ALT(s, r)
Chengsong
parents:
diff changeset
   268
    def ~ (r: Rexp) = SEQ(s, r)
Chengsong
parents:
diff changeset
   269
    def ~ (r: String) = SEQ(s, r)
Chengsong
parents:
diff changeset
   270
    def % = STAR(s)
Chengsong
parents:
diff changeset
   271
Chengsong
parents:
diff changeset
   272
}
Chengsong
parents:
diff changeset
   273
Chengsong
parents:
diff changeset
   274
//derive(0, init_zipper(re0))
Chengsong
parents:
diff changeset
   275
Chengsong
parents:
diff changeset
   276
// println(re1s.length)
Chengsong
parents:
diff changeset
   277
// mems.foreach(a => println(a))
Chengsong
parents:
diff changeset
   278
// val re1sPlugged = plug_all(re1s)
Chengsong
parents:
diff changeset
   279
// re1sPlugged.foreach(zipper => {
Chengsong
parents:
diff changeset
   280
//                         println(zipper); 
Chengsong
parents:
diff changeset
   281
//                         println("delimit") 
Chengsong
parents:
diff changeset
   282
//                         })
Chengsong
parents:
diff changeset
   283
                
Chengsong
parents:
diff changeset
   284
// mems.clear()
Chengsong
parents:
diff changeset
   285
// println(mems)
Chengsong
parents:
diff changeset
   286
// println(re0)
Chengsong
parents:
diff changeset
   287
// val re2s = lex(re0, "aab")
Chengsong
parents:
diff changeset
   288
// val re2sPlugged = plug_all(re2s)
Chengsong
parents:
diff changeset
   289
// re2sPlugged.foreach(v => {
Chengsong
parents:
diff changeset
   290
//         val Sequ(Empty, vp) = v
Chengsong
parents:
diff changeset
   291
//         println(vp)
Chengsong
parents:
diff changeset
   292
//     }
Chengsong
parents:
diff changeset
   293
// )
Chengsong
parents:
diff changeset
   294
// val re0 = SEQ(ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('a'))), 
Chengsong
parents:
diff changeset
   295
// ALT(SEQ(CHAR('a'), CHAR('b')), SEQ(CHAR('b'), CHAR('c')) )
Chengsong
parents:
diff changeset
   296
// )
Chengsong
parents:
diff changeset
   297
Chengsong
parents:
diff changeset
   298
// val (rgraph, re0root) = makeGraphFromObject(re0)
Chengsong
parents:
diff changeset
   299
// val asciir = GraphLayout.renderGraph(rgraph)
Chengsong
parents:
diff changeset
   300
// println("printing out re0")
Chengsong
parents:
diff changeset
   301
// println(asciir)
Chengsong
parents:
diff changeset
   302
// val re1s = lex(re0, "aabc")
Chengsong
parents:
diff changeset
   303
Chengsong
parents:
diff changeset
   304
def actualZipperSize(zs: List[Zipper]) : Int = zs match {
Chengsong
parents:
diff changeset
   305
    case Nil => 0
Chengsong
parents:
diff changeset
   306
    case z::zs1 => countParents(z._2) + actualZipperSize(zs1)
Chengsong
parents:
diff changeset
   307
}
Chengsong
parents:
diff changeset
   308
Chengsong
parents:
diff changeset
   309
def countParents(m: Mem) : Int = {
Chengsong
parents:
diff changeset
   310
    m.parents.map(c => countGrandParents(c)).sum
Chengsong
parents:
diff changeset
   311
}
Chengsong
parents:
diff changeset
   312
Chengsong
parents:
diff changeset
   313
def countGrandParents(c: Ctx) : Int = {
Chengsong
parents:
diff changeset
   314
    c match {
Chengsong
parents:
diff changeset
   315
        case RootC => 1
Chengsong
parents:
diff changeset
   316
        case SeqC(m, pr, unp) => countParents(m)
Chengsong
parents:
diff changeset
   317
        case AltC(m, w) => countParents(m)
Chengsong
parents:
diff changeset
   318
        case StarC(m, _, _) => countParents(m)
Chengsong
parents:
diff changeset
   319
    }
Chengsong
parents:
diff changeset
   320
}
Chengsong
parents:
diff changeset
   321
Chengsong
parents:
diff changeset
   322
def zipperSimp(zs: List[Zipper]) : List[Zipper] = {
Chengsong
parents:
diff changeset
   323
    zs.distinctBy(z => zipBackMem(z._2))
Chengsong
parents:
diff changeset
   324
}
Chengsong
parents:
diff changeset
   325
Chengsong
parents:
diff changeset
   326
def zipBackToRegex(c: Ctx, r: Rexp = ONE) : List[Rexp] = {
Chengsong
parents:
diff changeset
   327
    c match {
Chengsong
parents:
diff changeset
   328
        case RootC => r::Nil
Chengsong
parents:
diff changeset
   329
        case SeqC(m, pr, Nil) => zipBackMem(m, r)
Chengsong
parents:
diff changeset
   330
        case SeqC(m, pr, unp::Nil) => zipBackMem(m, SEQ(r, unp))
Chengsong
parents:
diff changeset
   331
        case AltC(m, w) => zipBackMem(m, r)
Chengsong
parents:
diff changeset
   332
        case StarC(m, vs, r0) => zipBackMem(m, SEQ(r, STAR(r0)))
Chengsong
parents:
diff changeset
   333
    }
Chengsong
parents:
diff changeset
   334
}
Chengsong
parents:
diff changeset
   335
Chengsong
parents:
diff changeset
   336
def zipBackMem(m: Mem, r: Rexp = ONE) : List[Rexp] = {
Chengsong
parents:
diff changeset
   337
    m.parents.flatMap(c => zipBackToRegex(c, r))
Chengsong
parents:
diff changeset
   338
}
Chengsong
parents:
diff changeset
   339
Chengsong
parents:
diff changeset
   340
//def crystalizeZipper
Chengsong
parents:
diff changeset
   341
Chengsong
parents:
diff changeset
   342
mems.clear()
Chengsong
parents:
diff changeset
   343
val re1 = ("a" | "aa").%
Chengsong
parents:
diff changeset
   344
val re1ss = lex(re1, "aa")
Chengsong
parents:
diff changeset
   345
Chengsong
parents:
diff changeset
   346
// drawZippers(re1ss)
Chengsong
parents:
diff changeset
   347
// println(actualZipperSize(re1ss))
Chengsong
parents:
diff changeset
   348
// println(re1ss)
Chengsong
parents:
diff changeset
   349
//val re1S = zipperSimp(re1ss)
Chengsong
parents:
diff changeset
   350
//println(actualZipperSize(re1S))
Chengsong
parents:
diff changeset
   351
Chengsong
parents:
diff changeset
   352
Chengsong
parents:
diff changeset
   353
val re2 = SEQ(ONE, "a")
Chengsong
parents:
diff changeset
   354
val re2res = lex(re2, "a")
Chengsong
parents:
diff changeset
   355
//lex(1~a, "a") --> lexRecurse((1v, m  (SeqC(m (RootC, Nil) ))))
Chengsong
parents:
diff changeset
   356
Chengsong
parents:
diff changeset
   357
Chengsong
parents:
diff changeset
   358
println(re2res)
Chengsong
parents:
diff changeset
   359
Chengsong
parents:
diff changeset
   360
val re2resPlugged = plug_all(re2res)
Chengsong
parents:
diff changeset
   361
re2resPlugged.foreach(v => {
Chengsong
parents:
diff changeset
   362
        val Sequ(Empty, vp) = v
Chengsong
parents:
diff changeset
   363
        println(vp)
Chengsong
parents:
diff changeset
   364
}
Chengsong
parents:
diff changeset
   365
)
Chengsong
parents:
diff changeset
   366
Chengsong
parents:
diff changeset
   367
// println("remaining regex")
Chengsong
parents:
diff changeset
   368
// println(re1ss.flatMap(z => zipBackMem(z._2)))
Chengsong
parents:
diff changeset
   369
Chengsong
parents:
diff changeset
   370
Chengsong
parents:
diff changeset
   371
// val re1ssPlugged = plug_all(re1ss)
Chengsong
parents:
diff changeset
   372
// println("each of the values")
Chengsong
parents:
diff changeset
   373
// re1ssPlugged.foreach(v => {
Chengsong
parents:
diff changeset
   374
//         //val Sequ(Empty, vp) = v
Chengsong
parents:
diff changeset
   375
//         //println(vp)
Chengsong
parents:
diff changeset
   376
//         println(v)
Chengsong
parents:
diff changeset
   377
//     }
Chengsong
parents:
diff changeset
   378
// )
Chengsong
parents:
diff changeset
   379
// println(mems.size)
Chengsong
parents:
diff changeset
   380
//println(mems)
Chengsong
parents:
diff changeset
   381
//mems.map({case (ir, m) => if (ir._1 == 1 && ir._2 == CHAR('b')) println(printMem(m)) })
Chengsong
parents:
diff changeset
   382
// println("Mkeps + inj:")
Chengsong
parents:
diff changeset
   383
// println(
Chengsong
parents:
diff changeset
   384
//     mems.get((0, re1)) match {
Chengsong
parents:
diff changeset
   385
//         case Some(m) => printMem(m)
Chengsong
parents:
diff changeset
   386
//         case None => ""
Chengsong
parents:
diff changeset
   387
//     }
Chengsong
parents:
diff changeset
   388
//     )
Chengsong
parents:
diff changeset
   389
Chengsong
parents:
diff changeset
   390
// println(re1sPlugged)
Chengsong
parents:
diff changeset
   391
//drawZippers(re1s, plugOrNot = false)
Chengsong
parents:
diff changeset
   392
// re1s.foreach{
Chengsong
parents:
diff changeset
   393
//   re1 => 
Chengsong
parents:
diff changeset
   394
//   {
Chengsong
parents:
diff changeset
   395
Chengsong
parents:
diff changeset
   396
//     drawZippers(derive(1, re1), plugOrNot = true)
Chengsong
parents:
diff changeset
   397
Chengsong
parents:
diff changeset
   398
//   }
Chengsong
parents:
diff changeset
   399
// }
Chengsong
parents:
diff changeset
   400
Chengsong
parents:
diff changeset
   401