| 
0
 | 
     1  | 
import Element.elem
  | 
| 
 | 
     2  | 
import RexpRelated._
  | 
| 
 | 
     3  | 
import RexpRelated.Rexp._
  | 
| 
 | 
     4  | 
import Partial._
  | 
| 
 | 
     5  | 
import BRexp._
  | 
| 
 | 
     6  | 
import scala.collection.mutable.ListBuffer
  | 
| 
 | 
     7  | 
object Spiral{
 | 
| 
 | 
     8  | 
  | 
| 
 | 
     9  | 
  val space = elem(" ")
 | 
| 
 | 
    10  | 
  val corner = elem("+")
 | 
| 
 | 
    11  | 
  | 
| 
 | 
    12  | 
  def spiral(nEdges: Int, direction: Int): Element = {
 | 
| 
 | 
    13  | 
    if(nEdges == 1)
  | 
| 
 | 
    14  | 
      elem("+")
 | 
| 
 | 
    15  | 
    else {
 | 
| 
 | 
    16  | 
      val sp = spiral(nEdges - 1, (direction + 3) % 4)
  | 
| 
 | 
    17  | 
      def verticalBar = elem('|', 1, sp.height)
 | 
| 
 | 
    18  | 
      def horizontalBar = elem('-', sp.width, 1)
 | 
| 
 | 
    19  | 
      if(direction == 0)
  | 
| 
 | 
    20  | 
        (corner beside horizontalBar) above sp//(sp beside space)
  | 
| 
 | 
    21  | 
      else if (direction == 1)
  | 
| 
 | 
    22  | 
        sp beside (corner above verticalBar)
  | 
| 
 | 
    23  | 
      else if (direction == 2)
  | 
| 
 | 
    24  | 
        (space beside sp) above (horizontalBar beside corner)
  | 
| 
 | 
    25  | 
      else
  | 
| 
 | 
    26  | 
        (verticalBar above corner) beside (space above sp)
  | 
| 
 | 
    27  | 
    }
  | 
| 
 | 
    28  | 
  }
  | 
| 
 | 
    29  | 
  val alphabet = ("""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:"=()\;-+*!<>\/%{} """+"\n\t").toSet//Set('a','b','c')
 | 
| 
 | 
    30  | 
  def bregx_tree(r: BRexp): Element = regx_tree(berase(r))
  | 
| 
 | 
    31  | 
  def regx_tree(r: Rexp): Element = aregx_tree(internalise(r))
  | 
| 
5
 | 
    32  | 
  def annotated_tree(r: ARexp): Element = {
 | 
| 
 | 
    33  | 
    r match {
 | 
| 
 | 
    34  | 
      case ACHAR(bs, d) => {
 | 
| 
 | 
    35  | 
        //val Some(d) = alphabet.find(f)
  | 
| 
 | 
    36  | 
        d match {
 | 
| 
 | 
    37  | 
          case '\n' => elem("\\n")
 | 
| 
 | 
    38  | 
          case '\t' => elem("\\t")
 | 
| 
 | 
    39  | 
          case ' ' => elem("space")
 | 
| 
 | 
    40  | 
          case d => elem(d.toString)
  | 
| 
 | 
    41  | 
        }   
  | 
| 
 | 
    42  | 
      }
  | 
| 
 | 
    43  | 
      case AONE(bs) => {
 | 
| 
 | 
    44  | 
        elem("ONE")
 | 
| 
 | 
    45  | 
      }
  | 
| 
 | 
    46  | 
      case AZERO => {
 | 
| 
 | 
    47  | 
        elem("ZERO")
 | 
| 
 | 
    48  | 
      }
  | 
| 
 | 
    49  | 
      case ASEQ(bs, r1, r2) => {
 | 
| 
 | 
    50  | 
        binary_print("SEQ", r1, r2)
 | 
| 
 | 
    51  | 
      }
  | 
| 
 | 
    52  | 
      case AALTS(bs, rs) => {
 | 
| 
 | 
    53  | 
        //elem("Awaiting completion")
 | 
| 
 | 
    54  | 
        list_print("ALT", rs)
 | 
| 
 | 
    55  | 
      }
  | 
| 
 | 
    56  | 
      case ASTAR(bs, r) => {
 | 
| 
 | 
    57  | 
        list_print("STA", List(r))
 | 
| 
 | 
    58  | 
      }
  | 
| 
 | 
    59  | 
    } 
  | 
| 
 | 
    60  | 
  }
  | 
| 
0
 | 
    61  | 
  def aregx_tree(r: ARexp): Element = {
 | 
| 
 | 
    62  | 
    r match {
 | 
| 
 | 
    63  | 
      case ACHAR(bs, d) => {
 | 
| 
 | 
    64  | 
        //val Some(d) = alphabet.find(f)
  | 
| 
 | 
    65  | 
        d match {
 | 
| 
 | 
    66  | 
          case '\n' => elem("\\n")
 | 
| 
 | 
    67  | 
          case '\t' => elem("\\t")
 | 
| 
 | 
    68  | 
          case ' ' => elem("space")
 | 
| 
 | 
    69  | 
          case d => elem(d.toString)
  | 
| 
 | 
    70  | 
        }   
  | 
| 
 | 
    71  | 
      }
  | 
| 
 | 
    72  | 
      case AONE(bs) => {
 | 
| 
 | 
    73  | 
        elem("ONE")
 | 
| 
 | 
    74  | 
      }
  | 
| 
 | 
    75  | 
      case AZERO => {
 | 
| 
 | 
    76  | 
        elem("ZERO")
 | 
| 
 | 
    77  | 
      }
  | 
| 
 | 
    78  | 
      case ASEQ(bs, r1, r2) => {
 | 
| 
 | 
    79  | 
        binary_print("SEQ", r1, r2)
 | 
| 
 | 
    80  | 
      }
  | 
| 
 | 
    81  | 
      case AALTS(bs, rs) => {
 | 
| 
 | 
    82  | 
        //elem("Awaiting completion")
 | 
| 
 | 
    83  | 
        list_print("ALT", rs)
 | 
| 
 | 
    84  | 
      }
  | 
| 
 | 
    85  | 
      case ASTAR(bs, r) => {
 | 
| 
 | 
    86  | 
        list_print("STA", List(r))
 | 
| 
 | 
    87  | 
      }
  | 
| 
 | 
    88  | 
    }
  | 
| 
 | 
    89  | 
  }
  | 
| 
 | 
    90  | 
  val port = elem(" └-")
 | 
| 
 | 
    91  | 
  def list_print(name: String, rs: List[ARexp]): Element = {
 | 
| 
 | 
    92  | 
    rs match {
 | 
| 
 | 
    93  | 
      case r::Nil => {
 | 
| 
 | 
    94  | 
        val pref = aregx_tree(r)
  | 
| 
 | 
    95  | 
        val head = elem(name)
  | 
| 
 | 
    96  | 
        (head left_align (port up_align pref) ) 
  | 
| 
 | 
    97  | 
      }
  | 
| 
 | 
    98  | 
      case r2::r1::Nil => {
 | 
| 
 | 
    99  | 
        binary_print(name, r2, r1)
  | 
| 
 | 
   100  | 
      }
  | 
| 
 | 
   101  | 
      case r::rs1 => {
 | 
| 
 | 
   102  | 
        val pref = aregx_tree(r)
  | 
| 
 | 
   103  | 
        val head = elem(name)
  | 
| 
 | 
   104  | 
        if (pref.height > 1){
 | 
| 
 | 
   105  | 
          val link = elem('|', 1, pref.height - 1)
 | 
| 
 | 
   106  | 
          (head left_align ((port above link) beside pref)) left_align tail_print(rs1)    
  | 
| 
 | 
   107  | 
        }
  | 
| 
 | 
   108  | 
        else{
 | 
| 
 | 
   109  | 
          (head left_align (port beside pref) ) left_align tail_print(rs1)
  | 
| 
 | 
   110  | 
        }
  | 
| 
 | 
   111  | 
      }
  | 
| 
 | 
   112  | 
    }
  | 
| 
 | 
   113  | 
  }
  | 
| 
 | 
   114  | 
  def tail_print(rs: List[ARexp]): Element = {
 | 
| 
 | 
   115  | 
    rs match {
 | 
| 
 | 
   116  | 
      case r2::r1::Nil => {
 | 
| 
 | 
   117  | 
        val pref = aregx_tree(r2)
  | 
| 
 | 
   118  | 
        val suff = aregx_tree(r1)
  | 
| 
 | 
   119  | 
        if (pref.height > 1){
 | 
| 
 | 
   120  | 
          val link = elem('|', 1, pref.height - 1)
 | 
| 
 | 
   121  | 
          ((port above link) beside pref) left_align (port up_align suff)
  | 
| 
 | 
   122  | 
        }
  | 
| 
 | 
   123  | 
        else{
 | 
| 
 | 
   124  | 
          (port beside pref) left_align (port up_align suff)
  | 
| 
 | 
   125  | 
        } 
  | 
| 
 | 
   126  | 
      }
  | 
| 
 | 
   127  | 
      case r2::rs1 => {
 | 
| 
 | 
   128  | 
        val pref = aregx_tree(r2)
  | 
| 
 | 
   129  | 
        
  | 
| 
 | 
   130  | 
        if (pref.height > 1){
 | 
| 
 | 
   131  | 
          val link = elem('|', 1, pref.height - 1)
 | 
| 
 | 
   132  | 
          ((port above link) beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1) )
  | 
| 
 | 
   133  | 
        }
  | 
| 
 | 
   134  | 
        else{
 | 
| 
 | 
   135  | 
          (port beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1))
  | 
| 
 | 
   136  | 
        } 
  | 
| 
 | 
   137  | 
        //pref left_align tail_print(rs1)
  | 
| 
 | 
   138  | 
      }
  | 
| 
 | 
   139  | 
    }
  | 
