thys2/zre7.sc
author Chengsong
Mon, 06 Jun 2022 15:57:17 +0100
changeset 535 ce91c29d2885
parent 394 4b22587fb667
permissions -rw-r--r--
fixed plotting error 6.1
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
394
Chengsong
parents: 393
diff changeset
    11
import pprint._
390
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
391
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    72
//(a+aa)*
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    73
//aaa
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    74
//[R(Sequ(a, a)), vs]
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    75
//[L(a), L(a), vs]
390
Chengsong
parents:
diff changeset
    76
def check_before_down(c: Ctx, r: Rexp, d: Int = 0) : List[Zipper] = {
Chengsong
parents:
diff changeset
    77
    mems.get((pos, r)) match {
Chengsong
parents:
diff changeset
    78
        case Some(m) => 
394
Chengsong
parents: 393
diff changeset
    79
            m.parents = c::m.parents//:::List(c)
390
Chengsong
parents:
diff changeset
    80
            m.result.find(tup2 => tup2._1 == pos) match {
Chengsong
parents:
diff changeset
    81
                // case Some((i, v)) => 
Chengsong
parents:
diff changeset
    82
                //   original_up(v, c, d)
Chengsong
parents:
diff changeset
    83
                case None => 
Chengsong
parents:
diff changeset
    84
                  List()
Chengsong
parents:
diff changeset
    85
            }
Chengsong
parents:
diff changeset
    86
        case None => 
Chengsong
parents:
diff changeset
    87
            val m = Mem(c::Nil, MList.empty[(Int, Val)])
Chengsong
parents:
diff changeset
    88
            mems = mems + ((pos, r) -> m)
Chengsong
parents:
diff changeset
    89
            original_down(r, m, d)
Chengsong
parents:
diff changeset
    90
    }
394
Chengsong
parents: 393
diff changeset
    91
    // val m = Mem(c::Nil, MList.empty[(Int, Val)])
Chengsong
parents: 393
diff changeset
    92
    // original_down(r, m, d)
390
Chengsong
parents:
diff changeset
    93
}
Chengsong
parents:
diff changeset
    94
391
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    95
//mems  pstart r  --> m parents [(pend, vres), ...]
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    96
//aaa
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    97
//012
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    98
//seq a a 
549257d0b8b2 zre7correct
Chengsong
parents: 390
diff changeset
    99
//0 a~a --> m ... [(2, Sequ a a)]
394
Chengsong
parents: 393
diff changeset
   100
        // c match {
Chengsong
parents: 393
diff changeset
   101
        //     case StarC(mst, vst, rst) => print(s"StarC $vst\t")
Chengsong
parents: 393
diff changeset
   102
        //     case SeqC(mse, pr, unp) => print(s"SeqC $unp\t")
Chengsong
parents: 393
diff changeset
   103
        //     case AltC(mal, w) => print(s"AltC ${w(Empty)}\t")
Chengsong
parents: 393
diff changeset
   104
        //     case RootC => print("Root")
Chengsong
parents: 393
diff changeset
   105
        // }
Chengsong
parents: 393
diff changeset
   106
def reorderCtx(cs: List[Ctx]): List[Ctx] = {
Chengsong
parents: 393
diff changeset
   107
    Nil
Chengsong
parents: 393
diff changeset
   108
}
390
Chengsong
parents:
diff changeset
   109
Chengsong
parents:
diff changeset
   110
def mem_up(vres: Val, m: Mem, rec_depth : Int = 0) : List[Zipper] = {
Chengsong
parents:
diff changeset
   111
    m.result += (pos -> vres)
394
Chengsong
parents: 393
diff changeset
   112
    //m.parents = m.parents.reverse
Chengsong
parents: 393
diff changeset
   113
          
Chengsong
parents: 393
diff changeset
   114
    // if(m.parents.size > 1){//println()
Chengsong
parents: 393
diff changeset
   115
    //     println()  
Chengsong
parents: 393
diff changeset
   116
    //     println("each of the contexts")
Chengsong
parents: 393
diff changeset
   117
    //     m.parents.reverse.foreach (c =>
Chengsong
parents: 393
diff changeset
   118
    //         println(simpleCtxDisplay(c))
Chengsong
parents: 393
diff changeset
   119
    //     )
Chengsong
parents: 393
diff changeset
   120
    //     println("after distinctCtx")
Chengsong
parents: 393
diff changeset
   121
    //     distinctCtx(m.parents.reverse).foreach(c =>
Chengsong
parents: 393
diff changeset
   122
    //         println(simpleCtxDisplay(c))
Chengsong
parents: 393
diff changeset
   123
    //     )
Chengsong
parents: 393
diff changeset
   124
    //     //println(s"vs is $vss")
Chengsong
parents: 393
diff changeset
   125
    
Chengsong
parents: 393
diff changeset
   126
    // }
Chengsong
parents: 393
diff changeset
   127
    //.distinctBy(zipBackToRegex(_))
Chengsong
parents: 393
diff changeset
   128
    (m.parents).distinctBy(zipBackToRegex(_)).flatMap((c: Ctx) =>
390
Chengsong
parents:
diff changeset
   129
        original_up(vres, c, rec_depth)
394
Chengsong
parents: 393
diff changeset
   130
    )    
Chengsong
parents: 393
diff changeset
   131
    // m.parents.reverse.flatMap((c: Ctx) =>
Chengsong
parents: 393
diff changeset
   132
    //     original_up(vres, c, rec_depth)
Chengsong
parents: 393
diff changeset
   133
    // )
Chengsong
parents: 393
diff changeset
   134
    // original_up(vres, m.parents.last, rec_depth)
390
Chengsong
parents:
diff changeset
   135
}
Chengsong
parents:
diff changeset
   136
