thys2/blexer2.sc
author Chengsong
Tue, 01 Mar 2022 11:14:17 +0000
changeset 435 65e786a58365
parent 432 994403dbbed5
child 492 61eff2abb0b6
permissions -rw-r--r--
hi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
431
Chengsong
parents:
diff changeset
     1
// A simple lexer inspired by work of Sulzmann & Lu
Chengsong
parents:
diff changeset
     2
//==================================================
Chengsong
parents:
diff changeset
     3
//
Chengsong
parents:
diff changeset
     4
// Call the test cases with 
Chengsong
parents:
diff changeset
     5
//
Chengsong
parents:
diff changeset
     6
//   amm lexer.sc small
Chengsong
parents:
diff changeset
     7
//   amm lexer.sc fib
Chengsong
parents:
diff changeset
     8
//   amm lexer.sc loops
Chengsong
parents:
diff changeset
     9
//   amm lexer.sc email
Chengsong
parents:
diff changeset
    10
//
Chengsong
parents:
diff changeset
    11
//   amm lexer.sc all
Chengsong
parents:
diff changeset
    12
Chengsong
parents:
diff changeset
    13
Chengsong
parents:
diff changeset
    14
// regular expressions including records
Chengsong
parents:
diff changeset
    15
abstract class Rexp 
Chengsong
parents:
diff changeset
    16
case object ZERO extends Rexp
Chengsong
parents:
diff changeset
    17
case object ONE extends Rexp
Chengsong
parents:
diff changeset
    18
case object ANYCHAR extends Rexp
Chengsong
parents:
diff changeset
    19
case class CHAR(c: Char) extends Rexp
Chengsong
parents:
diff changeset
    20
case class ALTS(r1: Rexp, r2: Rexp) extends Rexp 
Chengsong
parents:
diff changeset
    21
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Chengsong
parents:
diff changeset
    22
case class STAR(r: Rexp) extends Rexp 
Chengsong
parents:
diff changeset
    23
case class RECD(x: String, r: Rexp) extends Rexp  
Chengsong
parents:
diff changeset
    24
case class NTIMES(n: Int, r: Rexp) extends Rexp
Chengsong
parents:
diff changeset
    25
case class OPTIONAL(r: Rexp) extends Rexp
Chengsong
parents:
diff changeset
    26
case class NOT(r: Rexp) extends Rexp
Chengsong
parents:
diff changeset
    27
                // records for extracting strings or tokens
Chengsong
parents:
diff changeset
    28
  
Chengsong
parents:
diff changeset
    29
// values  
Chengsong
parents:
diff changeset
    30
abstract class Val
Chengsong
parents:
diff changeset
    31
case object Empty extends Val
Chengsong
parents:
diff changeset
    32
case class Chr(c: Char) extends Val
Chengsong
parents:
diff changeset
    33
case class Sequ(v1: Val, v2: Val) extends Val
Chengsong
parents:
diff changeset
    34
case class Left(v: Val) extends Val
Chengsong
parents:
diff changeset
    35
case class Right(v: Val) extends Val
Chengsong
parents:
diff changeset
    36
case class Stars(vs: List[Val]) extends Val
Chengsong
parents:
diff changeset
    37
case class Rec(x: String, v: Val) extends Val
Chengsong
parents:
diff changeset
    38
case class Ntime(vs: List[Val]) extends Val
Chengsong
parents:
diff changeset
    39
case class Optionall(v: Val) extends Val
Chengsong
parents:
diff changeset
    40
case class Nots(s: String) extends Val
Chengsong
parents:
diff changeset
    41
Chengsong
parents:
diff changeset
    42
Chengsong
parents:
diff changeset
    43
Chengsong
parents:
diff changeset
    44
abstract class Bit
Chengsong
parents:
diff changeset
    45
case object Z extends Bit
Chengsong
parents:
diff changeset
    46
case object S extends Bit
Chengsong
parents:
diff changeset
    47
Chengsong
parents:
diff changeset
    48
Chengsong
parents:
diff changeset
    49
type Bits = List[Bit]
Chengsong
parents:
diff changeset
    50
Chengsong
parents:
diff changeset
    51
abstract class ARexp 
Chengsong
parents:
diff changeset
    52
case object AZERO extends ARexp
Chengsong
parents:
diff changeset
    53
case class AONE(bs: Bits) extends ARexp
Chengsong
parents:
diff changeset
    54
case class ACHAR(bs: Bits, c: Char) extends ARexp
Chengsong
parents:
diff changeset
    55
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
Chengsong
parents:
diff changeset
    56
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
Chengsong
parents:
diff changeset
    57
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
Chengsong
parents:
diff changeset
    58
case class ANOT(bs: Bits, r: ARexp) extends ARexp
Chengsong
parents:
diff changeset
    59
case class AANYCHAR(bs: Bits) extends ARexp
Chengsong
parents:
diff changeset
    60
Chengsong
parents:
diff changeset
    61
Chengsong
parents:
diff changeset
    62
   
Chengsong
parents:
diff changeset
    63
// some convenience for typing in regular expressions
Chengsong
parents:
diff changeset
    64
Chengsong
parents:
diff changeset
    65
def charlist2rexp(s : List[Char]): Rexp = s match {
Chengsong
parents:
diff changeset
    66
  case Nil => ONE
Chengsong
parents:
diff changeset
    67
  case c::Nil => CHAR(c)
Chengsong
parents:
diff changeset
    68
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Chengsong
parents:
diff changeset
    69
}
Chengsong
parents:
diff changeset
    70
implicit def string2rexp(s : String) : Rexp = 
Chengsong
parents:
diff changeset
    71
  charlist2rexp(s.toList)
Chengsong
parents:
diff changeset
    72
Chengsong
parents:
diff changeset
    73
implicit def RexpOps(r: Rexp) = new {
Chengsong
parents:
diff changeset
    74
  def | (s: Rexp) = ALTS(r, s)
Chengsong
parents:
diff changeset
    75
  def % = STAR(r)
Chengsong
parents:
diff changeset
    76
  def ~ (s: Rexp) = SEQ(r, s)
Chengsong
parents:
diff changeset
    77
}
Chengsong
parents:
diff changeset
    78
Chengsong
parents:
diff changeset
    79
implicit def stringOps(s: String) = new {
Chengsong
parents:
diff changeset
    80
  def | (r: Rexp) = ALTS(s, r)
Chengsong
parents:
diff changeset
    81
  def | (r: String) = ALTS(s, r)
Chengsong
parents:
diff changeset
    82
  def % = STAR(s)
Chengsong
parents:
diff changeset
    83
  def ~ (r: Rexp) = SEQ(s, r)
Chengsong
parents:
diff changeset
    84
  def ~ (r: String) = SEQ(s, r)
Chengsong
parents:
diff changeset
    85
  def $ (r: Rexp) = RECD(s, r)
Chengsong
parents:
diff changeset
    86
}
Chengsong
parents:
diff changeset
    87
Chengsong
parents:
diff changeset
    88
def nullable(r: Rexp) : Boolean = r match {
Chengsong
parents:
diff changeset
    89
  case ZERO => false
Chengsong
parents:
diff changeset
    90
  case ONE => true
Chengsong
parents:
diff changeset
    91
  case CHAR(_) => false
Chengsong
parents:
diff changeset
    92
  case ANYCHAR => false
Chengsong
parents:
diff changeset
    93
  case ALTS(r1, r2) => nullable(r1) || nullable(r2)
Chengsong
parents:
diff changeset
    94
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Chengsong
parents:
diff changeset
    95
  case STAR(_) => true
Chengsong
parents:
diff changeset
    96
  case RECD(_, r1) => nullable(r1)
Chengsong
parents:
diff changeset
    97
  case NTIMES(n, r) => if (n == 0) true else nullable(r)
Chengsong
parents:
diff changeset
    98
  case OPTIONAL(r) => true
Chengsong
parents:
diff changeset
    99
  case NOT(r) => !nullable(r)
Chengsong
parents:
diff changeset
   100
}
Chengsong
parents:
diff changeset
   101
Chengsong
parents:
diff changeset
   102
def der(c: Char, r: Rexp) : Rexp = r match {
Chengsong
parents:
diff changeset
   103
  case ZERO => ZERO
Chengsong
parents:
diff changeset
   104
  case ONE => ZERO
Chengsong
parents:
diff changeset
   105
  case CHAR(d) => if (c == d) ONE else ZERO
Chengsong
parents:
diff changeset
   106
  case ANYCHAR => ONE 
Chengsong
parents:
diff changeset
   107
  case ALTS(r1, r2) => ALTS(der(c, r1), der(c, r2))
Chengsong
parents:
diff changeset
   108
  case SEQ(r1, r2) => 
Chengsong
parents:
diff changeset
   109
    if (nullable(r1)) ALTS(SEQ(der(c, r1), r2), der(c, r2))
Chengsong
parents:
diff changeset
   110
    else SEQ(der(c, r1), r2)
Chengsong
parents:
diff changeset
   111
  case STAR(r) => SEQ(der(c, r), STAR(r))
Chengsong
parents:
diff changeset
   112
  case RECD(_, r1) => der(c, r1)
Chengsong
parents:
diff changeset
   113
  case NTIMES(n, r) => if(n > 0) SEQ(der(c, r), NTIMES(n - 1, r)) else ZERO
Chengsong
parents:
diff changeset
   114
  case OPTIONAL(r) => der(c, r)
Chengsong
parents:
diff changeset
   115
  case NOT(r) =>  NOT(der(c, r))
Chengsong
parents:
diff changeset
   116
}
Chengsong
parents:
diff changeset
   117