| 
 | 
   140  | 
  }
  | 
| 
 | 
   141  | 
  | 
| 
 | 
   142  | 
  def binary_print(name: String, r1: ARexp, r2: ARexp): Element = {
 | 
| 
 | 
   143  | 
    val pref = aregx_tree(r1)
  | 
| 
 | 
   144  | 
    val suff = aregx_tree(r2)
  | 
| 
 | 
   145  | 
    val head = elem(name)
  | 
| 
 | 
   146  | 
    if (pref.height > 1){
 | 
| 
 | 
   147  | 
      val link = elem('|', 1, pref.height - 1)
 | 
| 
 | 
   148  | 
      (head left_align ((port above link) beside pref) ) left_align (port up_align suff)
  | 
| 
 | 
   149  | 
    }
  | 
| 
 | 
   150  | 
    else{
 | 
| 
 | 
   151  | 
      (head left_align (port beside pref) ) left_align (port up_align suff)
  | 
| 
 | 
   152  | 
    }
  | 
| 
 | 
   153  | 
  }
  | 
| 
 | 
   154  | 
  val arr_of_size = ListBuffer.empty[Int]
  | 
| 
2
 | 
   155  | 
  | 
| 
0
 | 
   156  | 
  def pC(r: Rexp): Set[Rexp] = {//PD's companion
 | 
| 
 | 
   157  | 
    r match {
 | 
| 
 | 
   158  | 
      case SEQ(r1, r2) => pC(r2)
  | 
| 
 | 
   159  | 
      case ALTS(rs) => rs.flatMap(a => pC(a) ).toSet
  | 
| 
 | 
   160  | 
      case CHAR(c) => Set(r)
  | 
| 
 | 
   161  | 
      case r => Set()
  | 
| 
 | 
   162  | 
    }
  | 
| 
 | 
   163  | 
  }
  | 
