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