Chengsong
parents:
diff changeset
   118
Chengsong
parents:
diff changeset
   119
// extracts a string from a value
Chengsong
parents:
diff changeset
   120
def flatten(v: Val) : String = v match {
Chengsong
parents:
diff changeset
   121
  case Empty => ""
Chengsong
parents:
diff changeset
   122
  case Chr(c) => c.toString
Chengsong
parents:
diff changeset
   123
  case Left(v) => flatten(v)
Chengsong
parents:
diff changeset
   124
  case Right(v) => flatten(v)
Chengsong
parents:
diff changeset
   125
  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
Chengsong
parents:
diff changeset
   126
  case Stars(vs) => vs.map(flatten).mkString
Chengsong
parents:
diff changeset
   127
  case Ntime(vs) => vs.map(flatten).mkString
Chengsong
parents:
diff changeset
   128
  case Optionall(v) => flatten(v)
Chengsong
parents:
diff changeset
   129
  case Rec(_, v) => flatten(v)
Chengsong
parents:
diff changeset
   130
}
Chengsong
parents:
diff changeset
   131
Chengsong
parents:
diff changeset
   132
Chengsong
parents:
diff changeset
   133
// extracts an environment from a value;
Chengsong
parents:
diff changeset
   134
// used for tokenising a string
Chengsong
parents:
diff changeset
   135
def env(v: Val) : List[(String, String)] = v match {
Chengsong
parents:
diff changeset
   136
  case Empty => Nil
Chengsong
parents:
diff changeset
   137
  case Chr(c) => Nil
Chengsong
parents:
diff changeset
   138
  case Left(v) => env(v)
Chengsong
parents:
diff changeset
   139
  case Right(v) => env(v)
Chengsong
parents:
diff changeset
   140
  case Sequ(v1, v2) => env(v1) ::: env(v2)
Chengsong
parents:
diff changeset
   141
  case Stars(vs) => vs.flatMap(env)
Chengsong
parents:
diff changeset
   142
  case Ntime(vs) => vs.flatMap(env)
Chengsong
parents:
diff changeset
   143
  case Rec(x, v) => (x, flatten(v))::env(v)
Chengsong
parents:
diff changeset
   144
  case Optionall(v) => env(v)
Chengsong
parents:
diff changeset
   145
  case Nots(s) => ("Negative", s) :: Nil
Chengsong
parents:
diff changeset
   146
}
Chengsong
parents:
diff changeset
   147
Chengsong
parents:
diff changeset
   148
Chengsong
parents:
diff changeset
   149
// The injection and mkeps part of the lexer
Chengsong
parents:
diff changeset
   150
//===========================================
Chengsong
parents:
diff changeset
   151
Chengsong
parents:
diff changeset
   152
def mkeps(r: Rexp) : Val = r match {
Chengsong
parents:
diff changeset
   153
  case ONE => Empty
Chengsong
parents:
diff changeset
   154
  case ALTS(r1, r2) => 
Chengsong
parents:
diff changeset
   155
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
Chengsong
parents:
diff changeset
   156
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
Chengsong
parents:
diff changeset
   157
  case STAR(r) => Stars(Nil)
Chengsong
parents:
diff changeset
   158
  case RECD(x, r) => Rec(x, mkeps(r))
Chengsong
parents:
diff changeset
   159
  case NTIMES(n, r) => Ntime(List.fill(n)(mkeps(r)))
Chengsong
parents:
diff changeset
   160
  case OPTIONAL(r) => Optionall(Empty)
Chengsong
parents:
diff changeset
   161
  case NOT(rInner) => if(nullable(rInner)) throw new Exception("error")  
Chengsong
parents:
diff changeset
   162
                         else Nots("")//Nots(s.reverse.toString)
Chengsong
parents:
diff changeset
   163
//   case NOT(ZERO) => Empty
Chengsong
parents:
diff changeset
   164
//   case NOT(CHAR(c)) => Empty
Chengsong
parents:
diff changeset
   165
//   case NOT(SEQ(r1, r2)) => Sequ(mkeps(NOT(r1)), mkeps(NOT(r2)))
Chengsong
parents:
diff changeset
   166
//   case NOT(ALTS(r1, r2)) => if(!nullable(r1)) Left(mkeps(NOT(r1))) else Right(mkeps(NOT(r2)))
Chengsong
parents:
diff changeset
   167
//   case NOT(STAR(r)) => Stars(Nil) 
Chengsong
parents:
diff changeset
   168
Chengsong
parents:
diff changeset
   169
}
Chengsong
parents:
diff changeset
   170
Chengsong
parents:
diff changeset
   171
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
Chengsong
parents:
diff changeset
   172
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
Chengsong
parents:
diff changeset
   173
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
Chengsong
parents:
diff changeset
   174
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
Chengsong
parents:
diff changeset
   175
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
Chengsong
parents:
diff changeset
   176
  case (ALTS(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
Chengsong
parents:
diff changeset
   177
  case (ALTS(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
Chengsong
parents:
diff changeset
   178
  case (CHAR(d), Empty) => Chr(c) 
Chengsong
parents:
diff changeset
   179
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
Chengsong
parents:
diff changeset
   180
  case (NTIMES(n, r), Sequ(v1, Ntime(vs))) => Ntime(inj(r, c, v1)::vs)
Chengsong
parents:
diff changeset
   181
  case (OPTIONAL(r), v) => Optionall(inj(r, c, v))
Chengsong
parents:
diff changeset
   182
  case (NOT(r), Nots(s)) => Nots(c.toString ++ s)
Chengsong
parents:
diff changeset
   183
  case (ANYCHAR, Empty) => Chr(c)
Chengsong
parents:
diff changeset
   184
}
Chengsong
parents:
diff changeset
   185
Chengsong
parents:
diff changeset
   186
// some "rectification" functions for simplification
Chengsong
parents:
diff changeset
   187
def F_ID(v: Val): Val = v
Chengsong
parents:
diff changeset
   188
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
Chengsong
parents:
diff changeset
   189
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
Chengsong
parents:
diff changeset
   190
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Chengsong
parents:
diff changeset
   191
  case Right(v) => Right(f2(v))
Chengsong
parents:
diff changeset
   192
  case Left(v) => Left(f1(v))
Chengsong
parents:
diff changeset
   193
}
Chengsong
parents:
diff changeset
   194
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
Chengsong
parents:
diff changeset
   195
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
Chengsong
parents:
diff changeset
   196
}
Chengsong
parents:
diff changeset
   197
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
Chengsong
parents:
diff changeset
   198
  (v:Val) => Sequ(f1(Empty), f2(v))
Chengsong
parents:
diff changeset
   199
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
Chengsong
parents:
diff changeset
   200
  (v:Val) => Sequ(f1(v), f2(Empty))
Chengsong
parents:
diff changeset
   201
Chengsong
parents:
diff changeset
   202
def F_ERROR(v: Val): Val = throw new Exception("error")
Chengsong
parents:
diff changeset
   203
Chengsong
parents:
diff changeset
   204
// simplification
Chengsong
parents:
diff changeset
   205
def simp(r: Rexp): (Rexp, Val => Val) = r match {
Chengsong
parents:
diff changeset
   206
  case ALTS(r1, r2) => {
Chengsong
parents:
diff changeset
   207
    val (r1s, f1s) = simp(r1)
Chengsong
parents:
diff changeset
   208
    val (r2s, f2s) = simp(r2)
Chengsong
parents:
diff changeset
   209
    (r1s, r2s) match {
Chengsong
parents:
diff changeset
   210
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
Chengsong
parents:
diff changeset
   211
      case (_, ZERO) => (r1s, F_LEFT(f1s))
Chengsong
parents:
diff changeset
   212
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
Chengsong
parents:
diff changeset
   213
                else (ALTS (r1s, r2s), F_ALT(f1s, f2s)) 
Chengsong
parents:
diff changeset
   214
    }
Chengsong
parents:
diff changeset
   215
  }
Chengsong
parents:
diff changeset
   216
  case SEQ(r1, r2) => {
Chengsong
parents:
diff changeset
   217
    val (r1s, f1s) = simp(r1)
Chengsong
parents:
diff changeset
   218
    val (r2s, f2s) = simp(r2)
Chengsong
parents:
diff changeset
   219
    (r1s, r2s) match {
Chengsong
parents:
diff changeset
   220
      case (ZERO, _) => (ZERO, F_ERROR)
Chengsong
parents:
diff changeset
   221
      case (_, ZERO) => (ZERO, F_ERROR)
Chengsong
parents:
diff changeset
   222
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
Chengsong
parents:
diff changeset
   223
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
Chengsong
parents:
diff changeset
   224
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
Chengsong
parents:
diff changeset
   225
    }
Chengsong
parents:
diff changeset
   226
  }
Chengsong
parents:
diff changeset
   227
  case r => (r, F_ID)
Chengsong
parents:
diff changeset
   228
}
Chengsong
parents:
diff changeset
   229
Chengsong
parents:
diff changeset
   230
// lexing functions including simplification
Chengsong
parents:
diff changeset
   231
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
Chengsong
parents:
diff changeset
   232
  case Nil => if (nullable(r)) mkeps(r) else 
Chengsong
parents:
diff changeset
   233
    { throw new Exception(s"lexing error $r not nullable") } 
Chengsong
parents:
diff changeset
   234
  case c::cs => {
Chengsong
parents:
diff changeset
   235
    val (r_simp, f_simp) = simp(der(c, r))
Chengsong
parents:
diff changeset
   236
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
Chengsong
parents:
diff changeset
   237
  }
Chengsong
parents:
diff changeset
   238
}
Chengsong
parents:
diff changeset
   239