Chengsong
parents:
diff changeset
   137
def original_down(r: Rexp, m: Mem, d: Int = 0) : List[Zipper] = (r, m) match {
Chengsong
parents:
diff changeset
   138
    case (CHAR(b), m) => {
Chengsong
parents:
diff changeset
   139
        if (input(pos) == b) {
Chengsong
parents:
diff changeset
   140
            List((Chr(b), m)) 
Chengsong
parents:
diff changeset
   141
        }
Chengsong
parents:
diff changeset
   142
        else 
Chengsong
parents:
diff changeset
   143
            Nil
Chengsong
parents:
diff changeset
   144
    }
394
Chengsong
parents: 393
diff changeset
   145
    case (ONE, m) => Nil//mem_up(Empty, m, d + 1)
Chengsong
parents: 393
diff changeset
   146
    case (SEQ(r1, r2), m) =>  
Chengsong
parents: 393
diff changeset
   147
        // if(nullable(r1)){
Chengsong
parents: 393
diff changeset
   148
        //     val mprime = Mem(AltC(m, x => x )::Nil, MList.empty[(Int, Val)])
Chengsong
parents: 393
diff changeset
   149
        //     check_before_down(SeqC(mprime, Nil, List(r2)), r1, d) :::
Chengsong
parents: 393
diff changeset
   150
        //     check_before_down(SeqC(mprime, mkeps(r1)::Nil, Nil), r2, d)
Chengsong
parents: 393
diff changeset
   151
        // }
Chengsong
parents: 393
diff changeset
   152
        // else
Chengsong
parents: 393
diff changeset
   153
            check_before_down(SeqC(m, Nil, List(r2)), r1, d)
390
Chengsong
parents:
diff changeset
   154
    case (ALT(r1, r2), m) => 
Chengsong
parents:
diff changeset
   155
        check_before_down(AltC(m, Left(_)), r1, d) ::: 
Chengsong
parents:
diff changeset
   156
        check_before_down(AltC(m, Right(_)), r2, d)
Chengsong
parents:
diff changeset
   157
    case (STAR(r0), m) =>
394
Chengsong
parents: 393
diff changeset
   158
        check_before_down(StarC(m, Nil, r0), r0, d) :::
Chengsong
parents: 393
diff changeset
   159
        mem_up(Stars(Nil), m, d + 1)
390
Chengsong
parents:
diff changeset
   160
    case (_, _) => throw new Exception("original down unexpected r or m")
Chengsong
parents:
diff changeset
   161
}
Chengsong
parents:
diff changeset
   162
Chengsong
parents:
diff changeset
   163
def original_up(v: Val, c: Ctx, d: Int = 0) : List[Zipper] = 
Chengsong
parents:
diff changeset
   164
{
Chengsong
parents:
diff changeset
   165
Chengsong
parents:
diff changeset
   166
(v, c) match {
Chengsong
parents:
diff changeset
   167
    case (v, SeqC(m, v1::Nil, Nil)) => 
Chengsong
parents:
diff changeset
   168
        mem_up(Sequ(v1, v), m, d + 1)
394
Chengsong
parents: 393
diff changeset
   169
    case (v, SeqC(m, vs, u1::Nil)) => 
Chengsong
parents: 393
diff changeset
   170
        check_before_down(SeqC(m, v::vs, Nil), u1, d)
390
Chengsong
parents:
diff changeset
   171
    case (v, AltC(m, wrap)) => m.result.find(tup2 => tup2._1 == pos) match {
Chengsong
parents:
diff changeset
   172
        case Some( (i, vPrime)  ) => 
Chengsong
parents:
diff changeset
   173
            m.result += (i -> wrap(v))
Chengsong
parents:
diff changeset
   174
            Nil
Chengsong
parents:
diff changeset
   175
        case None => 
Chengsong
parents:
diff changeset
   176
            mem_up(wrap(v), m, d + 1)
394
Chengsong
parents: 393
diff changeset
   177
    } //mem_up(AL1(v), par)
390
Chengsong
parents:
diff changeset
   178
    //case (v, StarC(m, vs, r0)) => throw new Exception("why not hit starC")
Chengsong
parents:
diff changeset
   179
Chengsong
parents:
diff changeset
   180
    case (v, RootC) => 
Chengsong
parents:
diff changeset
   181
        Nil
394
Chengsong
parents: 393
diff changeset
   182
    case (v, StarC(m, vs, r0) ) => //mem_up(Stars(v::vs), m, d + 1) //::: 
Chengsong
parents: 393
diff changeset
   183
        check_before_down(StarC(m, v::vs, r0), r0, d) :::
Chengsong
parents: 393
diff changeset
   184
        mem_up(Stars((v::vs).reverse), m, d + 1)
390
Chengsong
parents:
diff changeset
   185
    case (_, _) => throw new Exception("hit unexpected context")
Chengsong
parents:
diff changeset
   186
}
Chengsong
parents:
diff changeset
   187
Chengsong
parents:
diff changeset
   188
}
Chengsong
parents:
diff changeset
   189