| 
 | 
   164  | 
  def illustration(r: Rexp, s: String){
 | 
| 
 | 
   165  | 
    var i_like_imperative_style = internalise(r)
  | 
| 
 | 
   166  | 
    val all_chars = s.toList
  | 
| 
 | 
   167  | 
    for (i <- 0 to s.length - 1){
 | 
| 
 | 
   168  | 
      val der_res =  bder(all_chars(i), i_like_imperative_style)
  | 
| 
 | 
   169  | 
      val simp_res = bsimp(der_res)
  | 
| 
 | 
   170  | 
      println("The original regex, the regex after derivative w.r.t " + all_chars(i) + " and the simplification of the derivative.")
 | 
| 
 | 
   171  | 
      println(aregx_tree(i_like_imperative_style) up_align aregx_tree(der_res) up_align aregx_tree(simp_res))
  | 
| 
 | 
   172  | 
      //println(asize(i_like_imperative_style), asize(der_res), asize(simp_res))
  | 
| 
 | 
   173  | 
      arr_of_size += asize(i_like_imperative_style)
  | 
| 
 | 
   174  | 
      //println(asize(simp_res), asize(simp_res) / arr_of_size(0) )
  | 
| 
 | 
   175  | 
      i_like_imperative_style = simp_res
  | 
| 
 | 
   176  | 
    }
  | 
| 
 | 
   177  | 
    arr_of_size += asize(i_like_imperative_style)
  | 
| 
 | 
   178  | 
  }
  | 