Chengsong
parents:
diff changeset
   240
def lexing_simp(r: Rexp, s: String) = 
Chengsong
parents:
diff changeset
   241
  env(lex_simp(r, s.toList))
Chengsong
parents:
diff changeset
   242
Chengsong
parents:
diff changeset
   243
// The Lexing Rules for the WHILE Language
Chengsong
parents:
diff changeset
   244
Chengsong
parents:
diff changeset
   245
def PLUS(r: Rexp) = r ~ r.%
Chengsong
parents:
diff changeset
   246
Chengsong
parents:
diff changeset
   247
def Range(s : List[Char]) : Rexp = s match {
Chengsong
parents:
diff changeset
   248
  case Nil => ZERO
Chengsong
parents:
diff changeset
   249
  case c::Nil => CHAR(c)
Chengsong
parents:
diff changeset
   250
  case c::s => ALTS(CHAR(c), Range(s))
Chengsong
parents:
diff changeset
   251
}
Chengsong
parents:
diff changeset
   252
def RANGE(s: String) = Range(s.toList)
Chengsong
parents:
diff changeset
   253
Chengsong
parents:
diff changeset
   254
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
Chengsong
parents:
diff changeset
   255
val DIGIT = RANGE("0123456789")
Chengsong
parents:
diff changeset
   256
val ID = SYM ~ (SYM | DIGIT).% 
Chengsong
parents:
diff changeset
   257
val NUM = PLUS(DIGIT)
Chengsong
parents:
diff changeset
   258
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
Chengsong
parents:
diff changeset
   259
val SEMI: Rexp = ";"
Chengsong
parents:
diff changeset
   260
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
Chengsong
parents:
diff changeset
   261
val WHITESPACE = PLUS(" " | "\n" | "\t" | "\r")
Chengsong
parents:
diff changeset
   262
val RPAREN: Rexp = "{"
Chengsong
parents:
diff changeset
   263
val LPAREN: Rexp = "}"
Chengsong
parents:
diff changeset
   264
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
Chengsong
parents:
diff changeset
   265
Chengsong
parents:
diff changeset
   266
Chengsong
parents:
diff changeset
   267
//ab \ a --> 1b
Chengsong
parents:
diff changeset
   268
//
Chengsong
parents:
diff changeset
   269
val WHILE_REGS = (("k" $ KEYWORD) | 
Chengsong
parents:
diff changeset
   270
                  ("i" $ ID) | 
Chengsong
parents:
diff changeset
   271
                  ("o" $ OP) | 
Chengsong
parents:
diff changeset
   272
                  ("n" $ NUM) | 
Chengsong
parents:
diff changeset
   273
                  ("s" $ SEMI) | 
Chengsong
parents:
diff changeset
   274
                  ("str" $ STRING) |
Chengsong
parents:
diff changeset
   275
                  ("p" $ (LPAREN | RPAREN)) | 
Chengsong
parents:
diff changeset
   276
                  ("w" $ WHITESPACE)).%
Chengsong
parents:
diff changeset
   277
Chengsong
parents:
diff changeset
   278
val NREGS = NTIMES(5, OPTIONAL(SYM))
Chengsong
parents:
diff changeset
   279
val NREGS1 = ("test" $ NREGS)
Chengsong
parents:
diff changeset
   280
// Two Simple While Tests
Chengsong
parents:
diff changeset
   281
//========================
Chengsong
parents:
diff changeset
   282
val NOTREG = "hehe" ~ NOT((ANYCHAR.%) ~ "haha" ~ (ANYCHAR.%))
Chengsong
parents:
diff changeset
   283
Chengsong
parents:
diff changeset
   284
Chengsong
parents:
diff changeset
   285
  // bnullable function: tests whether the aregular 
Chengsong
parents:
diff changeset
   286
  // expression can recognise the empty string
Chengsong
parents:
diff changeset
   287
def bnullable (r: ARexp) : Boolean = r match {
Chengsong
parents:
diff changeset
   288
    case AZERO => false
Chengsong
parents:
diff changeset
   289
    case AONE(_) => true
Chengsong
parents:
diff changeset
   290
    case ACHAR(_,_) => false
Chengsong
parents:
diff changeset
   291
    case AALTS(_, rs) => rs.exists(bnullable)
Chengsong
parents:
diff changeset
   292
    case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
Chengsong
parents:
diff changeset
   293
    case ASTAR(_, _) => true
Chengsong
parents:
diff changeset
   294
    case ANOT(_, rn) => !bnullable(rn)
Chengsong
parents:
diff changeset
   295
  }
Chengsong
parents:
diff changeset
   296
Chengsong
parents:
diff changeset
   297
def mkepsBC(r: ARexp) : Bits = r match {
Chengsong
parents:
diff changeset
   298
    case AONE(bs) => bs
Chengsong
parents:
diff changeset
   299
    case AALTS(bs, rs) => {
Chengsong
parents:
diff changeset
   300
      val n = rs.indexWhere(bnullable)
Chengsong
parents:
diff changeset
   301
      bs ++ mkepsBC(rs(n))
Chengsong
parents:
diff changeset
   302
    }
Chengsong
parents:
diff changeset
   303
    case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
Chengsong
parents:
diff changeset
   304
    case ASTAR(bs, r) => bs ++ List(Z)
Chengsong
parents:
diff changeset
   305
    case ANOT(bs, rn) => bs
Chengsong
parents:
diff changeset
   306
  }
Chengsong
parents:
diff changeset
   307
Chengsong
parents:
diff changeset
   308
Chengsong
parents:
diff changeset
   309
def bder(c: Char, r: ARexp) : ARexp = r match {
Chengsong
parents:
diff changeset
   310
    case AZERO => AZERO
Chengsong
parents:
diff changeset
   311
    case AONE(_) => AZERO
Chengsong
parents:
diff changeset
   312
    case ACHAR(bs, f) => if (c == f) AONE(bs) else AZERO
Chengsong
parents:
diff changeset
   313
    case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
Chengsong
parents:
diff changeset
   314
    case ASEQ(bs, r1, r2) => 
Chengsong
parents:
diff changeset
   315
      if (bnullable(r1)) AALTS(bs, ASEQ(Nil, bder(c, r1), r2) :: fuse(mkepsBC(r1), bder(c, r2)) :: Nil )
Chengsong
parents:
diff changeset
   316
      else ASEQ(bs, bder(c, r1), r2)
Chengsong
parents:
diff changeset
   317
    case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
Chengsong
parents:
diff changeset
   318
    case ANOT(bs, rn) => ANOT(bs, bder(c, rn))
Chengsong
parents:
diff changeset
   319
    case AANYCHAR(bs) => AONE(bs)
Chengsong
parents:
diff changeset
   320
  } 
Chengsong
parents:
diff changeset
   321
Chengsong
parents:
diff changeset
   322
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
Chengsong
parents:
diff changeset
   323
    case AZERO => AZERO
Chengsong
parents:
diff changeset
   324
    case AONE(cs) => AONE(bs ++ cs)
Chengsong
parents:
diff changeset
   325
    case ACHAR(cs, f) => ACHAR(bs ++ cs, f)
Chengsong
parents:
diff changeset
   326
    case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
Chengsong
parents:
diff changeset
   327
    case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
Chengsong
parents:
diff changeset
   328
    case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
Chengsong
parents:
diff changeset
   329
    case ANOT(cs, r) => ANOT(bs ++ cs, r)
Chengsong
parents:
diff changeset
   330
  }
Chengsong
parents:
diff changeset
   331
Chengsong
parents:
diff changeset
   332
Chengsong
parents:
diff changeset
   333
def internalise(r: Rexp) : ARexp = r match {
Chengsong
parents:
diff changeset
   334
    case ZERO => AZERO
Chengsong
parents:
diff changeset
   335
    case ONE => AONE(Nil)
Chengsong
parents:
diff changeset
   336
    case CHAR(c) => ACHAR(Nil, c)
Chengsong
parents:
diff changeset
   337
    //case PRED(f) => APRED(Nil, f)
Chengsong
parents:
diff changeset
   338
    case ALTS(r1, r2) => 
Chengsong
parents:
diff changeset
   339
      AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
Chengsong
parents:
diff changeset
   340
    // case ALTS(r1::rs) => {
Chengsong
parents:
diff changeset
   341
    //   val AALTS(Nil, rs2) = internalise(ALTS(rs))
Chengsong
parents:
diff changeset
   342
    //   AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
Chengsong
parents:
diff changeset
   343
    // }
Chengsong
parents:
diff changeset
   344
    case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
Chengsong
parents:
diff changeset
   345
    case STAR(r) => ASTAR(Nil, internalise(r))
Chengsong
parents:
diff changeset
   346
    case RECD(x, r) => internalise(r)
Chengsong
parents:
diff changeset
   347
    case NOT(r) => ANOT(Nil, internalise(r))
Chengsong
parents:
diff changeset
   348
    case ANYCHAR => AANYCHAR(Nil)
Chengsong
parents:
diff changeset
   349
  }
