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