| 
 | 
   179  | 
  val ran = scala.util.Random
  | 
| 
 | 
   180  | 
  var alphabet_size = 3
  | 
| 
 | 
   181  | 
  def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = {
 | 
| 
 | 
   182  | 
    if(depth == 1){
 | 
| 
 | 
   183  | 
      ((ran.nextInt(4) + 97).toChar).toString
  | 
| 
 | 
   184  | 
    }
  | 
| 
 | 
   185  | 
    else if(star){
 | 
| 
 | 
   186  | 
      STAR(balanced_seq_star_gen(depth - 1, false))
  | 
| 
 | 
   187  | 
    }
  | 
| 
 | 
   188  | 
    else{
 | 
| 
 | 
   189  | 
      SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true))
  | 
| 
 | 
   190  | 
    }
  | 
| 
 | 
   191  | 
  }
  | 
| 
 | 
   192  | 
  def max(i: Int, j: Int): Int = {
 | 
| 
 | 
   193  | 
    if(i > j)
  | 
| 
 | 
   194  | 
      i
  | 
| 
 | 
   195  | 
    else 
  | 
| 
 | 
   196  | 
      j
  | 
| 
 | 
   197  | 
  }
  | 
| 
 | 
   198  | 
  def random_struct_gen(depth:Int): Rexp = {
 | 
| 
 | 
   199  | 
    val dice = ran.nextInt(3)
  | 
| 
 | 
   200  | 
    val dice2 = ran.nextInt(3)
  | 
| 
 | 
   201  | 
    (dice, depth) match {
 | 
| 
 | 
   202  | 
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
  | 
| 
 | 
   203  | 
      case (0, i) => STAR(random_struct_gen(max(0, i - 1 - dice2)))
  | 
| 
 | 
   204  | 
      case (1, i) => SEQ(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2)))
  | 
| 
 | 
   205  | 
      case (2, i) => ALTS( List(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2))) )
  | 
| 
 | 
   206  | 
    }
  | 
| 
 | 
   207  | 
  }
  | 