Chengsong
parents:
diff changeset
   350
Chengsong
parents:
diff changeset
   351
Chengsong
parents:
diff changeset
   352
def bsimp(r: ARexp): ARexp = 
Chengsong
parents:
diff changeset
   353
  {
Chengsong
parents:
diff changeset
   354
    r match {
Chengsong
parents:
diff changeset
   355
      case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
Chengsong
parents:
diff changeset
   356
          case (AZERO, _) => AZERO
Chengsong
parents:
diff changeset
   357
          case (_, AZERO) => AZERO
Chengsong
parents:
diff changeset
   358
          case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
Chengsong
parents:
diff changeset
   359
          case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
Chengsong
parents:
diff changeset
   360
      }
Chengsong
parents:
diff changeset
   361
      case AALTS(bs1, rs) => {
Chengsong
parents:
diff changeset
   362
            val rs_simp = rs.map(bsimp(_))
Chengsong
parents:
diff changeset
   363
            val flat_res = flats(rs_simp)
Chengsong
parents:
diff changeset
   364
            val dist_res = distinctBy(flat_res, erase)//strongDB(flat_res)//distinctBy(flat_res, erase)
Chengsong
parents:
diff changeset
   365
            dist_res match {
Chengsong
parents:
diff changeset
   366
              case Nil => AZERO
Chengsong
parents:
diff changeset
   367
              case s :: Nil => fuse(bs1, s)
Chengsong
parents:
diff changeset
   368
              case rs => AALTS(bs1, rs)  
Chengsong
parents:
diff changeset
   369
            }
Chengsong
parents:
diff changeset
   370
          
Chengsong
parents:
diff changeset
   371
      }
Chengsong
parents:
diff changeset
   372
      case r => r
Chengsong
parents:
diff changeset
   373
    }
Chengsong
parents:
diff changeset
   374
  }
Chengsong
parents:
diff changeset
   375
  def strongBsimp(r: ARexp): ARexp =
Chengsong
parents:
diff changeset
   376
  {
Chengsong
parents:
diff changeset
   377
    r match {
Chengsong
parents:
diff changeset
   378
      case ASEQ(bs1, r1, r2) => (strongBsimp(r1), strongBsimp(r2)) match {
Chengsong
parents:
diff changeset
   379
          case (AZERO, _) => AZERO
Chengsong
parents:
diff changeset
   380
          case (_, AZERO) => AZERO
Chengsong
parents:
diff changeset
   381
          case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
Chengsong
parents:
diff changeset
   382
          case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
Chengsong
parents:
diff changeset
   383
      }
Chengsong
parents:
diff changeset
   384
      case AALTS(bs1, rs) => {
Chengsong
parents:
diff changeset
   385
            val rs_simp = rs.map(strongBsimp(_))
Chengsong
parents:
diff changeset
   386
            val flat_res = flats(rs_simp)
Chengsong
parents:
diff changeset
   387
            val dist_res = distinctBy4(flat_res)//distinctBy(flat_res, erase)
Chengsong
parents:
diff changeset
   388
            dist_res match {
Chengsong
parents:
diff changeset
   389
              case Nil => AZERO
Chengsong
parents:
diff changeset
   390
              case s :: Nil => fuse(bs1, s)
Chengsong
parents:
diff changeset
   391
              case rs => AALTS(bs1, rs)  
Chengsong
parents:
diff changeset
   392
            }
Chengsong
parents:
diff changeset
   393
          
Chengsong
parents:
diff changeset
   394
      }
Chengsong
parents:
diff changeset
   395
      case r => r
Chengsong
parents:
diff changeset
   396
    }
Chengsong
parents:
diff changeset
   397
  }
Chengsong
parents:
diff changeset
   398
Chengsong
parents:
diff changeset
   399
  def bders (s: List[Char], r: ARexp) : ARexp = s match {
Chengsong
parents:
diff changeset
   400
    case Nil => r
Chengsong
parents:
diff changeset
   401
    case c::s => bders(s, bder(c, r))
Chengsong
parents:
diff changeset
   402
  }
Chengsong
parents:
diff changeset
   403
Chengsong
parents:
diff changeset
   404
  def flats(rs: List[ARexp]): List[ARexp] = rs match {
Chengsong
parents:
diff changeset
   405
      case Nil => Nil
Chengsong
parents:
diff changeset
   406
      case AZERO :: rs1 => flats(rs1)
Chengsong
parents:
diff changeset
   407
      case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
Chengsong
parents:
diff changeset
   408
      case r1 :: rs2 => r1 :: flats(rs2)
Chengsong
parents:
diff changeset
   409
    }
Chengsong
parents:
diff changeset
   410
Chengsong
parents:
diff changeset
   411
  def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
Chengsong
parents:
diff changeset
   412
    case Nil => Nil
Chengsong
parents:
diff changeset
   413
    case (x::xs) => {
Chengsong
parents:
diff changeset
   414
      val res = f(x)
Chengsong
parents:
diff changeset
   415
      if (acc.contains(res)) distinctBy(xs, f, acc)  
Chengsong
parents:
diff changeset
   416
      else x::distinctBy(xs, f, res::acc)
Chengsong
parents:
diff changeset
   417
    }
Chengsong
parents:
diff changeset
   418
  } 
Chengsong
parents:
diff changeset
   419
Chengsong
parents:
diff changeset
   420
  def projectFirstChild(rs: List[ARexp]) : List[ARexp] = rs match {
Chengsong
parents:
diff changeset
   421
    case (ASEQ(bs, r1, r2p)::rs1) => r1::projectFirstChild(rs1)
Chengsong
parents:
diff changeset
   422
    case Nil => Nil
Chengsong
parents:
diff changeset
   423
  }
Chengsong
parents:
diff changeset
   424
Chengsong
parents:
diff changeset
   425
Chengsong
parents:
diff changeset
   426
  def distinctBy2(xs: List[ARexp], acc: List[Rexp] = Nil): List[ARexp] = xs match {
Chengsong
parents:
diff changeset
   427
    case Nil => Nil
Chengsong
parents:
diff changeset
   428
    case (x::xs) => {
Chengsong
parents:
diff changeset
   429
      val res = erase(x)
Chengsong
parents:
diff changeset
   430
      if(acc.contains(res))
Chengsong
parents:
diff changeset
   431
        distinctBy2(xs, acc)
Chengsong
parents:
diff changeset
   432
      else
Chengsong
parents:
diff changeset
   433
        x match {
Chengsong
parents:
diff changeset
   434
          case ASEQ(bs0, AALTS(bs1, rs), r2) => 
Chengsong
parents:
diff changeset
   435
            val newTerms =  distinctBy2(rs.map(r1 => ASEQ(Nil, r1, r2)), acc)
Chengsong
parents:
diff changeset
   436
            val rsPrime = projectFirstChild(newTerms)
Chengsong
parents:
diff changeset
   437
            newTerms match {
Chengsong
parents:
diff changeset
   438
              case Nil => distinctBy2(xs, acc)
Chengsong
parents:
diff changeset
   439
              case t::Nil => ASEQ(bs0, fuse(bs1, rsPrime.head), r2)::distinctBy2(xs, erase(t)::acc)
Chengsong
parents:
diff changeset
   440
              case ts => ASEQ(bs0, AALTS(bs1, rsPrime), r2)::distinctBy2(xs, newTerms.map(erase(_)):::acc)
Chengsong
parents:
diff changeset
   441
            }
Chengsong
parents:
diff changeset
   442
          case x => x::distinctBy2(xs, res::acc)
Chengsong
parents:
diff changeset
   443
        }
Chengsong
parents:
diff changeset
   444
    }
Chengsong
parents:
diff changeset
   445
  }
Chengsong
parents:
diff changeset
   446
Chengsong
parents:
diff changeset
   447
  def deepFlts(r: ARexp): List[ARexp] = r match{
Chengsong
parents:
diff changeset
   448
Chengsong
parents:
diff changeset
   449
    case ASEQ(bs, r1, r2) => deepFlts(r1).map(r1p => ASEQ(bs, r1p, r2))
Chengsong
parents:
diff changeset
   450
    case ASTAR(bs, r0) => List(r)
Chengsong
parents:
diff changeset
   451
    case ACHAR(_, _) => List(r)
Chengsong
parents:
diff changeset
   452
    case AALTS(bs, rs) => rs.flatMap(deepFlts(_))//throw new Error("doubly nested alts in bsimp r")
Chengsong
parents:
diff changeset
   453
  }
Chengsong
parents:
diff changeset
   454
Chengsong
parents:
diff changeset
   455
