exps/bit.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 13 Mar 2019 12:31:03 +0000
changeset 315 ab7fe342e004
parent 314 20a57552d722
child 316 0eaa1851a5b6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.language.implicitConversions    
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import scala.language.reflectiveCalls
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import scala.annotation.tailrec   
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.util.Try
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// for escaping strings
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def escape(raw: String) : String = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  import scala.reflect.runtime.universe._
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  Literal(Constant(raw)).toString
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def esc2(r: (String, String)) = (escape(r._1), escape(r._2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case Nil => Nil
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case (x::xs) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    val res = f(x)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    if (acc.contains(res)) distinctBy(xs, f, acc)  
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
    else x::distinctBy(xs, f, res::acc)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
} 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
abstract class Bit
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object Z extends Bit
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case object S extends Bit
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class C(c: Char) extends Bit
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
type Bits = List[Bit]
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// usual regular expressions
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
abstract class Rexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case object ZERO extends Rexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
case object ONE extends Rexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
case class PRED(f: Char => Boolean, s: String = "_") extends Rexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
case class ALTS(rs: List[Rexp]) extends Rexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
case class STAR(r: Rexp) extends Rexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
case class RECD(x: String, r: Rexp) extends Rexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// abbreviations
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
def CHAR(c: Char) = PRED(_ == c, c.toString)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
def PLUS(r: Rexp) = SEQ(r, STAR(r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
val ANYCHAR = PRED(_ => true, ".")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// annotated regular expressions
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
abstract class ARexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
case object AZERO extends ARexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
case class AONE(bs: Bits) extends ARexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
case class APRED(bs: Bits, f: Char => Boolean, s: String = "_") extends ARexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// abbreviations
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// values
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
abstract class Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
case object Empty extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
case class Chr(c: Char) extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
case class Sequ(v1: Val, v2: Val) extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
case class Left(v: Val) extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
case class Right(v: Val) extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
case class Stars(vs: List[Val]) extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
case class Rec(x: String, v: Val) extends Val
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
   
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// some convenience for typing in regular expressions
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
def charlist2rexp(s : List[Char]): Rexp = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case Nil => ONE
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  case c::Nil => CHAR(c)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
implicit def RexpOps(r: Rexp) = new {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  def | (s: Rexp) = ALT(r, s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  def % = STAR(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  def ~ (s: Rexp) = SEQ(r, s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
implicit def stringOps(s: String) = new {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  def | (r: Rexp) = ALT(s, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  def | (r: String) = ALT(s, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  def % = STAR(s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  def ~ (r: Rexp) = SEQ(s, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  def ~ (r: String) = SEQ(s, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  def $ (r: Rexp) = RECD(s, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// string of a regular expressions - for testing purposes
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
def string(r: Rexp): String = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  case ZERO => "0"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  case ONE => "1"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case PRED(_, s) => s
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  case ALTS(rs) => rs.map(string).mkString("[", "|", "]")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case STAR(r) => s"{${string(r)}}*"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  case RECD(x, r) => s"(${x}! ${string(r)})"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
315
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   107
def strings(rs: Set[Rexp]): String =
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   108
  rs.map(string).mkString("{", "|", "}")
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   109
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
// string of an annotated regular expressions - for testing purposes
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
def astring(a: ARexp): String = a match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  case AZERO => "0"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case AONE(_) => "1"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  case APRED(_, _, s) => s
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  case AALTS(_, rs) => rs.map(astring).mkString("[", "|", "]")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  case ASEQ(_, r1, r2) => s"(${astring(r1)} ~ ${astring(r2)})"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  case ASTAR(_, r) => s"{${astring(r)}}*"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
//--------------------------------------------------------------
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
// START OF NON-BITCODE PART
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
//
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
// nullable function: tests whether the regular 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
// expression can recognise the empty string
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
def nullable(r: Rexp) : Boolean = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  case ZERO => false
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  case ONE => true
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  case PRED(_, _) => false
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  case ALTS(rs) => rs.exists(nullable)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
  case STAR(_) => true
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  case RECD(_, r) => nullable(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
// derivative of a regular expression w.r.t. a character
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
def der(c: Char, r: Rexp) : Rexp = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  case ZERO => ZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  case ONE => ZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  case PRED(f, _) => if (f(c)) ONE else ZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  case SEQ(r1, r2) => 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
    else SEQ(der(c, r1), r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
  case STAR(r) => SEQ(der(c, r), STAR(r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  case RECD(_, r1) => der(c, r1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
def flatten(v: Val) : String = v match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  case Empty => ""
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  case Chr(c) => c.toString
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  case Left(v) => flatten(v)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  case Right(v) => flatten(v)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
  case Stars(vs) => vs.map(flatten).mkString
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  case Rec(_, v) => flatten(v)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
// extracts an environment from a value
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
def env(v: Val) : List[(String, String)] = v match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  case Empty => Nil
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  case Chr(c) => Nil
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  case Left(v) => env(v)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  case Right(v) => env(v)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  case Sequ(v1, v2) => env(v1) ::: env(v2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  case Stars(vs) => vs.flatMap(env)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  case Rec(x, v) => (x, flatten(v))::env(v)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
// injection part
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
def mkeps(r: Rexp) : Val = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  case ONE => Empty
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  case ALTS(List(r1, r2)) => 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  case STAR(r) => Stars(Nil)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  case RECD(x, r) => Rec(x, mkeps(r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  case (PRED(_, _), Empty) => Chr(c) 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
// lexing without simplification
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
def lex(r: Rexp, s: List[Char]) : Val = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
  case c::cs => inj(r, c, lex(der(c, r), cs))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
//println(lexing(("ab" | "ab") ~ ("b" | ONE), "ab"))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
// some "rectification" functions for simplification
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
def F_ID(v: Val): Val = v
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  case Right(v) => Right(f2(v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  case Left(v) => Left(f1(v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
  (v:Val) => Sequ(f1(Empty), f2(v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
  (v:Val) => Sequ(f1(v), f2(Empty))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
def F_RECD(f: Val => Val) = (v:Val) => v match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
  case Rec(x, v) => Rec(x, f(v))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
def F_ERROR(v: Val): Val = throw new Exception("error")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
// simplification of regular expressions returning also an
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
// rectification function; no simplification under STAR 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
def simp(r: Rexp): (Rexp, Val => Val) = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
  case ALTS(List(r1, r2)) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
    val (r1s, f1s) = simp(r1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
    val (r2s, f2s) = simp(r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
    (r1s, r2s) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
      case (_, ZERO) => (r1s, F_LEFT(f1s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
                else (ALTS(List(r1s, r2s)), F_ALT(f1s, f2s)) 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  case SEQ(r1, r2) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
    val (r1s, f1s) = simp(r1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
    val (r2s, f2s) = simp(r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
    (r1s, r2s) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
      case (ZERO, _) => (ZERO, F_ERROR)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
      //case (_, ZERO) => (ZERO, F_ERROR)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
      //case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  case RECD(x, r1) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
    val (r1s, f1s) = simp(r1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
    (RECD(x, r1s), F_RECD(f1s))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  case r => (r, F_ID)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
def ders_simp(s: List[Char], r: Rexp) : Rexp = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
  case Nil => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  case c::s => ders_simp(s, simp(der(c, r))._1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
  case c::cs => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
    val (r_simp, f_simp) = simp(der(c, r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
//println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab"))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
def tokenise_simp(r: Rexp, s: String) = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
  env(lexing_simp(r, s)).map(esc2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
//--------------------------------------------------------------------
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
// Partial Derivatives
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
def pder(c: Char, r: Rexp): Set[Rexp] = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
  case ZERO => Set()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
  case ONE => Set()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
  case PRED(f, _) => if (f(c)) Set(ONE) else Set()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
  case ALTS(rs) => rs.toSet.flatMap(pder(c, _))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
  case SEQ(r1, r2) =>
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
    (for (pr1 <- pder(c, r1)) yield SEQ(pr1, r2)) ++
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
    (if (nullable(r1)) pder(c, r2) else Set())
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
  case STAR(r1) =>
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
    for (pr1 <- pder(c, r1)) yield SEQ(pr1, STAR(r1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
  case RECD(_, r1) => pder(c, r1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
def pders(cs: List[Char], r: Rexp): Set[Rexp] = cs match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
  case Nil => Set(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  case c::cs => pder(c, r).flatMap(pders(cs, _))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
def pders_simp(cs: List[Char], r: Rexp): Set[Rexp] = cs match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
  case Nil => Set(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
  case c::cs => pder(c, r).flatMap(pders_simp(cs, _)).map(simp(_)._1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
def psize(rs: Set[Rexp])  = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  rs.map(size).sum
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
//--------------------------------------------------------------------
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
// BITCODED PART
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  case AZERO => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
  case AONE(cs) => AONE(bs ++ cs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  case APRED(cs, f, s) => APRED(bs ++ cs, f, s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
  case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
// translation into ARexps
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
def internalise(r: Rexp) : ARexp = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
  case ZERO => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
  case ONE => AONE(Nil)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
  case PRED(f, s) => APRED(Nil, f, s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  case ALTS(List(r1, r2)) => 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
    AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
  case ALTS(r1::rs) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
     val AALTS(Nil, rs2) = internalise(ALTS(rs))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
     AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
  case STAR(r) => ASTAR(Nil, internalise(r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  case RECD(x, r) => internalise(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
internalise(("a" | "ab") ~ ("b" | ""))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
// decoding of values from bit sequences
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
  case (ONE, bs) => (Empty, bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
  case (PRED(f, _), C(c)::bs) => (Chr(c), bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
  case (ALTS(r::Nil), bs) => decode_aux(r, bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
  case (ALTS(rs), bs) => bs match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
    case Z::bs1 => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
      val (v, bs2) = decode_aux(rs.head, bs1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
      (Left(v), bs2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
    case S::bs1 => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
      val (v, bs2) = decode_aux(ALTS(rs.tail), bs1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
      (Right(v), bs2)			
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
  case (SEQ(r1, r2), bs) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
    val (v1, bs1) = decode_aux(r1, bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
    val (v2, bs2) = decode_aux(r2, bs1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
    (Sequ(v1, v2), bs2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
  case (STAR(r1), S::bs) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
    val (v, bs1) = decode_aux(r1, bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
    val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
    (Stars(v::vs), bs2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
  case (STAR(_), Z::bs) => (Stars(Nil), bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  case (RECD(x, r1), bs) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
    val (v, bs1) = decode_aux(r1, bs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
    (Rec(x, v), bs1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
  case (v, Nil) => v
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  case _ => throw new Exception("Not decodable")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
//erase function: extracts a Rexp from Arexp
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
def erase(r: ARexp) : Rexp = r match{
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
  case AZERO => ZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
  case AONE(_) => ONE
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
  case APRED(bs, f, s) => PRED(f, s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
  case AALTS(bs, rs) => ALTS(rs.map(erase(_)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
  case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
  case ASTAR(cs, r)=> STAR(erase(r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
// bnullable function: tests whether the aregular 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
// expression can recognise the empty string
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
def bnullable (r: ARexp) : Boolean = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
  case AZERO => false
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
  case AONE(_) => true
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
  case APRED(_,_,_) => false
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
  case AALTS(_, rs) => rs.exists(bnullable)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
  case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
  case ASTAR(_, _) => true
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
def bmkeps(r: ARexp) : Bits = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
  case AONE(bs) => bs
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
  case AALTS(bs, rs) => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
    val n = rs.indexWhere(bnullable)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
    bs ++ bmkeps(rs(n))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
  case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
  case ASTAR(bs, r) => bs ++ List(Z)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
// derivative of a regular expression w.r.t. a character
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
def bder(c: Char, r: ARexp) : ARexp = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
  case AZERO => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
  case AONE(_) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
  case APRED(bs, f, _) => if (f(c)) AONE(bs:::List(C(c))) else AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
  case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
  case ASEQ(bs, r1, r2) => 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
    if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
    else ASEQ(bs, bder(c, r1), r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
// derivative w.r.t. a string (iterates bder)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
@tailrec
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
def bders (s: List[Char], r: ARexp) : ARexp = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
  case Nil => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
  case c::s => bders(s, bder(c, r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
def flats(rs: List[ARexp]): List[ARexp] = rs match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
    case Nil => Nil
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
    case AZERO :: rs1 => flats(rs1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
    case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
    case r1 :: rs2 => r1 :: flats(rs2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
def bsimp(r: ARexp): ARexp = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
      case (AZERO, _) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
      case (_, AZERO) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
315
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   441
      //case (AALTS(bs2, rs), r2s) =>  
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   442
      //  AALTS(bs1 ++ bs2, rs.map(ASEQ(Nil, _, r2s)))
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
  case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp)), erase) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
    case Nil => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
    case r :: Nil => fuse(bs1, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
    case rs => AALTS(bs1, rs)  
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
  //case ASTAR(bs1, r1) => ASTAR(bs1, bsimp(r1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
  case r => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
def bders_simp (s: List[Char], r: ARexp) : ARexp = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
  case Nil => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
  case c::s => bders_simp(s, bsimp(bder(c, r)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
  case Nil => if (bnullable(r)) bmkeps(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
	      else throw new Exception("Not matched")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
  case c::cs => blex_simp(bsimp(bder(c, r)), cs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
def blexing_simp(r: Rexp, s: String) : Val = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
 decode(r, blex_simp(internalise(r), s.toList))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
def btokenise_simp(r: Rexp, s: String) = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
  env(blexing_simp(r, s)).map(esc2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
// INCLUDING SIMPLIFICATION UNDER STARS
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
def bsimp_full(r: ARexp): ARexp = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
  case ASEQ(bs1, r1, r2) => (bsimp_full(r1), bsimp_full(r2)) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
      case (AZERO, _) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
      case (_, AZERO) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
315
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   482
      //case (AALTS(bs2, rs), r2s) =>  
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   483
      //  AALTS(bs1 ++ bs2, rs.map(ASEQ(Nil, _, r2s)))
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
  case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp_full)), erase) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
    case Nil => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
    case r :: Nil => fuse(bs1, r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
    case rs => AALTS(bs1, rs)  
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
  case ASTAR(bs1, r1) => ASTAR(bs1, bsimp_full(r1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
  case r => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
def bders_simp_full(s: List[Char], r: ARexp) : ARexp = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
  case Nil => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
  case c::s => bders_simp_full(s, bsimp_full(bder(c, r)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
def blex_simp_full(r: ARexp, s: List[Char]) : Bits = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
  case Nil => if (bnullable(r)) bmkeps(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
	      else throw new Exception("Not matched")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
  case c::cs => blex_simp_full(bsimp_full(bder(c, r)), cs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
def blexing_simp_full(r: Rexp, s: String) : Val = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
 decode(r, blex_simp_full(internalise(r), s.toList))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
def btokenise_simp_full(r: Rexp, s: String) = env(blexing_simp_full(r, s)).map(esc2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
// bders2 for strings in the ALTS case
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
def bders2_simp(s: List[Char], a: ARexp) : ARexp = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
  //println(s"s = ${s.length}   a = ${asize(a)}")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
  //Console.readLine
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
  (s, a) match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
  case (Nil, r) => r
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
  case (s, AZERO) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
  case (s, AONE(_)) => AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
  case (s, APRED(bs, f, _)) => 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
    if (f(s.head) && s.tail == Nil) AONE(bs:::List(C(s.head))) else AZERO
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
  case (s, AALTS(bs, rs)) => bsimp(AALTS(bs, rs.map(bders2_simp(s, _)))) 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
  case (c::s, r) => bders2_simp(s, bsimp(bder(c, r)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
}}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
def blexing2_simp(r: Rexp, s: String) : Val = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
  val bder = bders2_simp(s.toList, internalise(r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
  if (bnullable(bder)) decode(r, bmkeps(bder)) else 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
  throw new Exception("Not matched")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
def btokenise2_simp(r: Rexp, s: String) = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
  env(blexing2_simp(r, s)).map(esc2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
// Parser for regexes
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
case class Parser(s: String) {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
  var i = 0
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
  
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  def peek() = s(i)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
  def eat(c: Char) = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
    if (c == s(i)) i = i + 1 else throw new Exception("Expected " + c + " got " + s(i))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
  def next() = { i = i + 1; s(i - 1) }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
  def more() = s.length - i > 0
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
  def Regex() : Rexp = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
    val t = Term();
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
    if (more() && peek() == '|') {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
      eat ('|') ; 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
      ALT(t, Regex()) 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
    } 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
    else t
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
  def Term() : Rexp = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
    var f : Rexp = 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
      if (more() && peek() != ')' && peek() != '|') Factor() else ONE;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
    while (more() && peek() != ')' && peek() != '|') {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
      f = SEQ(f, Factor()) ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
    f
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  def Factor() : Rexp = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
    var b = Base();
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
    while (more() && peek() == '*') {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
      eat('*') ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
      b = STAR(b) ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
    while (more() && peek() == '?') {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
      eat('?') ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
      b = ALT(b, ONE) ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
    while (more() && peek() == '+') {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
      eat('+') ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
      b = SEQ(b, STAR(b)) ;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
    b
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
  def Base() : Rexp = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
    peek() match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
      case '(' => { eat('(') ; val r = Regex(); eat(')') ; r }   // if groups should be groups RECD("",r) }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
      case '.' => { eat('.'); ANYCHAR }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
      case _ => CHAR(next())
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
// two simple examples for the regex parser
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
println("two simple examples for the regex parser")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
println(string(Parser("a|(bc)*").Regex()))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
println(string(Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex()))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
//System.exit(0)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
//   Testing
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
//============
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
def time[T](code: => T) = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
  val start = System.nanoTime()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
  val result = code
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
  val end = System.nanoTime()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
  ((end - start)/1.0e9).toString
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
  //result
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
def timeR[T](code: => T) = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
  val start = System.nanoTime()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
  for (i <- 1 to 10) code
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
  val result = code
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
  val end = System.nanoTime()
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
  (result, (end - start))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
//size: of a Aregx for testing purposes 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
def size(r: Rexp) : Int = r match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
  case ZERO => 1
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
  case ONE => 1
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
  case PRED(_,_) => 1
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
  case ALTS(rs) => 1 + rs.map(size).sum
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
  case STAR(r) => 1 + size(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
  case RECD(_, r) => size(r)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
def asize(a: ARexp) = size(erase(a))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
// Lexing Rules for a Small While Language
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
//symbols
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_), "SYM")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
//digits
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
val DIGIT = PRED("0123456789".contains(_), "NUM")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
//identifiers
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
val ID = SYM ~ (SYM | DIGIT).% 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
//numbers
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
val NUM = STAR(DIGIT)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
//keywords
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
//semicolons
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
val SEMI: Rexp = ";"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
//operators
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
//whitespaces
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
val WHITESPACE = PLUS(" " | "\n" | "\t")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
//parentheses
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
val RPAREN: Rexp = ")"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
val LPAREN: Rexp = "("
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
val BEGIN: Rexp = "{"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
val END: Rexp = "}"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
//strings...but probably needs not
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
val WHILE_REGS = (("k" $ KEYWORD) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
                  ("i" $ ID) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
                  ("o" $ OP) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
                  ("n" $ NUM) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   672
                  ("s" $ SEMI) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
                  ("str" $ STRING) |
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
                  ("p" $ (LPAREN | RPAREN)) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
                  ("b" $ (BEGIN | END)) | 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
                  ("w" $ WHITESPACE)).%
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
// Some Small Tests
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
//==================
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
println("Small tests")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
315
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   684
val q = STAR(STAR("bb" | ("a" | "b")))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   685
val qs = "bb"
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
println("Size Bit  " + asize(bders_simp(qs.toList, internalise(q))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
println("Size Bitf " + asize(bders_simp_full(qs.toList, internalise(q))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
println("Size Bit2 " + asize(bders2_simp(qs.toList, internalise(q))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
println("Size Old  " + size(ders_simp(qs.toList, q)))
315
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   691
println("Size Pder " + psize(pders(qs.toList, q)))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   692
println("Size Pder simp " + psize(pders_simp(qs.toList, q)))
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
315
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   694
println(astring(bders_simp(qs.toList, internalise(q))))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   695
println(astring(bders_simp_full(qs.toList, internalise(q))))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   696
println(string(ders_simp(qs.toList, q)))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   697
println(strings(pders(qs.toList, q)))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   698
println(strings(pders_simp(qs.toList, q)))
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   699
ab7fe342e004 updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   700
System.exit(0)
314
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
val re1 = STAR("a" | "aa")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
println(astring(bders_simp("".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
println(astring(bders_simp("a".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
println(astring(bders_simp("aa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
println(astring(bders_simp("aaa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
println(astring(bders_simp("aaaaaa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
println(astring(bders_simp("aaaaaaaaa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
println(astring(bders_simp("aaaaaaaaaaaa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
println(astring(bders_simp("aaaaaaaaaaaaaaaaaaaaaaaaa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
println(astring(bders_simp("aaaaaabaaaabbbbbaaaaaaaaaaaaaaa".toList, internalise(re1))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
for (i <- 0 to 100 by 5) {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
  //print("Old: " + time(tokenise_simp(re1, "a" * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
  print(" Bit: " + time(btokenise_simp(re1, "a" * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
  print(" Bit full simp: " + time(btokenise_simp_full(re1, "a" * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
  println(" Bit2: " + time(btokenise2_simp(re1, "a" * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
Console.readLine
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
// Bigger Tests
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
//==============
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
println("Big tests")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
val fib_prog = """
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
write "Fib";
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
read n;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
minus1 := 0;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
minus2 := 1;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
while n > 0 do {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
  temp := minus2;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
  minus2 := minus1 + minus2;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
  minus1 := temp;
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
  n := n - 1
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
};
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
write "Result";
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
write minus2
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
"""
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
println("fib prog tests :")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
println(tokenise_simp(WHILE_REGS, fib_prog))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
println(btokenise_simp(WHILE_REGS, fib_prog))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
println("equal? " + (tokenise_simp(WHILE_REGS, fib_prog) == btokenise_simp(WHILE_REGS, fib_prog)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
for (i <- 1 to 20) {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
  print("Old: " + time(tokenise_simp(WHILE_REGS, fib_prog * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
  print(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
  println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
  //println(" Bit2: " + time(btokenise2_simp(WHILE_REGS, fib_prog * i)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   756
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
println("Original " + size(WHILE_REGS))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
println("Size Bit  " + asize(bders_simp((fib_prog * 1).toList, internalise(WHILE_REGS))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
println("Size Bitf " + asize(bders_simp_full((fib_prog * 1).toList, internalise(WHILE_REGS))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
println("Size Bit2 " + asize(bders2_simp((fib_prog * 1).toList, internalise(WHILE_REGS))))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
println("Size Old  " + size(ders_simp((fib_prog * 1).toList, WHILE_REGS)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
println("Size Pder " + psize(pders_simp((fib_prog * 1).toList, WHILE_REGS)))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   765
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
System.exit(0)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
println("Internal sizes test OK or strange")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   770
def perc(p1: Double, p2: Double) : String =
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
  f"${(((p1 - p2) / p2) * 100.0) }%5.0f" + "%"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   773
def ders_test(n: Int, s: List[Char], r: Rexp, a: ARexp) : (Rexp, ARexp) = s match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   774
  case Nil => (r, a)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
  case c::s => {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
    // derivative 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
    val (rd1, tr1) = timeR(der(c, r))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   778
    val (ad1, ta1) = timeR(bder(c, a))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
    val trs1 = f"${tr1}%.5f"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   780
    val tas1 = f"${ta1}%.5f"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
    if (tr1 < ta1) println(s"Time strange der  (step) ${n} ${perc(ta1, tr1)} sizes  der ${size(rd1)} ${asize(ad1)}")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
    //simplification
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
    val (rd, tr) = timeR(simp(rd1)._1)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
    val (ad, ta) = timeR(bsimp(ad1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
    val trs = f"${tr}%.5f"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
    val tas = f"${ta}%.5f"
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
    //full simplification
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
    val (adf, taf) = timeR(bsimp_full(ad1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
    if (tr < ta) println(s"Time strange simp (step) ${n} ${perc(ta, tr)} sizes simp ${size(rd)} ${asize(ad)}")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
    if (n == 1749 || n == 1734) {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
      println{s"Aregex before bder (size: ${asize(a)})\n ${string(erase(a))}"}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
      println{s"Aregex after bder (size: ${asize(ad1)})\n ${string(erase(ad1))}"}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   793
      println{s"Aregex after bsimp (size: ${asize(ad)})\n ${string(erase(ad))}"}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
      println{s"Aregex after bsimp_full (size: ${asize(adf)})\n ${string(erase(adf))}"}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   795
    }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   796
    ders_test(n + 1, s, rd, ad)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   798
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
val prg = (fib_prog * 10).toList
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   801
ders_test(0, prg, WHILE_REGS, internalise(WHILE_REGS))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   802
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   803
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   804
//testing the two lexings produce the same value
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   805
//enumerates strings of length n over alphabet cs
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   806
def strs(n: Int, cs: String) : Set[String] = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   807
  if (n == 0) Set("")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   808
  else {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   809
    val ss = strs(n - 1, cs)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   810
    ss ++
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   811
    (for (s <- ss; c <- cs.toList) yield c + s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   812
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   813
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   814
def enum(n: Int, s: String) : Stream[Rexp] = n match {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   815
  case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   816
  case n => {  
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   817
    val rs = enum(n - 1, s)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   818
    rs #:::
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   819
    (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   820
    (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   821
    (for (r1 <- rs) yield STAR(r1))
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   822
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   823
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   824
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   825
//tests blexing and lexing
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   826
def tests(ss: Set[String])(r: Rexp) = {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   827
  //println(s"Testing ${r}")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   828
  for (s <- ss.par) yield {
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   829
    val res1 = Try(Some(lexing_simp(r, s))).getOrElse(None)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   830
    val res2 = Try(Some(blexing_simp(r, s))).getOrElse(None)
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   831
    if (res1 != res2) 
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   832
      { println(s"Disagree on ${r} and ${s}")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   833
	println(s"   ${res1} !=  ${res2}")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   834
	Some((r, s)) } else None
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   835
  }
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   836
}
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   837
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   838
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   839
println("Partial searching: ")
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   840
enum(2, "abc").map(tests(strs(3, "abc"))).toSet
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   841
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   842
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   843
20a57552d722 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   844