Chengsong
parents:
diff changeset
   190
Chengsong
parents:
diff changeset
   191
def derive(p: Int, z: Zipper) : List[Zipper] = {
Chengsong
parents:
diff changeset
   192
    pos = p
394
Chengsong
parents: 393
diff changeset
   193
    //println(s"z's actual size is ${actualZipperSize(z::Nil)}")
Chengsong
parents: 393
diff changeset
   194
    
390
Chengsong
parents:
diff changeset
   195
    z match {
394
Chengsong
parents: 393
diff changeset
   196
        case (v, m) => 
Chengsong
parents: 393
diff changeset
   197
            
Chengsong
parents: 393
diff changeset
   198
            mem_up(v, m)
390
Chengsong
parents:
diff changeset
   199
        case _ => throw new Exception("error")
Chengsong
parents:
diff changeset
   200
    }
Chengsong
parents:
diff changeset
   201
}
Chengsong
parents:
diff changeset
   202
//let e' = Seq([]) in 
Chengsong
parents:
diff changeset
   203
//
Chengsong
parents:
diff changeset
   204
def init_zipper(r: Rexp) : Zipper = {
Chengsong
parents:
diff changeset
   205
    val m_top = Mem(RootC::Nil, MList.empty[(Int, Val)])
Chengsong
parents:
diff changeset
   206
    val c_top = SeqC(m_top, Nil, r::Nil)
Chengsong
parents:
diff changeset
   207
    val m_r = Mem(c_top::Nil, MList.empty[(Int, Val)])
394
Chengsong
parents: 393
diff changeset
   208
    println(s"initial zipper is (Empty, $m_r)")
390
Chengsong
parents:
diff changeset
   209
    (Empty, m_r)//TODO: which val should we start with? Maybe Empty, maybe doesn't matter
Chengsong
parents:
diff changeset
   210
    // val dummyRexp = ONE
Chengsong
parents:
diff changeset
   211
    // val dummyMem = Mem()
Chengsong
parents:
diff changeset
   212
Chengsong
parents:
diff changeset
   213
}
Chengsong
parents:
diff changeset
   214
Chengsong
parents:
diff changeset
   215
Chengsong
parents:
diff changeset
   216
def plug_convert(v: Val, c: Ctx) : List[Val] = 
Chengsong
parents:
diff changeset
   217
{
Chengsong
parents:
diff changeset
   218
Chengsong
parents:
diff changeset
   219
c match {
Chengsong
parents:
diff changeset
   220
    case RootC => List(v)
Chengsong
parents:
diff changeset
   221
    //TODO: non-binary Seq requires ps.rev
Chengsong
parents:
diff changeset
   222
    case SeqC(m, ps::Nil, Nil) => 
Chengsong
parents:
diff changeset
   223
        plug_mem(Sequ(ps, v), m)
Chengsong
parents:
diff changeset
   224
Chengsong
parents:
diff changeset
   225
    //TODO: un not nullable--partial values?
Chengsong
parents:
diff changeset
   226
    case SeqC(m, Nil, un::Nil) => 
Chengsong
parents:
diff changeset
   227
        if(nullable(un))
Chengsong
parents:
diff changeset
   228
            plug_mem(Sequ(v, mkeps(un)), m)
Chengsong
parents:
diff changeset
   229
        else
Chengsong
parents:
diff changeset
   230
            Nil
Chengsong
parents:
diff changeset
   231
Chengsong
parents:
diff changeset
   232
    //TODO: when multiple results stored in m, which one to choose?
Chengsong
parents:
diff changeset
   233
    case AltC(m, wrap) => 
Chengsong
parents:
diff changeset
   234
        plug_mem(wrap(v), m)
394
Chengsong
parents: 393
diff changeset
   235
    case StarC(m, vs, r0) => plug_mem(Stars((v::vs).reverse), m)
390
Chengsong
parents:
diff changeset
   236
}
Chengsong
parents:
diff changeset
   237
Chengsong
parents:
diff changeset
   238
}
Chengsong
parents:
diff changeset
   239