Chengsong
parents:
diff changeset
   456
  def distinctBy3(xs: List[ARexp], acc: List[Rexp] = Nil): List[ARexp] = xs match {
Chengsong
parents:
diff changeset
   457
    case Nil => Nil
Chengsong
parents:
diff changeset
   458
    case (x::xs) => {
Chengsong
parents:
diff changeset
   459
      val res = erase(x)
Chengsong
parents:
diff changeset
   460
      if(acc.contains(res))
Chengsong
parents:
diff changeset
   461
        distinctBy3(xs, acc)
Chengsong
parents:
diff changeset
   462
      else
Chengsong
parents:
diff changeset
   463
        x match {
Chengsong
parents:
diff changeset
   464
          case ASEQ(bs0, AALTS(bs1, rs), r2) => 
Chengsong
parents:
diff changeset
   465
            val newTerms =  distinctBy3(rs.flatMap(deepFlts(_)).map(r1 => ASEQ(Nil, r1, r2)), acc)//deepFlts(rs.flatMap(r1 => ASEQ(Nil, r1, r2)), acc)
Chengsong
parents:
diff changeset
   466
            val rsPrime = projectFirstChild(newTerms)
Chengsong
parents:
diff changeset
   467
            newTerms match {
Chengsong
parents:
diff changeset
   468
              case Nil => distinctBy3(xs, acc)
Chengsong
parents:
diff changeset
   469
              case t::Nil => ASEQ(bs0, fuse(bs1, rsPrime.head), r2)::distinctBy3(xs, erase(t)::acc)
Chengsong
parents:
diff changeset
   470
              case ts => ASEQ(bs0, AALTS(bs1, rsPrime), r2)::distinctBy3(xs, newTerms.map(erase(_)):::acc)
Chengsong
parents:
diff changeset
   471
            }
Chengsong
parents:
diff changeset
   472
          case x => x::distinctBy3(xs, res::acc)
Chengsong
parents:
diff changeset
   473
        }
Chengsong
parents:
diff changeset
   474
    }
Chengsong
parents:
diff changeset
   475
  }
Chengsong
parents:
diff changeset
   476
Chengsong
parents:
diff changeset
   477
  def pruneRexp(r: ARexp, allowableTerms: List[Rexp]) : ARexp = {
Chengsong
parents:
diff changeset
   478
    r match {
Chengsong
parents:
diff changeset
   479
      case ASEQ(bs, r1, r2) => 
Chengsong
parents:
diff changeset
   480
        val termsTruncated = allowableTerms.collect(rt => rt match {
432
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   481
          case SEQ(r1p, r2p) if(r2p == erase(r2)) => r1p//if(r2p == erase(r2)) 
431
Chengsong
parents:
diff changeset
   482
        })
Chengsong
parents:
diff changeset
   483
        val pruned : ARexp = pruneRexp(r1, termsTruncated)
Chengsong
parents:
diff changeset
   484
        pruned match {
Chengsong
parents:
diff changeset
   485
          case AZERO => AZERO
Chengsong
parents:
diff changeset
   486
          case AONE(bs1) => fuse(bs ++ bs1, r2)
Chengsong
parents:
diff changeset
   487
          case pruned1 => ASEQ(bs, pruned1, r2)
Chengsong
parents:
diff changeset
   488
        }
Chengsong
parents:
diff changeset
   489
Chengsong
parents:
diff changeset
   490
      case AALTS(bs, rs) => 
Chengsong
parents:
diff changeset
   491
        //allowableTerms.foreach(a => println(shortRexpOutput(a)))        
432
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   492
        val rsp = rs.map(r => 
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   493
                      pruneRexp(r, allowableTerms)
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   494
                    )
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   495
                    .filter(r => r != AZERO)
431
Chengsong
parents:
diff changeset
   496
        rsp match {
Chengsong
parents:
diff changeset
   497
          case Nil => AZERO
Chengsong
parents:
diff changeset
   498
          case r1::Nil => fuse(bs, r1)
Chengsong
parents:
diff changeset
   499
          case rs1 => AALTS(bs, rs1)
Chengsong
parents:
diff changeset
   500
        }
Chengsong
parents:
diff changeset
   501
      case r => 
Chengsong
parents:
diff changeset
   502
        if(allowableTerms.contains(erase(r))) r else AZERO //assert(r != AZERO)
Chengsong
parents:
diff changeset
   503
    }
Chengsong
parents:
diff changeset
   504
  }
Chengsong
parents:
diff changeset
   505
Chengsong
parents:
diff changeset
   506
  def oneSimp(r: Rexp) : Rexp = r match {
Chengsong
parents:
diff changeset
   507
    case SEQ(ONE, r) => r
Chengsong
parents:
diff changeset
   508
    case SEQ(r1, r2) => SEQ(oneSimp(r1), r2)
Chengsong
parents:
diff changeset
   509
    case r => r//assert r != 0 
Chengsong
parents:
diff changeset
   510
     
Chengsong
parents:
diff changeset
   511
  }
Chengsong
parents:
diff changeset
   512
Chengsong
parents:
diff changeset
   513
Chengsong
parents:
diff changeset
   514
  def distinctBy4(xs: List[ARexp], acc: List[Rexp] = Nil) : List[ARexp] = xs match {
432
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   515
    
431
Chengsong
parents:
diff changeset
   516
    case Nil => Nil
Chengsong
parents:
diff changeset
   517
    case x :: xs =>
432
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   518
      //assert(acc.distinct == acc)
431
Chengsong
parents:
diff changeset
   519
      val erased = erase(x)
Chengsong
parents:
diff changeset
   520
      if(acc.contains(erased))
Chengsong
parents:
diff changeset
   521
        distinctBy4(xs, acc)
Chengsong
parents:
diff changeset
   522
      else{
Chengsong
parents:
diff changeset
   523
        val addToAcc =  breakIntoTerms(erased).filter(r => !acc.contains(oneSimp(r)))
432
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   524
        //val xp = pruneRexp(x, addToAcc)
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   525
        pruneRexp(x, addToAcc) match {
431
Chengsong
parents:
diff changeset
   526
          case AZERO => distinctBy4(xs, addToAcc.map(oneSimp(_)) ::: acc)
Chengsong
parents:
diff changeset
   527
          case xPrime => xPrime :: distinctBy4(xs, addToAcc.map(oneSimp(_)) ::: acc)
Chengsong
parents:
diff changeset
   528
        }
Chengsong
parents:
diff changeset
   529
        
Chengsong
parents:
diff changeset
   530
      }
Chengsong
parents:
diff changeset
   531
  }
Chengsong
parents:
diff changeset
   532
Chengsong
parents:
diff changeset
   533
Chengsong
parents:
diff changeset
   534
  def breakIntoTerms(r: Rexp) : List[Rexp] = r match {
Chengsong
parents:
diff changeset
   535
    case SEQ(r1, r2)  => breakIntoTerms(r1).map(r11 => SEQ(r11, r2))
Chengsong
parents:
diff changeset
   536
      // breakIntoTerms(r1).map(r11 => r11 match {
Chengsong
parents:
diff changeset
   537
      //   case ONE => r2
Chengsong
parents:
diff changeset
   538
      //   case r => SEQ(r11, r2)
Chengsong
parents:
diff changeset
   539
      // }
Chengsong
parents:
diff changeset
   540
      //)
Chengsong
parents:
diff changeset
   541
    case ALTS(r1, r2) => breakIntoTerms(r1) ::: breakIntoTerms(r2)
Chengsong
parents:
diff changeset
   542
    case _ => r::Nil
Chengsong
parents:
diff changeset
   543
  } 
Chengsong
parents:
diff changeset
   544
Chengsong
parents:
diff changeset
   545
  def prettyRexp(r: Rexp) : String = r match {
Chengsong
parents:
diff changeset
   546
      case STAR(r0) => s"${prettyRexp(r0)}*"
Chengsong
parents:
diff changeset
   547
      case SEQ(CHAR(c), r2) => c.toString ++ prettyRexp(r2)
Chengsong
parents:
diff changeset
   548
      case SEQ(r1, r2) => s"${prettyRexp(r1)}~${prettyRexp(r2)}"
Chengsong
parents:
diff changeset
   549
      case CHAR(c) => c.toString
Chengsong
parents:
diff changeset
   550
      case ANYCHAR => "."
Chengsong
parents:
diff changeset
   551
    //   case NOT(r0) => s
Chengsong
parents:
diff changeset
   552
  }
Chengsong
parents:
diff changeset
   553
Chengsong
parents:
diff changeset
   554
  def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
Chengsong
parents:
diff changeset
   555
    case (ONE, bs) => (Empty, bs)
Chengsong
parents:
diff changeset
   556
    case (CHAR(f), bs) => (Chr(f), bs)
Chengsong
parents:
diff changeset
   557
    case (ALTS(r1, r2), Z::bs1) => {
Chengsong
parents:
diff changeset
   558
        val (v, bs2) = decode_aux(r1, bs1)
Chengsong
parents:
diff changeset
   559
        (Left(v), bs2)
Chengsong
parents:
diff changeset
   560
    }
Chengsong
parents:
diff changeset
   561
    case (ALTS(r1, r2), S::bs1) => {
Chengsong
parents:
diff changeset
   562
        val (v, bs2) = decode_aux(r2, bs1)
Chengsong
parents:
diff changeset
   563
        (Right(v), bs2)
Chengsong
parents:
diff changeset
   564
    }
Chengsong
parents:
diff changeset
   565
    case (SEQ(r1, r2), bs) => {
Chengsong
parents:
diff changeset
   566
      val (v1, bs1) = decode_aux(r1, bs)
Chengsong
parents:
diff changeset
   567
      val (v2, bs2) = decode_aux(r2, bs1)
Chengsong
parents:
diff changeset
   568
      (Sequ(v1, v2), bs2)
Chengsong
parents:
diff changeset
   569
    }
Chengsong
parents:
diff changeset
   570
    case (STAR(r1), S::bs) => {
Chengsong
parents:
diff changeset
   571
      val (v, bs1) = decode_aux(r1, bs)
Chengsong
parents:
diff changeset
   572
      //println(v)
Chengsong
parents:
diff changeset
   573
      val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
Chengsong
parents:
diff changeset
   574
      (Stars(v::vs), bs2)
Chengsong
parents:
diff changeset
   575
    }