| 
 | 
   208  | 
  def balanced_struct_gen(depth: Int): Rexp = {
 | 
| 
 | 
   209  | 
    val dice = ran.nextInt(3)
  | 
| 
 | 
   210  | 
    (dice, depth) match {
 | 
| 
 | 
   211  | 
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
  | 
| 
 | 
   212  | 
      case (0, i) => STAR(random_struct_gen(depth - 1))
  | 
| 
 | 
   213  | 
      case (1, i) => SEQ(random_struct_gen(depth - 1), random_struct_gen(depth - 1))
  | 
| 
 | 
   214  | 
      case (2, i) => ALTS( List(random_struct_gen(depth - 1), random_struct_gen(depth - 1) ) )
  | 
| 
 | 
   215  | 
    }
  | 
| 
 | 
   216  | 
  }
  | 
| 
6
 | 
   217  | 
  def random_pool(n: Int, d: Int) : Stream[Rexp] = n match {
 | 
| 
 | 
   218  | 
    case 0 => (for (i <- 1 to 10) yield balanced_struct_gen(d)).toStream
  | 
| 
 | 
   219  | 
    case n => {  
 | 
| 
 | 
   220  | 
      val rs = random_pool(n - 1, d)
  | 
| 
 | 
   221  | 
      rs #:::
  | 
| 
 | 
   222  | 
      (for (i <- (1 to 10).toStream) yield balanced_struct_gen(d)) #::: 
  | 
| 
 | 
   223  | 
      (for (i <- (1 to 10).toStream) yield random_struct_gen(d))
  | 
| 
 | 
   224  | 
    }
  | 
| 
 | 
   225  | 
  }
  | 
| 
 | 
   226  | 
  | 