Chengsong
parents:
diff changeset
   240
Chengsong
parents:
diff changeset
   241
var cnt = 0;
Chengsong
parents:
diff changeset
   242
def plug_mem(v: Val, m: Mem) : List[Val] = {
Chengsong
parents:
diff changeset
   243
    m.result += (pos -> v)
Chengsong
parents:
diff changeset
   244
    m.parents.flatMap({c =>
Chengsong
parents:
diff changeset
   245
        plug_convert(v, c)
Chengsong
parents:
diff changeset
   246
    }
Chengsong
parents:
diff changeset
   247
    )
Chengsong
parents:
diff changeset
   248
}
Chengsong
parents:
diff changeset
   249
Chengsong
parents:
diff changeset
   250
def plug_all(zs: List[Zipper]) : List[Val] = {
Chengsong
parents:
diff changeset
   251
    zs.flatMap(z => plug_mem(z._1, z._2))
Chengsong
parents:
diff changeset
   252
}
Chengsong
parents:
diff changeset
   253
Chengsong
parents:
diff changeset
   254
Chengsong
parents:
diff changeset
   255
def mkeps(r: Rexp) : Val = r match {
Chengsong
parents:
diff changeset
   256
  case ONE => Empty
Chengsong
parents:
diff changeset
   257
  case ALT(r1, r2) => 
Chengsong
parents:
diff changeset
   258
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
Chengsong
parents:
diff changeset
   259
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
Chengsong
parents:
diff changeset
   260
  case _ => DummyFilling
Chengsong
parents:
diff changeset
   261
}
Chengsong
parents:
diff changeset
   262
Chengsong
parents:
diff changeset
   263
def nullable(r: Rexp) : Boolean = r match {
Chengsong
parents:
diff changeset
   264
  case ZERO => false
Chengsong
parents:
diff changeset
   265
  case ONE => true
Chengsong
parents:
diff changeset
   266
  case CHAR(_) => false
Chengsong
parents:
diff changeset
   267
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Chengsong
parents:
diff changeset
   268
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Chengsong
parents:
diff changeset
   269
  case _ => false
Chengsong
parents:
diff changeset
   270
}
Chengsong
parents:
diff changeset
   271
Chengsong
parents:
diff changeset
   272
Chengsong
parents:
diff changeset
   273
val tokList : List[Char] = "aab".toList
Chengsong
parents:
diff changeset
   274
var input : List[Char] = tokList
Chengsong
parents:
diff changeset
   275
Chengsong
parents:
diff changeset
   276
Chengsong
parents:
diff changeset
   277
Chengsong
parents:
diff changeset
   278
Chengsong
parents:
diff changeset
   279
Chengsong
parents:
diff changeset
   280
Chengsong
parents:
diff changeset
   281
Chengsong
parents:
diff changeset
   282
def lexRecurse(zs: List[Zipper], index: Int) : List[Zipper] = {
Chengsong
parents:
diff changeset
   283
    if(index <  input.length )
Chengsong
parents:
diff changeset
   284
        lexRecurse(zs.flatMap(z => derive(index, z) ), index + 1)
Chengsong
parents:
diff changeset
   285
    else 
Chengsong
parents:
diff changeset
   286
        zs
Chengsong
parents:
diff changeset
   287
}
Chengsong
parents:
diff changeset
   288
Chengsong
parents:
diff changeset
   289
def lex(r: Rexp, s: String) : List[Zipper] = {
Chengsong
parents:
diff changeset
   290
    input = s.toList
Chengsong
parents:
diff changeset
   291
    
Chengsong
parents:
diff changeset
   292
    lexRecurse(init_zipper(r)::Nil,  0)
Chengsong
parents:
diff changeset
   293
}
Chengsong
parents:
diff changeset
   294
Chengsong
parents:
diff changeset
   295
Chengsong
parents:
diff changeset
   296
Chengsong
parents:
diff changeset
   297
implicit def charlist2rexp(s: List[Char]): Rexp = s match {
Chengsong
parents:
diff changeset
   298
    case Nil => ONE
Chengsong
parents:
diff changeset
   299
    case c::Nil => CHAR(c)
Chengsong
parents:
diff changeset
   300
    case c::cs => SEQ(CHAR(c), charlist2rexp(cs))
Chengsong
parents:
diff changeset
   301
}
Chengsong
parents:
diff changeset
   302
implicit def string2Rexp(s: String) : Rexp = charlist2rexp(s.toList)
Chengsong
parents:
diff changeset
   303
Chengsong
parents:
diff changeset
   304
implicit def RexpOps(r: Rexp) = new {
Chengsong
parents:
diff changeset
   305
    def | (s: Rexp) = ALT(r, s)
Chengsong
parents:
diff changeset
   306
    def ~ (s: Rexp) = SEQ(r, s)
Chengsong
parents:
diff changeset
   307
    def % = STAR(r)
Chengsong
parents:
diff changeset
   308
}
Chengsong
parents:
diff changeset
   309