Chengsong
parents:
diff changeset
   576
    case (STAR(_), Z::bs) => (Stars(Nil), bs)
Chengsong
parents:
diff changeset
   577
    case (RECD(x, r1), bs) => {
Chengsong
parents:
diff changeset
   578
      val (v, bs1) = decode_aux(r1, bs)
Chengsong
parents:
diff changeset
   579
      (Rec(x, v), bs1)
Chengsong
parents:
diff changeset
   580
    }
Chengsong
parents:
diff changeset
   581
    case (NOT(r), bs) => (Nots(prettyRexp(r)), bs)
Chengsong
parents:
diff changeset
   582
  }
Chengsong
parents:
diff changeset
   583
Chengsong
parents:
diff changeset
   584
  def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
Chengsong
parents:
diff changeset
   585
    case (v, Nil) => v
Chengsong
parents:
diff changeset
   586
    case _ => throw new Exception("Not decodable")
Chengsong
parents:
diff changeset
   587
  }
Chengsong
parents:
diff changeset
   588
Chengsong
parents:
diff changeset
   589
Chengsong
parents:
diff changeset
   590
Chengsong
parents:
diff changeset
   591
def blexSimp(r: Rexp, s: String) : List[Bit] = {
Chengsong
parents:
diff changeset
   592
    blex_simp(internalise(r), s.toList)
Chengsong
parents:
diff changeset
   593
}
Chengsong
parents:
diff changeset
   594
Chengsong
parents:
diff changeset
   595
def blexing_simp(r: Rexp, s: String) : Val = {
Chengsong
parents:
diff changeset
   596
    val bit_code = blex_simp(internalise(r), s.toList)
Chengsong
parents:
diff changeset
   597
    decode(r, bit_code)
Chengsong
parents:
diff changeset
   598
  }
Chengsong
parents:
diff changeset
   599
Chengsong
parents:
diff changeset
   600
  def strong_blexing_simp(r: Rexp, s: String) : Val = {
Chengsong
parents:
diff changeset
   601
    decode(r, strong_blex_simp(internalise(r), s.toList))
Chengsong
parents:
diff changeset
   602
  }
Chengsong
parents:
diff changeset
   603
Chengsong
parents:
diff changeset
   604
  def strong_blex_simp(r: ARexp, s: List[Char]) :Bits = s match {
Chengsong
parents:
diff changeset
   605
    case Nil => {
Chengsong
parents:
diff changeset
   606
      if (bnullable(r)) {
Chengsong
parents:
diff changeset
   607
        //println(asize(r))
Chengsong
parents:
diff changeset
   608
        val bits = mkepsBC(r)
Chengsong
parents:
diff changeset
   609
        bits
Chengsong
parents:
diff changeset
   610
      }
Chengsong
parents:
diff changeset
   611
      else 
Chengsong
parents:
diff changeset
   612
        throw new Exception("Not matched")
Chengsong
parents:
diff changeset
   613
    }
Chengsong
parents:
diff changeset
   614
    case c::cs => {
Chengsong
parents:
diff changeset
   615
      val der_res = bder(c,r)
Chengsong
parents:
diff changeset
   616
      val simp_res = strongBsimp(der_res)  
Chengsong
parents:
diff changeset
   617
      strong_blex_simp(simp_res, cs)      
Chengsong
parents:
diff changeset
   618
    }
Chengsong
parents:
diff changeset
   619
  }
Chengsong
parents:
diff changeset
   620
Chengsong
parents:
diff changeset
   621
Chengsong
parents:
diff changeset
   622
Chengsong
parents:
diff changeset
   623
  def bders_simp(s: List[Char], r: ARexp) : ARexp = s match {
Chengsong
parents:
diff changeset
   624
    case Nil => r
435
Chengsong
parents: 432
diff changeset
   625
    case c::s => 
Chengsong
parents: 432
diff changeset
   626
      println(erase(r))
Chengsong
parents: 432
diff changeset
   627
      bders_simp(s, bsimp(bder(c, r)))
431
Chengsong
parents:
diff changeset
   628
  }
Chengsong
parents:
diff changeset
   629
  
Chengsong
parents:
diff changeset
   630
  def bdersSimp(s: String, r: Rexp) : ARexp = bders_simp(s.toList, internalise(r))
Chengsong
parents:
diff changeset
   631
Chengsong
parents:
diff changeset
   632
  def bders_simpS(s: List[Char], r: ARexp) : ARexp = s match {
Chengsong
parents:
diff changeset
   633
    case Nil => r 
Chengsong
parents:
diff changeset
   634
    case c::s => bders_simpS(s, strongBsimp(bder(c, r)))
Chengsong
parents:
diff changeset
   635
  }
Chengsong
parents:
diff changeset
   636
Chengsong
parents:
diff changeset
   637
  def bdersSimpS(s: String, r: Rexp) : ARexp = bders_simpS(s.toList, internalise(r))
Chengsong
parents:
diff changeset
   638
Chengsong
parents:
diff changeset
   639
  def erase(r:ARexp): Rexp = r match{
Chengsong
parents:
diff changeset
   640
    case AZERO => ZERO
Chengsong
parents:
diff changeset
   641
    case AONE(_) => ONE
Chengsong
parents:
diff changeset
   642
    case ACHAR(bs, c) => CHAR(c)
Chengsong
parents:
diff changeset
   643
    case AALTS(bs, Nil) => ZERO
Chengsong
parents:
diff changeset
   644
    case AALTS(bs, a::Nil) => erase(a)
Chengsong
parents:
diff changeset
   645
    case AALTS(bs, a::as) => ALTS(erase(a), erase(AALTS(bs, as)))
Chengsong
parents:
diff changeset
   646
    case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
Chengsong
parents:
diff changeset
   647
    case ASTAR(cs, r)=> STAR(erase(r))
Chengsong
parents:
diff changeset
   648
    case ANOT(bs, r) => NOT(erase(r))
Chengsong
parents:
diff changeset
   649
    case AANYCHAR(bs) => ANYCHAR
Chengsong
parents:
diff changeset
   650
  }
Chengsong
parents:
diff changeset
   651
Chengsong
parents:
diff changeset
   652
Chengsong
parents:
diff changeset
   653
def allCharSeq(r: Rexp) : Boolean = r match {
Chengsong
parents:
diff changeset
   654
  case CHAR(c) => true
Chengsong
parents:
diff changeset
   655
  case SEQ(r1, r2) => allCharSeq(r1) && allCharSeq(r2)
Chengsong
parents:
diff changeset
   656
  case _ => false
Chengsong
parents:
diff changeset
   657
}
Chengsong
parents:
diff changeset
   658
Chengsong
parents:
diff changeset
   659
def flattenSeq(r: Rexp) : String = r match {
Chengsong
parents:
diff changeset
   660
  case CHAR(c) => c.toString
Chengsong
parents:
diff changeset
   661
  case SEQ(r1, r2) => flattenSeq(r1) ++ flattenSeq(r2)
Chengsong
parents:
diff changeset
   662
  case _ => throw new Error("flatten unflattenable rexp")
Chengsong
parents:
diff changeset
   663
} 
Chengsong
parents:
diff changeset
   664
Chengsong
parents:
diff changeset
   665
def shortRexpOutput(r: Rexp) : String = r match {
Chengsong
parents:
diff changeset
   666
    case CHAR(c) => c.toString
Chengsong
parents:
diff changeset
   667
    case ONE => "1"
Chengsong
parents:
diff changeset
   668
    case ZERO => "0"
Chengsong
parents:
diff changeset
   669
    case SEQ(r1, r2) if(allCharSeq(r)) => flattenSeq(r)//"[" ++ shortRexpOutput(r1) ++ "~" ++ shortRexpOutput(r2) ++ "]"
Chengsong
parents:
diff changeset
   670
    case SEQ(r1, r2) => "[" ++ shortRexpOutput(r1) ++ "~" ++ shortRexpOutput(r2) ++ "]"
Chengsong
parents:
diff changeset
   671
    case ALTS(r1, r2) => "(" ++ shortRexpOutput(r1) ++ "+" ++ shortRexpOutput(r2) ++ ")"
Chengsong
parents:
diff changeset
   672
    case STAR(STAR(r)) => "(..)*"// ++ shortRexpOutput(r) ++ "]*"
Chengsong
parents:
diff changeset
   673
    case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
Chengsong
parents:
diff changeset
   674
    //case RTOP => "RTOP"
Chengsong
parents:
diff changeset
   675
  }
Chengsong
parents:
diff changeset
   676
Chengsong
parents:
diff changeset
   677
Chengsong
parents:
diff changeset
   678
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
Chengsong
parents:
diff changeset
   679
    case Nil => {
Chengsong
parents:
diff changeset
   680
      if (bnullable(r)) {
Chengsong
parents:
diff changeset
   681
        //println(asize(r))
Chengsong
parents:
diff changeset
   682
        val bits = mkepsBC(r)
Chengsong
parents:
diff changeset
   683
        bits
Chengsong
parents:
diff changeset
   684
      }
Chengsong
parents:
diff changeset
   685
      else 
Chengsong
parents:
diff changeset
   686
        throw new Exception("Not matched")
Chengsong
parents:
diff changeset
   687
    }