| 
0
 | 
   227  | 
  def rd_string_gen(alp_size: Int, len: Int): String = {
 | 
| 
 | 
   228  | 
    if( len > 0)
  | 
| 
 | 
   229  | 
      ((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1)
  | 
| 
 | 
   230  | 
    else
  | 
| 
 | 
   231  | 
      ((ran.nextInt(alp_size) + 97).toChar).toString
  | 
| 
 | 
   232  | 
  }
  | 
| 
 | 
   233  | 
  def plot(b: List[Int]){
 | 
| 
 | 
   234  | 
    println(b(0),b.max)
  | 
| 
 | 
   235  | 
  | 
| 
 | 
   236  | 
  }
  | 
| 
 | 
   237  | 
  def dep_exp(depth: List[Int]){
 | 
| 
 | 
   238  | 
    for(i <- depth){
 | 
| 
 | 
   239  | 
      arr_of_size.clear()
  | 
| 
 | 
   240  | 
      val s = rd_string_gen(alphabet_size, (i-8)*(i-8)+10)
  | 
| 
 | 
   241  | 
      val r = random_struct_gen(i)
  | 
| 
 | 
   242  | 
      println("depth: "+i)
 | 
| 
 | 
   243  | 
      illustration(r, s) //"abcabadaaadcabdbabcdaadbabbcbbdabdabbcbdbabdbcdb") 
  | 
| 
 | 
   244  | 
      //println("depth: " + i + " general stats:"+ arr_of_size(0), arr_of_size.max, arr_of_size.max/arr_of_size(0))
 | 
| 
 | 
   245  | 
      //println("x y label alignment")
 | 
| 
 | 
   246  | 
      /*for(i <- 0 to s.length - 1){
 | 
| 
 | 
   247  | 
        if(s(i) == '\n')
  | 
| 
 | 
   248  | 
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
  | 
| 
 | 
   249  | 
        else if(s(i) ==  ' ')
  | 
| 
 | 
   250  | 
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
  | 
| 
 | 
   251  | 
        else
  | 
| 
 | 
   252  | 
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
  | 
| 
 | 
   253  | 
      }*/
  | 
| 
 | 
   254  | 
      //println(s.length + " " + arr_of_size(s.length) + " ]" + " -140")
  | 
| 
 | 
   255  | 
    }
  | 
| 
 | 
   256  | 
  }
  | 
| 
 | 
   257  | 
  def case_study(ss: List[String], r: Rexp){
 | 
| 
 | 
   258  | 
    for(s <- ss){
 | 
| 
 | 
   259  | 
      arr_of_size.clear()
  | 
| 
 | 
   260  | 
      illustration(r, s)
  | 
| 
 | 
   261  | 
      println("x y label alignment")
 | 
| 
 | 
   262  | 
      for(i <- 0 to s.length - 1){
 | 
| 
 | 
   263  | 
        if(s(i) == '\n')
  | 
| 
 | 
   264  | 
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
  | 
| 
 | 
   265  | 
        else if(s(i) ==  ' ')
  | 
| 
 | 
   266  | 
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
  | 
| 
 | 
   267  | 
        else
  | 
| 
 | 
   268  | 
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
  | 
| 
 | 
   269  | 
      }
  | 
| 
 | 
   270  | 
    }
  | 
| 
 | 
   271  | 
  }
  | 
| 
 | 
   272  | 
  def star_gen(dp: Int): Rexp = {
 | 
| 
 | 
   273  | 
    if(dp > 0)
  | 
| 
 | 
   274  | 
      STAR(star_gen(dp - 1))
  | 
| 
 | 
   275  | 
    else
  | 
| 
 | 
   276  | 
      "a"
  | 
| 
 | 
   277  | 
  }
  | 
| 
 | 
   278  | 
  def strs_gen(len: Int, num: Int): List[String] = {
 | 
| 
 | 
   279  | 
    if(num > 0){
 | 
| 
 | 
   280  | 
      rd_string_gen(3, len)::strs_gen(len, num - 1)
  | 
| 
 | 
   281  | 
    }
  | 
| 
 | 
   282  | 
    else{
 | 
| 
 | 
   283  | 
      Nil
  | 
| 
 | 
   284  | 
    }
  | 
| 
 | 
   285  | 
  }
  | 
| 
 | 
   286  | 
  def regx_print(r: Rexp): String = {
 | 
| 
 | 
   287  | 
    r match {
 | 
| 
 | 
   288  | 
      case ZERO =>
  | 
| 
 | 
   289  | 
        "ZERO"
  | 
| 
 | 
   290  | 
      case CHAR(c) => {
 | 
| 
 | 
   291  | 
         //val Some(c) = alphabet.find(f)
  | 
| 
 | 
   292  | 
         "\"" + c.toString + "\""
  | 
| 
 | 
   293  | 
      }
  | 
| 
 | 
   294  | 
      case ONE => {
 | 
| 
 | 
   295  | 
        "ONE"
  | 
| 
 | 
   296  | 
      }
  | 
| 
 | 
   297  | 
      case ALTS(rs) => {
 | 
| 
 | 
   298  | 
        "ALTS(List("+(rs.map(regx_print)).foldLeft("")((a, b) => if(a == "") b else a + "," + b)+"))"
 | 
| 
 | 
   299  | 
      }
  | 
| 
 | 
   300  | 
      case SEQ(r1, r2) => {
 | 
| 
 | 
   301  | 
        "SEQ(" + regx_print(r1) + "," + regx_print(r2) + ")"
 | 
| 
 | 
   302  | 
      }
  | 
| 
 | 
   303  | 
      case STAR(r) => {
 | 
| 
 | 
   304  | 
        "STAR(" + regx_print(r) + ")"
 | 
| 
 | 
   305  | 
      }
  | 
| 
 | 
   306  | 
    }
  | 
| 
 | 
   307  | 
  }
  | 
| 
 | 
   308  | 
  val mkst = "abcdefghijklmnopqrstuvwxyz"
  | 
| 
 | 
   309  | 
  def weak_sub_check(r: Rexp, s: String, i: Int, f: (List[Rexp], Set[Rexp]) => Boolean){
 | 
| 
 | 
   310  | 
    //we first compute pders over the set of all strings on the alphabet
  | 
| 
 | 
   311  | 
    val pd = pderas(Set(r), i + 4)
  | 
| 
 | 
   312  | 
    //then "b-internalise" the regular expression into a brexp(this is essentially 
  | 
| 
 | 
   313  | 
    //attaching a bit Z to every alts to signify that they come from the original regular expression)
  | 
| 
 | 
   314  | 
    var old = brternalise(r)
  | 
| 
 | 
   315  | 
    //this is for comparison between normal simp and the weakened version of simp
  | 
| 
 | 
   316  | 
    //normal simp will be performed on syncold
  | 
| 
 | 
   317  | 
    //weakend simp will be performed on old
  | 
| 
5
 | 
   318  | 
    var syncold = internalise(r)
  | 
| 
0
 | 
   319  | 
    val all_chars = s.toList
  | 
| 
 | 
   320  | 
    for (i <- 0 to s.length - 1){
 | 
| 
5
 | 
   321  | 
      val syncder_res = bder(all_chars(i), syncold)
  | 
| 
 | 
   322  | 
      val syncsimp_res = super_bsimp(syncder_res)
  | 
| 
0
 | 
   323  | 
      //see brder for detailed explanation
  | 
| 
 | 
   324  | 
      //just changes bit Z to S when deriving an ALTS, 
  | 
| 
 | 
   325  | 
      //signifying that the structure has been "touched" and
  | 
| 
 | 
   326  | 
      //therefore able to be spilled in the bspill function
  | 
| 
 | 
   327  | 
      val der_res =  brder(all_chars(i), old)
  | 
| 
 | 
   328  | 
      val simp_res = br_simp(der_res)
  | 
| 
 | 
   329  | 
      val anatomy = bspill(simp_res)
  | 
| 
 | 
   330  | 
      //track if the number of regular expressions exceeds those in the PD set(remember PD means the pders over A*)
  | 
| 
5
 | 
   331  | 
      if(f(List(berase(simp_res)), pd)  == false ){
 | 
| 
 | 
   332  | 
        println(size(erase(syncsimp_res)))
  | 
| 
0
 | 
   333  | 
        println(size(berase(simp_res)))
  | 
| 
3
 | 
   334  | 
        println(bregx_tree(simp_res))
  | 
| 
5
 | 
   335  | 
        println(s)
  | 
| 
 | 
   336  | 
        println(i)
  | 
| 
 | 
   337  | 
        println(r)
  | 
| 
 | 
   338  | 
        println()
  | 
| 
0
 | 
   339  | 
        println(anatomy.map(size).sum)
  | 
| 
 | 
   340  | 
        println(pd.map(size).sum)
  | 
| 
 | 
   341  | 
      }  
  | 
| 
 | 
   342  | 
      old = simp_res
  | 
| 
 | 
   343  | 
      syncold = syncsimp_res
  | 
| 
 | 
   344  | 
    }
  | 
| 
 | 
   345  | 
  }
  | 
| 
 | 
   346  | 
  def inclusion_truth(anatomy: List[Rexp], pd: Set[Rexp]): Boolean = {
 | 
| 
 | 
   347  | 
    val aset = anatomy.toSet
  | 
| 
 | 
   348  | 
    if(aset subsetOf pd){
 | 
| 
 | 
   349  | 
      true
  | 
| 
 | 
   350  | 
    }
  | 
| 
 | 
   351  | 
    else{
 | 
| 
 | 
   352  | 
      println("inclusion not true")
 | 
| 
 | 
   353  | 
      false
  | 
| 
 | 
   354  | 
    }
  | 
| 
 | 
   355  | 
  }
  | 
| 
 | 
   356  | 
  def size_comp(anatomy: List[Rexp], pd: Set[Rexp]):Boolean = {println("size of PD and bspilled simp regx: ", pd.size, anatomy.size); true}
 | 
| 
5
 | 
   357  | 
  def size_expansion_rate(r: List[Rexp], pd: Set[Rexp]): Boolean = if(size(r(0)) > (pd.map(size).sum) * 3 ) { false }else {true}
 | 
| 
4
 | 
   358  | 
  def ders_simp(r: ARexp, s: List[Char]): ARexp = {
 | 
| 
 | 
   359  | 
    s match {
 | 
| 
 | 
   360  | 
      case Nil => r 
  | 
| 
 | 
   361  | 
      case c::cs => ders_simp(bsimp(bder(c, r)), cs)
  | 
| 
 | 
   362  | 
    }
  | 
| 
 | 
   363  | 
  }
  | 
| 
5
 | 
   364  | 
  val big_panda = STAR(STAR(STAR(ALTS(List(ALTS(List(CHAR('c'), CHAR('b'))), SEQ(CHAR('c'),CHAR('c')))))))
 | 
| 
 | 
   365  | 
  val str_panda = "ccccb"
  | 
| 
0
 | 
   366  | 
  def check_all(){
 | 
| 
5
 | 
   367  | 
  | 
| 
 | 
   368  | 
        weak_sub_check(big_panda, str_panda, 6, size_expansion_rate)
  | 
| 
 | 
   369  | 
  | 
| 
0
 | 
   370  | 
  }
  | 
| 
5
 | 
   371  | 
  //simplified regex size 291, so called pd_simp size 70 (did not do simplification to terms in the PD set)
  | 
| 
 | 
   372  | 
  | 
| 
4
 | 
   373  | 
  def correctness_proof_convenient_path(){
 | 
| 
 | 
   374  | 
    for(i <- 1 to 1)
  | 
| 
 | 
   375  | 
    {
 | 
| 
 | 
   376  | 
        val s = "abaa"//rd_string_gen(alphabet_size, 3)
  | 
| 
 | 
   377  | 
        val r = ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) //random_struct_gen(7)
 | 
| 
 | 
   378  | 
        for(j <- 0 to s.length - 1){
 | 
| 
 | 
   379  | 
          val ss = s.slice(0, j+ 1)
  | 
| 
 | 
   380  | 
          val nangao = ders_simp(r, ss.toList)
  | 
| 
 | 
   381  | 
          val easy = bsimp(bders(ss.toList, r))
  | 
| 
5
 | 
   382  | 
          if(true){
 | 
| 
4
 | 
   383  | 
            println(j)
  | 
| 
 | 
   384  | 
            if(j == 3) println("not equal")
 | 
| 
5
 | 
   385  | 
            println("string")
 | 
| 
4
 | 
   386  | 
            println(ss)
  | 
| 
5
 | 
   387  | 
            println("original regex")
 | 
| 
4
 | 
   388  | 
            println(r)
  | 
| 
5
 | 
   389  | 
            println("regex after ders simp")
 | 
| 
4
 | 
   390  | 
            println(nangao)
  | 
| 
5
 | 
   391  | 
            println("regex after ders")
 | 
| 
 | 
   392  | 
            println(bders(ss.toList, r))
  | 
| 
 | 
   393  | 
            println("regex after ders and then a single simp")
 | 
| 
4
 | 
   394  | 
            println(easy)
  | 
| 
 | 
   395  | 
          }
  | 
| 
 | 
   396  | 
        }
  | 
| 
 | 
   397  | 
    }
  | 
| 
 | 
   398  | 
  }
  | 
| 
0
 | 
   399  | 
  def main(args: Array[String]) {
 | 
| 
4
 | 
   400  | 
    //check_all()   
  | 
| 
6
 | 
   401  | 
    //enum(3, "abc").map(tests_blexer_simp(strs(3, "abc"))).toSet
  | 
| 
 | 
   402  | 
    //
  | 
| 
 | 
   403  | 
    random_pool(1, 5).map(tests_blexer_simp(strs(5, "abc"))).toSet
  | 
| 
5
 | 
   404  | 
    //correctness_proof_convenient_path()
  | 
| 
0
 | 
   405  | 
  } 
  | 
| 
 | 
   406  | 
}
  | 
| 
 | 
   407  | 
  |