Chengsong
parents:
diff changeset
   310
implicit def stringOps(s: String) = new {
Chengsong
parents:
diff changeset
   311
    def | (r: Rexp) = ALT(s, r)
Chengsong
parents:
diff changeset
   312
    def | (r: String) = ALT(s, r)
Chengsong
parents:
diff changeset
   313
    def ~ (r: Rexp) = SEQ(s, r)
Chengsong
parents:
diff changeset
   314
    def ~ (r: String) = SEQ(s, r)
Chengsong
parents:
diff changeset
   315
    def % = STAR(s)
Chengsong
parents:
diff changeset
   316
Chengsong
parents:
diff changeset
   317
}
Chengsong
parents:
diff changeset
   318
Chengsong
parents:
diff changeset
   319
//derive(0, init_zipper(re0))
Chengsong
parents:
diff changeset
   320
Chengsong
parents:
diff changeset
   321
// println(re1s.length)
Chengsong
parents:
diff changeset
   322
// mems.foreach(a => println(a))
Chengsong
parents:
diff changeset
   323
// val re1sPlugged = plug_all(re1s)
Chengsong
parents:
diff changeset
   324
// re1sPlugged.foreach(zipper => {
Chengsong
parents:
diff changeset
   325
//                         println(zipper); 
Chengsong
parents:
diff changeset
   326
//                         println("delimit") 
Chengsong
parents:
diff changeset
   327
//                         })
Chengsong
parents:
diff changeset
   328
                
Chengsong
parents:
diff changeset
   329
// mems.clear()
Chengsong
parents:
diff changeset
   330
// println(mems)
Chengsong
parents:
diff changeset
   331
// println(re0)
Chengsong
parents:
diff changeset
   332
// val re2s = lex(re0, "aab")
Chengsong
parents:
diff changeset
   333
// val re2sPlugged = plug_all(re2s)
Chengsong
parents:
diff changeset
   334
// re2sPlugged.foreach(v => {
Chengsong
parents:
diff changeset
   335
//         val Sequ(Empty, vp) = v
Chengsong
parents:
diff changeset
   336
//         println(vp)
Chengsong
parents:
diff changeset
   337
//     }
Chengsong
parents:
diff changeset
   338
// )
Chengsong
parents:
diff changeset
   339
// val re0 = SEQ(ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('a'))), 
Chengsong
parents:
diff changeset
   340
// ALT(SEQ(CHAR('a'), CHAR('b')), SEQ(CHAR('b'), CHAR('c')) )
Chengsong
parents:
diff changeset
   341
// )
Chengsong
parents:
diff changeset
   342
Chengsong
parents:
diff changeset
   343
// val (rgraph, re0root) = makeGraphFromObject(re0)
Chengsong
parents:
diff changeset
   344
// val asciir = GraphLayout.renderGraph(rgraph)
Chengsong
parents:
diff changeset
   345
// println("printing out re0")
Chengsong
parents:
diff changeset
   346
// println(asciir)
Chengsong
parents:
diff changeset
   347
// val re1s = lex(re0, "aabc")
394
Chengsong
parents: 393
diff changeset
   348
 
390
Chengsong
parents:
diff changeset
   349
def actualZipperSize(zs: List[Zipper]) : Int = zs match {
Chengsong
parents:
diff changeset
   350
    case Nil => 0
Chengsong
parents:
diff changeset
   351
    case z::zs1 => countParents(z._2) + actualZipperSize(zs1)
Chengsong
parents:
diff changeset
   352
}
Chengsong
parents:
diff changeset
   353
Chengsong
parents:
diff changeset
   354
def countParents(m: Mem) : Int = {
Chengsong
parents:
diff changeset
   355
    m.parents.map(c => countGrandParents(c)).sum
Chengsong
parents:
diff changeset
   356
}
Chengsong
parents:
diff changeset
   357
Chengsong
parents:
diff changeset
   358
def countGrandParents(c: Ctx) : Int = {
Chengsong
parents:
diff changeset
   359
    c match {
Chengsong
parents:
diff changeset
   360
        case RootC => 1
Chengsong
parents:
diff changeset
   361
        case SeqC(m, pr, unp) => countParents(m)
Chengsong
parents:
diff changeset
   362
        case AltC(m, w) => countParents(m)
Chengsong
parents:
diff changeset
   363
        case StarC(m, _, _) => countParents(m)
Chengsong
parents:
diff changeset
   364
    }
Chengsong
parents:
diff changeset
   365
}
394
Chengsong
parents: 393
diff changeset
   366
//(a+aa)* \a --> (1 + a)(a+aa)* --> (a+aa)* + (1+a)(a+aa)*
390
Chengsong
parents:
diff changeset
   367
394
Chengsong
parents: 393
diff changeset
   368