Chengsong
parents:
diff changeset
   688
    case c::cs => {
Chengsong
parents:
diff changeset
   689
      val der_res = bder(c,r)
Chengsong
parents:
diff changeset
   690
      val simp_res = bsimp(der_res)  
Chengsong
parents:
diff changeset
   691
      blex_simp(simp_res, cs)      
Chengsong
parents:
diff changeset
   692
    }
Chengsong
parents:
diff changeset
   693
  }
Chengsong
parents:
diff changeset
   694
  def size(r: Rexp) : Int = r match {
Chengsong
parents:
diff changeset
   695
    case ZERO => 1
Chengsong
parents:
diff changeset
   696
    case ONE => 1
Chengsong
parents:
diff changeset
   697
    case CHAR(_) => 1
Chengsong
parents:
diff changeset
   698
    case ANYCHAR => 1
Chengsong
parents:
diff changeset
   699
    case NOT(r0) => 1 + size(r0)
Chengsong
parents:
diff changeset
   700
    case SEQ(r1, r2) => 1 + size(r1) + size(r2)
Chengsong
parents:
diff changeset
   701
    case ALTS(r1, r2) => 1 + List(r1, r2).map(size).sum
Chengsong
parents:
diff changeset
   702
    case STAR(r) => 1 + size(r)
Chengsong
parents:
diff changeset
   703
  }
Chengsong
parents:
diff changeset
   704
Chengsong
parents:
diff changeset
   705
  def asize(a: ARexp) = size(erase(a))
Chengsong
parents:
diff changeset
   706
Chengsong
parents:
diff changeset
   707
//pder related
Chengsong
parents:
diff changeset
   708
type Mon = (Char, Rexp)
Chengsong
parents:
diff changeset
   709
type Lin = Set[Mon]
Chengsong
parents:
diff changeset
   710
Chengsong
parents:
diff changeset
   711
def dot_prod(rs: Set[Rexp], r: Rexp): Set[Rexp] = r match {
Chengsong
parents:
diff changeset
   712
    case ZERO => Set()
Chengsong
parents:
diff changeset
   713
    case ONE => rs
Chengsong
parents:
diff changeset
   714
    case r => rs.map((re) => if (re == ONE) r else SEQ(re, r)  )   
Chengsong
parents:
diff changeset
   715
  }
Chengsong
parents:
diff changeset
   716
  def cir_prod(l: Lin, t: Rexp): Lin = t match {//remember this Lin is different from the Lin in Antimirov's paper. Here it does not mean the set of all subsets of linear forms that does not contain zero, but rather the type a set of linear forms
Chengsong
parents:
diff changeset
   717
    case ZERO => Set()
Chengsong
parents:
diff changeset
   718
    case ONE => l
Chengsong
parents:
diff changeset
   719
    case t => l.map( m => m._2 match {case ZERO => m case ONE => (m._1, t) case p => (m._1, SEQ(p, t)) }  )
Chengsong
parents:
diff changeset
   720
  }
Chengsong
parents:
diff changeset
   721
  def lf(r: Rexp): Lin = r match {
Chengsong
parents:
diff changeset
   722
    case ZERO => Set()
Chengsong
parents:
diff changeset
   723
    case ONE => Set()
Chengsong
parents:
diff changeset
   724
    case CHAR(f) => {
Chengsong
parents:
diff changeset
   725
      //val Some(c) = alphabet.find(f) 
Chengsong
parents:
diff changeset
   726
      Set((f, ONE))
Chengsong
parents:
diff changeset
   727
    }
Chengsong
parents:
diff changeset
   728
    case ALTS(r1, r2) => {
Chengsong
parents:
diff changeset
   729
      lf(r1 ) ++ lf(r2)
Chengsong
parents:
diff changeset
   730
    }
Chengsong
parents:
diff changeset
   731
    case STAR(r1) => cir_prod(lf(r1), STAR(r1)) //may try infix notation later......
Chengsong
parents:
diff changeset
   732
    case SEQ(r1, r2) =>{
Chengsong
parents:
diff changeset
   733
      if (nullable(r1))
Chengsong
parents:
diff changeset
   734
        cir_prod(lf(r1), r2) ++ lf(r2)
Chengsong
parents:
diff changeset
   735
      else
Chengsong
parents:
diff changeset
   736
        cir_prod(lf(r1), r2) 
Chengsong
parents:
diff changeset
   737
    }
Chengsong
parents:
diff changeset
   738
  }
Chengsong
parents:
diff changeset
   739
  def lfs(r: Set[Rexp]): Lin = {
Chengsong
parents:
diff changeset
   740
    r.foldLeft(Set[Mon]())((acc, r) => acc ++ lf(r))
Chengsong
parents:
diff changeset
   741
  }
Chengsong
parents:
diff changeset
   742
Chengsong
parents:
diff changeset
   743
  def pder(x: Char, t: Rexp): Set[Rexp] = {
Chengsong
parents:
diff changeset
   744
    val lft = lf(t)
Chengsong
parents:
diff changeset
   745
    (lft.filter(mon => if(mon._1 == x) true else false)).map(mon => mon._2)
Chengsong
parents:
diff changeset
   746
  }
Chengsong
parents:
diff changeset
   747
  def pders_single(s: List[Char], t: Rexp) : Set[Rexp] = s match {
Chengsong
parents:
diff changeset
   748
    case x::xs => pders(xs, pder(x, t))
Chengsong
parents:
diff changeset
   749
    case Nil => Set(t)
Chengsong
parents:
diff changeset
   750
  }
Chengsong
parents:
diff changeset
   751
  def pders(s: List[Char], ts: Set[Rexp]) : Set[Rexp] = s match {
Chengsong
parents:
diff changeset
   752
    case x::xs => pders(xs, ts.foldLeft(Set[Rexp]())((acc, t) => acc ++ pder(x, t)))
Chengsong
parents:
diff changeset
   753
    case Nil => ts 
Chengsong
parents:
diff changeset
   754
  }
Chengsong
parents:
diff changeset
   755
  def pderss(ss: List[List[Char]], t: Rexp): Set[Rexp] = ss.foldLeft( Set[Rexp]() )( (acc, s) => pders_single(s, t) ++ acc )
Chengsong
parents:
diff changeset
   756
  def pdera(t: Rexp): Set[Rexp] = lf(t).map(mon => mon._2)
Chengsong
parents:
diff changeset
   757
  //all implementation of partial derivatives that involve set union are potentially buggy
Chengsong
parents:
diff changeset
   758
  //because they don't include the original regular term before they are pdered.
Chengsong
parents:
diff changeset
   759
  //now only pderas is fixed.
Chengsong
parents:
diff changeset
   760
  def pderas(t: Set[Rexp], d: Int): Set[Rexp] = if(d > 0) pderas(lfs(t).map(mon => mon._2), d - 1) ++ t else lfs(t).map(mon => mon._2) ++ t//repeated application of pderas over the newest set of pders.
Chengsong
parents:
diff changeset
   761
  def pderUNIV(r: Rexp) : Set[Rexp] = pderas(Set(r), awidth(r) + 1)
Chengsong
parents:
diff changeset
   762
  def awidth(r: Rexp) : Int = r match {
Chengsong
parents:
diff changeset
   763
    case CHAR(c) => 1
Chengsong
parents:
diff changeset
   764
    case SEQ(r1, r2) => awidth(r1) + awidth(r2)
Chengsong
parents:
diff changeset
   765
    case ALTS(r1, r2) => awidth(r1) + awidth(r2)
Chengsong
parents:
diff changeset
   766
    case ONE => 0
Chengsong
parents:
diff changeset
   767
    case ZERO => 0
Chengsong
parents:
diff changeset
   768
    case STAR(r) => awidth(r)
Chengsong
parents:
diff changeset
   769
  }
Chengsong
parents:
diff changeset
   770
  //def sigma_lf(l: Set[Mon]) : Rexp = ALTS(l.map(mon => SEQ(CHAR(mon._1),mon._2)).toList)
Chengsong
parents:
diff changeset
   771
  //def sigma(rs: Set[Rexp]) : Rexp = ALTS(rs.toList)
Chengsong
parents:
diff changeset
   772
  def o(r: Rexp) = if (nullable(r)) ONE else ZERO
Chengsong
parents:
diff changeset
   773
  //def nlf(t: Rexp) : Rexp = ALTS(List( o(t), sigma_lf(lf(t)) ))
Chengsong
parents:
diff changeset
   774
  def pdp(x: Char, r: Rexp) : Set[Rexp] = r match {
Chengsong
parents:
diff changeset
   775
    case ZERO => Set[Rexp]()
Chengsong
parents:
diff changeset
   776
    case ONE => Set[Rexp]()
Chengsong
parents:
diff changeset
   777
    case CHAR(f) => if(x == f) Set(ONE) else Set[Rexp]()
Chengsong
parents:
diff changeset
   778
    case ALTS(r1, r2) => pdp(x, r1) ++ pdp(x, r2)
Chengsong
parents:
diff changeset
   779
    case STAR(r1) => pdp(x, r).map(a => SEQ(a, STAR(r1)))
Chengsong
parents:
diff changeset
   780
    case SEQ(a0, b) => if(nullable(a0)) pdp(x, a0).map(a => SEQ(a, b)) ++ pdp(x, b) else pdp(x, a0).map(a => SEQ(a, b))
Chengsong
parents:
diff changeset
   781
  }
