exps/profile.scala
author Chengsong
Wed, 02 Mar 2022 23:13:59 +0000
changeset 438 a73b2e553804
parent 305 6e2cef17a9b3
permissions -rw-r--r--
templateforPhd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.language.implicitConversions    
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import scala.language.reflectiveCalls
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import scala.annotation.tailrec   
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.util.Try
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def escape(raw: String) : String = {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  import scala.reflect.runtime.universe._
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  Literal(Constant(raw)).toString
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
def esc2(r: (String, String)) = (escape(r._1), escape(r._2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case Nil => Nil
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case (x::xs) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
    val res = f(x)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    if (acc.contains(res)) distinctBy(xs, f, acc)  
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    else x::distinctBy(xs, f, res::acc)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
} 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
abstract class Bit
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object Z extends Bit
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object S extends Bit
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class C(c: Char) extends Bit
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
type Bits = List[Bit]
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
// usual regular expressions
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
abstract class Rexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case object ZERO extends Rexp
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case object ONE extends Rexp
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    34
case class PRED(f: Char => Boolean, s: String = "_") extends Rexp 
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
case class ALTS(rs: List[Rexp]) extends Rexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
case class STAR(r: Rexp) extends Rexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
case class RECD(x: String, r: Rexp) extends Rexp
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// abbreviations
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    42
def CHAR(c: Char) = PRED(_ == c, c.toString)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def PLUS(r: Rexp) = SEQ(r, STAR(r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// annotated regular expressions
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
abstract class ARexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
case object AZERO extends ARexp
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
case class AONE(bs: Bits) extends ARexp
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    50
case class APRED(bs: Bits, f: Char => Boolean, s: String = "_") extends ARexp
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// abbreviations
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// values
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
abstract class Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
case object Empty extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
case class Chr(c: Char) extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
case class Sequ(v1: Val, v2: Val) extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
case class Left(v: Val) extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
case class Right(v: Val) extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
case class Stars(vs: List[Val]) extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
case class Rec(x: String, v: Val) extends Val
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
   
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// some convenience for typing in regular expressions
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
def charlist2rexp(s : List[Char]): Rexp = s match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  case Nil => ONE
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  case c::Nil => CHAR(c)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
implicit def RexpOps(r: Rexp) = new {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  def | (s: Rexp) = ALT(r, s)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  def % = STAR(r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  def ~ (s: Rexp) = SEQ(r, s)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
implicit def stringOps(s: String) = new {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  def | (r: Rexp) = ALT(s, r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  def | (r: String) = ALT(s, r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  def % = STAR(s)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  def ~ (r: Rexp) = SEQ(s, r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  def ~ (r: String) = SEQ(s, r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  def $ (r: Rexp) = RECD(s, r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
    94
// string of a regular expressions - for testing purposes
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    95
def string(r: Rexp): String = r match {
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    96
  case ZERO => "0"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    97
  case ONE => "1"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    98
  case PRED(_, s) => s
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
    99
  case ALTS(rs) => rs.map(string).mkString("[", "|", "]")
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   100
  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   101
  case STAR(r) => s"{${string(r)}}*"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   102
  case RECD(x, r) => s"(${x}! ${string(r)})"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   103
}
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   104
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   105
// string of an annotated regular expressions - for testing purposes
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   106
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   107
def astring(a: ARexp): String = a match {
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   108
  case AZERO => "0"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   109
  case AONE(_) => "1"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   110
  case APRED(_, _, s) => s
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   111
  case AALTS(_, rs) => rs.map(astring).mkString("[", "|", "]")
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   112
  case ASEQ(_, r1, r2) => s"(${astring(r1)} ~ ${astring(r2)})"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   113
  case ASTAR(_, r) => s"{${astring(r)}}*"
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   114
}
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   115
 
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   116
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
//--------------------------------------------------------------------------------------------------------
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
// START OF NON-BITCODE PART
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
//
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
// nullable function: tests whether the regular 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
// expression can recognise the empty string
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
def nullable (r: Rexp) : Boolean = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  case ZERO => false
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  case ONE => true
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   126
  case PRED(_, _) => false
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  case ALTS(rs) => rs.exists(nullable)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  case STAR(_) => true
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  case RECD(_, r) => nullable(r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
// derivative of a regular expression w.r.t. a character
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
def der (c: Char, r: Rexp) : Rexp = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  case ZERO => ZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  case ONE => ZERO
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   137
  case PRED(f, _) => if (f(c)) ONE else ZERO
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  case SEQ(r1, r2) => 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
    if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
    else SEQ(der(c, r1), r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  case STAR(r) => SEQ(der(c, r), STAR(r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  case RECD(_, r1) => der(c, r1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
def flatten(v: Val) : String = v match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  case Empty => ""
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  case Chr(c) => c.toString
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  case Left(v) => flatten(v)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  case Right(v) => flatten(v)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  case Stars(vs) => vs.map(flatten).mkString
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  case Rec(_, v) => flatten(v)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
// extracts an environment from a value
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
def env(v: Val) : List[(String, String)] = v match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  case Empty => Nil
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
  case Chr(c) => Nil
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  case Left(v) => env(v)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  case Right(v) => env(v)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  case Sequ(v1, v2) => env(v1) ::: env(v2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  case Stars(vs) => vs.flatMap(env)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  case Rec(x, v) => (x, flatten(v))::env(v)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
// injection part
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
def mkeps(r: Rexp) : Val = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  case ONE => Empty
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  case ALTS(List(r1, r2)) => 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  case STAR(r) => Stars(Nil)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  case RECD(x, r) => Rec(x, mkeps(r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2))
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   186
  case (PRED(_, _), Empty) => Chr(c) 
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
// lexing without simplification
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
def lex(r: Rexp, s: List[Char]) : Val = s match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
  case c::cs => inj(r, c, lex(der(c, r), cs))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
//println(lexing(("ab" | "ab") ~ ("b" | ONE), "ab"))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
// some "rectification" functions for simplification
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
def F_ID(v: Val): Val = v
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
  case Right(v) => Right(f2(v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
  case Left(v) => Left(f1(v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
  (v:Val) => Sequ(f1(Empty), f2(v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
  (v:Val) => Sequ(f1(v), f2(Empty))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
def F_RECD(f: Val => Val) = (v:Val) => v match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
  case Rec(x, v) => Rec(x, f(v))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
def F_ERROR(v: Val): Val = throw new Exception("error")
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
// simplification of regular expressions returning also an
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
// rectification function; no simplification under STAR 
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   222
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   223
var profile_simp : Int = 0
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   224
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   225
def simp(r: Rexp): (Rexp, Val => Val) = {
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   226
  profile_simp =  profile_simp + 1
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   227
  r match {
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
  case ALTS(List(r1, r2)) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
    val (r1s, f1s) = simp(r1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
    val (r2s, f2s) = simp(r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
    (r1s, r2s) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
      case (_, ZERO) => (r1s, F_LEFT(f1s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
                else (ALTS(List(r1s, r2s)), F_ALT(f1s, f2s)) 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
    }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  case SEQ(r1, r2) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
    val (r1s, f1s) = simp(r1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
    val (r2s, f2s) = simp(r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
    (r1s, r2s) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
      case (ZERO, _) => (ZERO, F_ERROR)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
      case (_, ZERO) => (ZERO, F_ERROR)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
    }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  case RECD(x, r1) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
    val (r1s, f1s) = simp(r1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
    (RECD(x, r1s), F_RECD(f1s))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  case r => (r, F_ID)
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   254
}}
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   256
def ders_simp(s: List[Char], r: Rexp) : Rexp = s match {
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   257
  case Nil => r
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   258
  case c::s => ders_simp(s, simp(der(c, r))._1)
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   259
}
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   260
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   261
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
  case c::cs => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
    val (r_simp, f_simp) = simp(der(c, r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab"))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
def tokenise_simp(r: Rexp, s: String) = env(lexing_simp(r, s)).map(esc2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
//--------------------------------------------------------------------------------------------------------
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
// BITCODED PART
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
  case AZERO => AZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  case AONE(cs) => AONE(bs ++ cs)
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   284
  case APRED(cs, f, s) => APRED(bs ++ cs, f, s)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
  case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
// translation into ARexps
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
def internalise(r: Rexp) : ARexp = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
  case ZERO => AZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  case ONE => AONE(Nil)
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   294
  case PRED(f, s) => APRED(Nil, f, s)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
  case ALTS(List(r1, r2)) => 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
    AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  case ALTS(r1::rs) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
     val AALTS(Nil, rs2) = internalise(ALTS(rs))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
     AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
  case STAR(r) => ASTAR(Nil, internalise(r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
  case RECD(x, r) => internalise(r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
internalise(("a" | "ab") ~ ("b" | ""))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
// decoding of values from bit sequences
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
  case (ONE, bs) => (Empty, bs)
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   311
  case (PRED(f, _), C(c)::bs) => (Chr(c), bs)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  case (ALTS(r::Nil), bs) => decode_aux(r, bs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  case (ALTS(rs), bs) => bs match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
    case Z::bs1 => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
      val (v, bs2) = decode_aux(rs.head, bs1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
      (Left(v), bs2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
    }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
    case S::bs1 => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
      val (v, bs2) = decode_aux(ALTS(rs.tail), bs1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
      (Right(v), bs2)			
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
    }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
  case (SEQ(r1, r2), bs) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
    val (v1, bs1) = decode_aux(r1, bs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
    val (v2, bs2) = decode_aux(r2, bs1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
    (Sequ(v1, v2), bs2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
  case (STAR(r1), S::bs) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
    val (v, bs1) = decode_aux(r1, bs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
    val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
    (Stars(v::vs), bs2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
  case (STAR(_), Z::bs) => (Stars(Nil), bs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  case (RECD(x, r1), bs) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
    val (v, bs1) = decode_aux(r1, bs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
    (Rec(x, v), bs1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
  case (v, Nil) => v
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
  case _ => throw new Exception("Not decodable")
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
//erase function: extracts a Rexp from Arexp
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
def erase(r: ARexp) : Rexp = r match{
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
  case AZERO => ZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
  case AONE(_) => ONE
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   350
  case APRED(bs, f, s) => PRED(f, s)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
  case AALTS(bs, rs) => ALTS(rs.map(erase(_)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
  case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  case ASTAR(cs, r)=> STAR(erase(r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
// bnullable function: tests whether the aregular 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
// expression can recognise the empty string
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
def bnullable (r: ARexp) : Boolean = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
  case AZERO => false
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  case AONE(_) => true
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   362
  case APRED(_,_,_) => false
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
  case AALTS(_, rs) => rs.exists(bnullable)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
  case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  case ASTAR(_, _) => true
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
def bmkeps(r: ARexp) : Bits = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  case AONE(bs) => bs
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
  case AALTS(bs, rs) => {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
    val n = rs.indexWhere(bnullable)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
    bs ++ bmkeps(rs(n))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
  case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
  case ASTAR(bs, r) => bs ++ List(Z)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
// derivative of a regular expression w.r.t. a character
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
def bder(c: Char, r: ARexp) : ARexp = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
  case AZERO => AZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
  case AONE(_) => AZERO
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   382
  case APRED(bs, f, _) => if (f(c)) AONE(bs:::List(C(c))) else AZERO
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
  case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
  case ASEQ(bs, r1, r2) => 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
    if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
    else ASEQ(bs, bder(c, r1), r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
// derivative w.r.t. a string (iterates bder)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
@tailrec
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
def bders (s: List[Char], r: ARexp) : ARexp = s match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
  case Nil => r
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
  case c::s => bders(s, bder(c, r))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   399
def inc_profile(m: Map[ARexp, Int], a: ARexp) : Map[ARexp, Int] = {
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   400
  val current =  m.getOrElse(a, 0) 
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   401
  m + (a -> (current + 1))
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   402
}
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   403
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   404
var profile_bsimp_args : Map[ARexp, Int] = Map()
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   405
var profile_bsimp : Int = 0
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   406
var profile_flats : Int = 0
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   407
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   408
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   409
def flats(rs: List[ARexp]): List[ARexp] = {
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   410
  profile_flats = profile_flats + 1
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   411
  rs match {
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
    case Nil => Nil
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
    case AZERO :: rs1 => flats(rs1)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
    case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
    case r1 :: rs2 => r1 :: flats(rs2)
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   416
}}
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   418
def bsimp(r: ARexp): ARexp = {
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   419
  //profile_bsimp = inc_profile(profile_bsimp, r)
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   420
  profile_bsimp = profile_bsimp + 1
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   421
  r match {
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
  case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
      case (AZERO, _) => AZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
      case (_, AZERO) => AZERO
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
  }
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
  case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp)), erase) match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
    case Nil => AZERO
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   430
    case r :: Nil => fuse(bs1, r)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
    case rs => AALTS(bs1, rs)  
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
  }
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   433
  //case ASTAR(bs1, r1) => ASTAR(bs1, bsimp(r1))
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  case r => r
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   435
  }
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   438
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   439
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   440
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
def bders_simp (s: List[Char], r: ARexp) : ARexp = s match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
  case Nil => r
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
  case c::s => bders_simp(s, bsimp(bder(c, r)))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
  case Nil => if (bnullable(r)) bmkeps(r)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
	      else throw new Exception("Not matched")
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
  case c::cs => blex_simp(bsimp(bder(c, r)), cs)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
def blexing_simp(r: Rexp, s: String) : Val = 
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   454
 decode(r, blex_simp(internalise(r), s.toList))
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   455
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
def btokenise_simp(r: Rexp, s: String) = env(blexing_simp(r, s)).map(esc2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   461
// INCLUDING SIMPLIFICATION UNDER STARS
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   462
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   463
def bsimp_full(r: ARexp): ARexp = r match {
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   464
  case ASEQ(bs1, r1, r2) => (bsimp_full(r1), bsimp_full(r2)) match {
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   465
      case (AZERO, _) => AZERO
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   466
      case (_, AZERO) => AZERO
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   467
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   468
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   469
  }
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   470
  case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp_full)), erase) match {
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   471
    case Nil => AZERO
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   472
    case r :: Nil => fuse(bs1, r)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   473
    case rs => AALTS(bs1, rs)  
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   474
  }
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   475
  case ASTAR(bs1, r1) => ASTAR(bs1, bsimp_full(r1))
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   476
  case r => r
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   477
}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   478
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   479
def bders_simp_full(s: List[Char], r: ARexp) : ARexp = s match {
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   480
  case Nil => r
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   481
  case c::s => bders_simp_full(s, bsimp_full(bder(c, r)))
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   482
}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   483
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   484
def blex_simp_full(r: ARexp, s: List[Char]) : Bits = s match {
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   485
  case Nil => if (bnullable(r)) bmkeps(r)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   486
	      else throw new Exception("Not matched")
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   487
  case c::cs => blex_simp_full(bsimp_full(bder(c, r)), cs)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   488
}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   489
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   490
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   491
def blexing_simp_full(r: Rexp, s: String) : Val = 
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   492
 decode(r, blex_simp_full(internalise(r), s.toList))
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   493
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   494
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   495
def btokenise_simp_full(r: Rexp, s: String) = env(blexing_simp_full(r, s)).map(esc2)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   496
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   497
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   498
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
//   Testing
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
//============
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
def time[T](code: => T) = {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
  val start = System.nanoTime()
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
  val result = code
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
  val end = System.nanoTime()
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
  ((end - start)/1.0e9).toString
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
  //result
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   510
def timeR[T](code: => T) = {
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   511
  val start = System.nanoTime()
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   512
  for (i <- 1 to 10) code
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   513
  val result = code
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   514
  val end = System.nanoTime()
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   515
  (result, (end - start))
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   516
}
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   517
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
//size: of a Aregx for testing purposes 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
def size(r: Rexp) : Int = r match {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
  case ZERO => 1
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
  case ONE => 1
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   522
  case PRED(_,_) => 1
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
  case ALTS(rs) => 1 + rs.map(size).sum
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
  case STAR(r) => 1 + size(r)
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   526
  case RECD(_, r) => size(r)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
}
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
def asize(a: ARexp) = size(erase(a))
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
// Lexing Rules for a Small While Language
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
//symbols
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   535
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_), "SYM")
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
//digits
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   537
val DIGIT = PRED("0123456789".contains(_), "NUM")
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
//identifiers
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
val ID = SYM ~ (SYM | DIGIT).% 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
//numbers
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
val NUM = STAR(DIGIT)
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
//keywords
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
//semicolons
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
val SEMI: Rexp = ";"
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
//operators
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
//whitespaces
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
val WHITESPACE = PLUS(" " | "\n" | "\t")
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
//parentheses
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
val RPAREN: Rexp = ")"
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
val LPAREN: Rexp = "("
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
val BEGIN: Rexp = "{"
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
val END: Rexp = "}"
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
//strings...but probably needs not
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
val WHILE_REGS = (("k" $ KEYWORD) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
                  ("i" $ ID) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
                  ("o" $ OP) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
                  ("n" $ NUM) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
                  ("s" $ SEMI) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
                  ("str" $ STRING) |
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
                  ("p" $ (LPAREN | RPAREN)) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
                  ("b" $ (BEGIN | END)) | 
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
                  ("w" $ WHITESPACE)).%
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
// Bigger Tests
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
//==============
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
val fib_prog = """
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
write "Fib";
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
read n;
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
minus1 := 0;
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
minus2 := 1;
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
while n > 0 do {
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
  temp := minus2;
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
  minus2 := minus1 + minus2;
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
  minus1 := temp;
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
  n := n - 1
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
};
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
write "Result";
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
write minus2
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
"""
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   590
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   592
profile_simp = 0
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   593
profile_bsimp = 0
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   594
profile_flats = 0
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   595
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   596
println("Original " + size(WHILE_REGS))
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   597
println("Size Bit  " + asize(bders_simp((fib_prog * 20).toList, internalise(WHILE_REGS))) + "  " + profile_bsimp +  " + " + profile_flats)
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   598
println("Size Old  " + size(ders_simp((fib_prog * 20).toList, WHILE_REGS)) + "  " + profile_simp)
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
305
6e2cef17a9b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 300
diff changeset
   600
System.exit(0)
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   601
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   602
println("Internal sizes test OK or strange")
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   603
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   604
def perc(p1: Double, p2: Double) : String =
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   605
  f"${(((p1 - p2) / p2) * 100.0) }%5.0f" + "%"
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   606
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   607
def ders_test(n: Int, s: List[Char], r: Rexp, a: ARexp) : (Rexp, ARexp) = s match {
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   608
  case Nil => (r, a)
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   609
  case c::s => {
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   610
    // derivative 
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   611
    val (rd1, tr1) = timeR(der(c, r))
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   612
    val (ad1, ta1) = timeR(bder(c, a))
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   613
    val trs1 = f"${tr1}%.5f"
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   614
    val tas1 = f"${ta1}%.5f"
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   615
    if (tr1 < ta1) println(s"Time strange der  (step) ${n} ${perc(ta1, tr1)} sizes  der ${size(rd1)} ${asize(ad1)}")
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   616
    //simplification
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   617
    val (rd, tr) = timeR(simp(rd1)._1)
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   618
    val (ad, ta) = timeR(bsimp(ad1))
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   619
    val trs = f"${tr}%.5f"
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   620
    val tas = f"${ta}%.5f"
300
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   621
    //full simplification
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   622
    val (adf, taf) = timeR(bsimp_full(ad1))
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   623
    if (tr < ta) println(s"Time strange simp (step) ${n} ${perc(ta, tr)} sizes simp ${size(rd)} ${asize(ad)}")
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   624
    if (n == 1749 || n == 1734) {
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   625
      println{s"Aregex before bder (size: ${asize(a)})\n ${string(erase(a))}"}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   626
      println{s"Aregex after bder (size: ${asize(ad1)})\n ${string(erase(ad1))}"}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   627
      println{s"Aregex after bsimp (size: ${asize(ad)})\n ${string(erase(ad))}"}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   628
      println{s"Aregex after bsimp_full (size: ${asize(adf)})\n ${string(erase(adf))}"}
b7987014fed8 updated
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   629
    }
299
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   630
    ders_test(n + 1, s, rd, ad)
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   631
  }
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   632
}
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   633
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   634
val prg = (fib_prog * 10).toList
cae7eab03018 added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents: 298
diff changeset
   635
ders_test(0, prg, WHILE_REGS, internalise(WHILE_REGS))
298
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
db329a4d2bc0 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637