//a(a+aa)* + 1(a+aa)* + (a+aa)*
390
Chengsong
parents:
diff changeset
   369
394
Chengsong
parents: 393
diff changeset
   370
//a~(a + aa)* \a -> 1 ~ (a + aa)* 
Chengsong
parents: 393
diff changeset
   371
//va <-----> m --> SeqC(m1, pr, "a") --> AltC(m4, Right)--> StarC(m2, vs, "a" + "aa") --> SeqC(m) ---> Root
Chengsong
parents: 393
diff changeset
   372
//           ^
Chengsong
parents: 393
diff changeset
   373
//           ---> AltC(m4, Left) 
Chengsong
parents: 393
diff changeset
   374
def zipBackToRegex(c: Ctx, r: Rexp = ONE) : Rexp = {
390
Chengsong
parents:
diff changeset
   375
    c match {
394
Chengsong
parents: 393
diff changeset
   376
        case RootC => r
Chengsong
parents: 393
diff changeset
   377
        case SeqC(m, pr, Nil) => zipBackToRegex(m.parents.head, r)
Chengsong
parents: 393
diff changeset
   378
        case SeqC(m, pr, unp::Nil) => zipBackToRegex(m.parents.head, SEQ(r, unp))
Chengsong
parents: 393
diff changeset
   379
        case AltC(m, w) => zipBackToRegex(m.parents.head, r)
Chengsong
parents: 393
diff changeset
   380
        case StarC(m, vs, r0) => zipBackToRegex(m.parents.head, SEQ(r, STAR(r0)))
390
Chengsong
parents:
diff changeset
   381
    }
Chengsong
parents:
diff changeset
   382
}
Chengsong
parents:
diff changeset
   383
394
Chengsong
parents: 393
diff changeset
   384
def zipperSimp(z: Zipper) : Unit = z match {
Chengsong
parents: 393
diff changeset
   385
    case (v, m) => //m.parents = m.parents.distinctBy(c => zipBackToRegex(c))
390
Chengsong
parents:
diff changeset
   386
}
Chengsong
parents:
diff changeset
   387
394
Chengsong
parents: 393
diff changeset
   388
def distinctCtx(cs: List[Ctx]) : List[Ctx] = cs.distinctBy(c => zipBackToRegex(c))
Chengsong
parents: 393
diff changeset
   389
Chengsong
parents: 393
diff changeset
   390
Chengsong
parents: 393
diff changeset
   391
def simpleCtxDisplay(c: Ctx, indent : Int = 0) : String = c match {
Chengsong
parents: 393
diff changeset
   392
    case SeqC(m, pr, unp) => "Sc[m:" ++ printMem(m, indent + 1) ++ 
Chengsong
parents: 393
diff changeset
   393
        "pr:" ++ pr.map(v => shortValOutput(v)).mkString(", ") ++ " unp:" ++ unp.map(r2 => shortRexpOutput(r2)).mkString(", ") ++ "]"
Chengsong
parents: 393
diff changeset
   394
    case AltC(m, w) =>
Chengsong
parents: 393
diff changeset
   395
        w(Empty) match {
Chengsong
parents: 393
diff changeset
   396
            case Left(_) => s"Ac(m:${printMem(m, indent + 1)}, Left(_))"
Chengsong
parents: 393
diff changeset
   397
            case Right(_) => s"Ac(m:${printMem(m, indent + 1)}, Right(_))"
Chengsong
parents: 393
diff changeset
   398
            case Empty => s"Ac(m:${printMem(m, indent + 1)}, id)"
Chengsong
parents: 393
diff changeset
   399
        } 
Chengsong
parents: 393
diff changeset
   400
    case StarC(m, vs, r0) => s"StarC[m:" ++ printMem(m, indent + 1) ++ 
Chengsong
parents: 393
diff changeset
   401
        "vs:" ++ vs.map(v => shortValOutput(v)).mkString(", ") ++ " r0: " ++ shortRexpOutput(r0)
Chengsong
parents: 393
diff changeset
   402
    case RootC => "Root"
Chengsong
parents: 393
diff changeset
   403
    //case AL1(r) => s"(+${shortRexpOutput(r)})"
Chengsong
parents: 393
diff changeset
   404
    //case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
Chengsong
parents: 393
diff changeset
   405
    //case RTOP => "RTOP"
Chengsong
parents: 393
diff changeset
   406
  }
Chengsong
parents: 393
diff changeset
   407
Chengsong
parents: 393
diff changeset
   408
def printMem(m: Mem, indent: Int = 0) : String = {
Chengsong
parents: 393
diff changeset
   409
   "M(par:" ++
Chengsong
parents: 393
diff changeset
   410
   m.parents.map(c => simpleCtxDisplay(c, indent + 1)).mkString("(",",", ")")  ++
Chengsong
parents: 393
diff changeset
   411
  (", results:")  ++
Chengsong
parents: 393
diff changeset
   412
  (for(iRexp <- m.result) 
Chengsong
parents: 393
diff changeset
   413
    yield iRexp match {case (p: Int, v: Val) => s"$p->${shortValOutput(v)}"}
Chengsong
parents: 393
diff changeset
   414
  ).mkString("(",",", ")")  ++ 
Chengsong
parents: 393
diff changeset
   415
   ")" 
Chengsong
parents: 393
diff changeset
   416
}
Chengsong
parents: 393
diff changeset
   417