Chengsong
parents:
diff changeset
   782
  def pdps(s: List[Char], ts: Set[Rexp]): Set[Rexp] = s match {
Chengsong
parents:
diff changeset
   783
    case x::xs => pdps(xs, ts.foldLeft(Set[Rexp]())((acc, t) => acc ++ pder(x, t)))
Chengsong
parents:
diff changeset
   784
    case Nil => ts   
Chengsong
parents:
diff changeset
   785
  }
Chengsong
parents:
diff changeset
   786
  def pdpss(ss: List[List[Char]], t: Rexp): Set[Rexp] = ss.foldLeft( Set[Rexp]())((acc, s) => pdps(s, Set(t)) ++ acc)
Chengsong
parents:
diff changeset
   787
Chengsong
parents:
diff changeset
   788
Chengsong
parents:
diff changeset
   789
Chengsong
parents:
diff changeset
   790
def starPrint(r: Rexp) : Unit = r match {
Chengsong
parents:
diff changeset
   791
        
Chengsong
parents:
diff changeset
   792
          case SEQ(head, rstar) =>
Chengsong
parents:
diff changeset
   793
            println(shortRexpOutput(head) ++ "~STARREG")
Chengsong
parents:
diff changeset
   794
          case STAR(rstar) =>
Chengsong
parents:
diff changeset
   795
            println("STARREG")
Chengsong
parents:
diff changeset
   796
          case ALTS(r1, r2) =>  
Chengsong
parents:
diff changeset
   797
            println("(")
Chengsong
parents:
diff changeset
   798
            starPrint(r1)
Chengsong
parents:
diff changeset
   799
            println("+")
Chengsong
parents:
diff changeset
   800
            starPrint(r2)
Chengsong
parents:
diff changeset
   801
            println(")")
Chengsong
parents:
diff changeset
   802
          case ZERO => println("0")
Chengsong
parents:
diff changeset
   803
        
Chengsong
parents:
diff changeset
   804
      }
Chengsong
parents:
diff changeset
   805
Chengsong
parents:
diff changeset
   806
// @arg(doc = "small tests")
Chengsong
parents:
diff changeset
   807
val STARREG = //((( (STAR("aa")) | STAR("aaa") ).%).%)
Chengsong
parents:
diff changeset
   808
(((STAR("a") | ( STAR("aaa")) | STAR("aaaaa"| ( STAR("aaaaaaa")) | STAR("aaaaaaaaaaa"))).%).%).%
Chengsong
parents:
diff changeset
   809
Chengsong
parents:
diff changeset
   810
// @main
Chengsong
parents:
diff changeset
   811
def small() = {
Chengsong
parents:
diff changeset
   812
Chengsong
parents:
diff changeset
   813
Chengsong
parents:
diff changeset
   814
//   println(lexing_simp(NOTREG, prog0))
Chengsong
parents:
diff changeset
   815
//   val v = lex_simp(NOTREG, prog0.toList)
Chengsong
parents:
diff changeset
   816
//   println(v)
Chengsong
parents:
diff changeset
   817
Chengsong
parents:
diff changeset
   818
//   val d =  (lex_simp(NOTREG, prog0.toList))
Chengsong
parents:
diff changeset
   819
//   println(d)
Chengsong
parents:
diff changeset
   820
  val pderSTAR = pderUNIV(STARREG)
Chengsong
parents:
diff changeset
   821
Chengsong
parents:
diff changeset
   822
  val refSize = pderSTAR.map(size(_)).sum
435
Chengsong
parents: 432
diff changeset
   823
  // println("different partial derivative terms:")
Chengsong
parents: 432
diff changeset
   824
  // pderSTAR.foreach(r => r match {
431
Chengsong
parents:
diff changeset
   825
      
435
Chengsong
parents: 432
diff changeset
   826
  //       case SEQ(head, rstar) =>
Chengsong
parents: 432
diff changeset
   827
  //         println(shortRexpOutput(head) ++ "~STARREG")
Chengsong
parents: 432
diff changeset
   828
  //       case STAR(rstar) =>
Chengsong
parents: 432
diff changeset
   829
  //         println("STARREG")
431
Chengsong
parents:
diff changeset
   830
      
435
Chengsong
parents: 432
diff changeset
   831
  //   }
Chengsong
parents: 432
diff changeset
   832
  //   )
Chengsong
parents: 432
diff changeset
   833
  // println("the total number of terms is")
Chengsong
parents: 432
diff changeset
   834
  // //println(refSize)
Chengsong
parents: 432
diff changeset
   835
  // println(pderSTAR.size)
431
Chengsong
parents:
diff changeset
   836
432
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   837
  val A : Rexp= ("c" | (ONE | "b") ~ "d") ~((ONE).%)
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   838
  val B : Rexp = ((ONE).%)
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   839
  val C : Rexp = ("d") ~ ((ONE).%)
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   840
  val PRUNE_REG : Rexp = (C | B | A)
994403dbbed5 strong!
Chengsong
parents: 431
diff changeset
   841
  val APRUNE_REG = internalise(PRUNE_REG)
435
Chengsong
parents: 432
diff changeset
   842
  // // val program_solution = pruneRexp(APRUNE_REG, breakIntoTerms(PRUNE_REG))
Chengsong
parents: 432
diff changeset
   843
  // // println("program executes and gives: as disired!")
Chengsong
parents: 432
diff changeset
   844
  // // println(shortRexpOutput(erase(program_solution)))
Chengsong
parents: 432
diff changeset
   845
  // val simpedPruneReg = strongBsimp(APRUNE_REG)
Chengsong
parents: 432
diff changeset
   846
Chengsong
parents: 432
diff changeset
   847
  // println(shortRexpOutput(erase(simpedPruneReg)))
Chengsong
parents: 432
diff changeset
   848
  // for(i <- List(1,2 ) ){// 100, 400, 800, 840, 841, 900
Chengsong
parents: 432
diff changeset
   849
  //   val prog0 = "a" * i
Chengsong
parents: 432
diff changeset
   850
  //   //println(s"test: $prog0")
Chengsong
parents: 432
diff changeset
   851
  //   println(s"testing with $i a's" )
Chengsong
parents: 432
diff changeset
   852
  //   //val bd = bdersSimp(prog0, STARREG)//DB
Chengsong
parents: 432
diff changeset
   853
  //   val sbd = bdersSimpS(prog0, STARREG)//strongDB
Chengsong
parents: 432
diff changeset
   854
  //   starPrint(erase(sbd))
Chengsong
parents: 432
diff changeset
   855
  //   val subTerms = breakIntoTerms(erase(sbd))
Chengsong
parents: 432
diff changeset
   856
  //   //val subTermsLarge = breakIntoTerms(erase(bd))
431
Chengsong
parents:
diff changeset
   857
    
435
Chengsong
parents: 432
diff changeset
   858
  //   println(s"subterms of regex with strongDB: ${subTerms.length}")//, standard DB: ${subTermsLarge.length}")
431
Chengsong
parents:
diff changeset
   859
Chengsong
parents:
diff changeset
   860
Chengsong
parents:
diff changeset
   861
435
Chengsong
parents: 432
diff changeset
   862
  //   println("the number of distinct subterms for bsimp with strongDB")
Chengsong
parents: 432
diff changeset
   863
  //   println(subTerms.distinct.size)
Chengsong
parents: 432
diff changeset
   864
  //   //println(subTermsLarge.distinct.size)
Chengsong
parents: 432
diff changeset
   865
  //   println("which coincides with the number of PDER terms")
431
Chengsong
parents:
diff changeset
   866
Chengsong
parents:
diff changeset
   867
435
Chengsong
parents: 432
diff changeset
   868
  //   // println(shortRexpOutput(erase(sbd)))
Chengsong
parents: 432
diff changeset
   869
  //   // println(shortRexpOutput(erase(bd)))
431
Chengsong
parents:
diff changeset
   870
    
435
Chengsong
parents: 432
diff changeset
   871
  //   println("pdersize, original, strongSimp")
Chengsong
parents: 432
diff changeset
   872
  //   println(refSize, size(STARREG),  asize(sbd))
431
Chengsong
parents:
diff changeset
   873
435
Chengsong
parents: 432
diff changeset
   874
  //   val vres = strong_blexing_simp( STARREG, prog0)
Chengsong
parents: 432
diff changeset
   875
  //   println(vres)
Chengsong
parents: 432
diff changeset
   876
  // }
431
Chengsong
parents:
diff changeset
   877
//   println(vs.length)
Chengsong
parents:
diff changeset
   878
//   println(vs)
Chengsong
parents:
diff changeset
   879
   
Chengsong
parents:
diff changeset
   880
Chengsong
parents:
diff changeset
   881
  // val prog1 = """read  n; write n"""  
Chengsong
parents:
diff changeset
   882
  // println(s"test: $prog1")
Chengsong
parents:
diff changeset
   883
  // println(lexing_simp(WHILE_REGS, prog1))
435
Chengsong
parents: 432
diff changeset
   884
  val display = ("a"| "ab").%
Chengsong
parents: 432
diff changeset
   885
  val adisplay = internalise(display)
Chengsong
parents: 432
diff changeset
   886
  bders_simp( "aaaaa".toList, adisplay)
431
Chengsong
parents:
diff changeset
   887
}
Chengsong
parents:
diff changeset
   888
Chengsong
parents:
diff changeset
   889
small()