Chengsong
parents: 393
diff changeset
   418
def shortRexpOutput(r: Rexp) : String = r match {
Chengsong
parents: 393
diff changeset
   419
    case CHAR(c) => c.toString
Chengsong
parents: 393
diff changeset
   420
    case ONE => "1"
Chengsong
parents: 393
diff changeset
   421
    case ZERO => "0"
Chengsong
parents: 393
diff changeset
   422
    case SEQ(r1, r2) => "[" ++ shortRexpOutput(r1) ++ "~" ++ shortRexpOutput(r2) ++ "]"
Chengsong
parents: 393
diff changeset
   423
    case ALT(r1, r2) => "(" ++ shortRexpOutput(r1) ++ "+" ++ shortRexpOutput(r2) ++ ")"
Chengsong
parents: 393
diff changeset
   424
    case STAR(r) => "[" ++ shortRexpOutput(r) ++ "]*"
Chengsong
parents: 393
diff changeset
   425
    //case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
Chengsong
parents: 393
diff changeset
   426
    case RTOP => "RTOP"
Chengsong
parents: 393
diff changeset
   427
  }
Chengsong
parents: 393
diff changeset
   428
Chengsong
parents: 393
diff changeset
   429
def shortValOutput(v: Val) : String = v match {
Chengsong
parents: 393
diff changeset
   430
    case Left(v) => "L(" ++ shortValOutput(v) ++ ")"
Chengsong
parents: 393
diff changeset
   431
    case Right(v) => "R(" ++ shortValOutput(v) ++ ")"
Chengsong
parents: 393
diff changeset
   432
    case Empty => "e"
Chengsong
parents: 393
diff changeset
   433
    case Sequ(v1, v2) => "[" ++ shortValOutput(v1) ++ "~" ++ shortValOutput(v2) ++ "]"
Chengsong
parents: 393
diff changeset
   434
    case Chr(a) => a.toString
Chengsong
parents: 393
diff changeset
   435
    case Stars(vs) => "Stars" ++ vs.map(shortValOutput(_)).mkString("[", ",", "]")
Chengsong
parents: 393
diff changeset
   436
    case _ => "???"
Chengsong
parents: 393
diff changeset
   437
}
Chengsong
parents: 393
diff changeset
   438
Chengsong
parents: 393
diff changeset
   439
390
Chengsong
parents:
diff changeset
   440
//def crystalizeZipper
Chengsong
parents:
diff changeset
   441
394
Chengsong
parents: 393
diff changeset
   442
for(i <- 1 to 10) {
Chengsong
parents: 393
diff changeset
   443
    mems.clear()
Chengsong
parents: 393
diff changeset
   444
println(s"there are $i number of a's")
Chengsong
parents: 393
diff changeset
   445
val re1 = (("a" | "b") ~ "c" | ("b" | "e") ~ "c" ) ~ "f"//("a" | "aa" |"ab").%
Chengsong
parents: 393
diff changeset
   446
val re1Lexed = lex(re1, "bcf")//"a"*i+"b")
Chengsong
parents: 393
diff changeset
   447
Chengsong
parents: 393
diff changeset
   448
//drawZippers(re1Lexed)
Chengsong
parents: 393
diff changeset
   449
println("size of actual zipper (including memoized contexts")
Chengsong
parents: 393
diff changeset
   450
println(actualZipperSize(re1Lexed))
Chengsong
parents: 393
diff changeset
   451
//println(re1Lexed)
Chengsong
parents: 393
diff changeset
   452
//re1Lexed.foreach(zipperSimp(_))
Chengsong
parents: 393
diff changeset
   453
//println(actualZipperSize(re1S))
Chengsong
parents: 393
diff changeset
   454
val re1resPlugged = plug_all(re1Lexed)
Chengsong
parents: 393
diff changeset
   455
//println(actualZipperSize(re1Lexed))
390
Chengsong
parents:
diff changeset
   456
394
Chengsong
parents: 393
diff changeset
   457
println("value extracted")
Chengsong
parents: 393
diff changeset
   458
re1resPlugged.foreach(v => {
Chengsong
parents: 393
diff changeset
   459
        val Sequ(Empty, vp) = v
Chengsong
parents: 393
diff changeset
   460
        println(vp)
Chengsong
parents: 393
diff changeset
   461
}
Chengsong
parents: 393
diff changeset
   462
)
390
Chengsong
parents:
diff changeset
   463
394
Chengsong
parents: 393
diff changeset
   464
  val mb = 1024*1024
Chengsong
parents: 393
diff changeset
   465
val runtime = Runtime.getRuntime
Chengsong
parents: 393
diff changeset
   466
println("ALL RESULTS IN MB")
Chengsong
parents: 393
diff changeset
   467
println("** Used Memory:  " + (runtime.totalMemory - runtime.freeMemory) / mb)
Chengsong
parents: 393
diff changeset
   468
println("** Free Memory:  " + runtime.freeMemory / mb)
Chengsong
parents: 393
diff changeset
   469
println("** Total Memory: " + runtime.totalMemory / mb)
Chengsong
parents: 393
diff changeset
   470
println("** Max Memory:   " + runtime.maxMemory / mb)
Chengsong
parents: 393
diff changeset
   471
Chengsong
parents: 393
diff changeset
   472
}
390
Chengsong
parents:
diff changeset
   473
393
3954579ebdaf polished
Christian Urban <christian.urban@kcl.ac.uk>
parents: 391
diff changeset
   474
mems.clear()
394
Chengsong
parents: 393
diff changeset
   475
val re2 = SEQ(ONE, "a")
393
3954579ebdaf polished
Christian Urban <christian.urban@kcl.ac.uk>
parents: 391
diff changeset
   476
val re2res = lex(re2, "a")
394
Chengsong
parents: 393
diff changeset
   477
//lex(1~a, "a") --> lexRecurse((1v, m  (SeqC(m (RootC, Nil), Nil, [1~a] ) )))
390
Chengsong
parents:
diff changeset
   478
Chengsong
parents:
diff changeset
   479
393
3954579ebdaf polished
Christian Urban <christian.urban@kcl.ac.uk>
parents: 391
diff changeset
   480
println(re2res)
390
Chengsong
parents:
diff changeset
   481
393
3954579ebdaf polished
Christian Urban <christian.urban@kcl.ac.uk>
parents: 391
diff changeset
   482
val re2resPlugged = plug_all(re2res)
394
Chengsong
parents: 393
diff changeset
   483
re2resPlugged.foreach(v => {
Chengsong
parents: 393
diff changeset
   484
        val Sequ(Empty, vp) = v
Chengsong
parents: 393
diff changeset
   485
        println(vp)
Chengsong
parents: 393
diff changeset
   486
}
Chengsong
parents: 393
diff changeset
   487
)
390
Chengsong
parents:
diff changeset
   488
Chengsong
parents:
diff changeset
   489
// println("remaining regex")
Chengsong
parents:
diff changeset
   490
// println(re1ss.flatMap(z => zipBackMem(z._2)))
Chengsong
parents:
diff changeset
   491
Chengsong
parents:
diff changeset
   492
Chengsong
parents:
diff changeset
   493
// val re1ssPlugged = plug_all(re1ss)
Chengsong
parents:
diff changeset
   494
// println("each of the values")
Chengsong
parents:
diff changeset
   495
// re1ssPlugged.foreach(v => {
Chengsong
parents:
diff changeset
   496
//         //val Sequ(Empty, vp) = v
Chengsong
parents:
diff changeset
   497
//         //println(vp)
Chengsong
parents:
diff changeset
   498
//         println(v)
Chengsong
parents:
diff changeset
   499
//     }
Chengsong
parents:
diff changeset
   500
// )
Chengsong
parents:
diff changeset
   501
// println(mems.size)
Chengsong
parents:
diff changeset
   502
//println(mems)
Chengsong
parents:
diff changeset
   503
//mems.map({case (ir, m) => if (ir._1 == 1 && ir._2 == CHAR('b')) println(printMem(m)) })
Chengsong
parents:
diff changeset
   504
// println("Mkeps + inj:")
Chengsong
parents:
diff changeset
   505
// println(
Chengsong
parents:
diff changeset
   506
//     mems.get((0, re1)) match {
Chengsong
parents:
diff changeset
   507
//         case Some(m) => printMem(m)
Chengsong
parents:
diff changeset
   508
//         case None => ""
Chengsong
parents:
diff changeset
   509
//     }
Chengsong
parents:
diff changeset
   510
//     )
Chengsong
parents:
diff changeset
   511
Chengsong
parents:
diff changeset
   512
// println(re1sPlugged)
Chengsong
parents:
diff changeset
   513
//drawZippers(re1s, plugOrNot = false)
Chengsong
parents:
diff changeset
   514
// re1s.foreach{
Chengsong
parents:
diff changeset
   515
//   re1 => 
Chengsong
parents:
diff changeset
   516
//   {
Chengsong
parents:
diff changeset
   517
Chengsong
parents:
diff changeset
   518
//     drawZippers(derive(1, re1), plugOrNot = true)
Chengsong
parents:
diff changeset
   519
Chengsong
parents:
diff changeset
   520
//   }
Chengsong
parents:
diff changeset
   521
// }
Chengsong
parents:
diff changeset
   522
Chengsong
parents:
diff changeset
   523