progs/scala/re-annotated2.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 22 Feb 2021 03:22:26 +0000
changeset 360 e752d84225ec
child 361 8bb064045b4e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// uses bitcode sequences and annotated regular 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// for basic regular expressions and RECD
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// the main difference is that it includes
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
// also character sets
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// uses a non-standard extraction method for generating
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// tokens (this is tail-recursive) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
// can match 200 copies of the fib-program (size 35000)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
// in about 20 secs
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
import scala.language.implicitConversions    
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
import scala.language.reflectiveCalls
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
import scala.annotation.tailrec   
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// standard regular expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
abstract class Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case object ZERO extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case object ONE extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class STAR(r: Rexp) extends Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class RECD(x: String, r: Rexp) extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class CHARSET(f: Char => Boolean) extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
//abbreviation for single characters
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
def CHAR(c: Char) = CHARSET(_ == c)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
// bit-codes with additional information about 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
// matched characters
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
abstract class Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
case object Z extends Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
case object S extends Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
case class C(c: Char) extends Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
type Bits = List[Bit]
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
// annotated regular expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
abstract class ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
case object AZERO extends ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
case class AONE(bs: Bits) extends ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
case class ACHARSET(bs: Bits, f: Char => Boolean) extends ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
// an abbreviation for binary alternatives
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
abstract class Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
case object Empty extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
case class Chr(c: Char) extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
case class Sequ(v1: Val, v2: Val) extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
case class Left(v: Val) extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
case class Right(v: Val) extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
case class Stars(vs: List[Val]) extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
case class Recd(x: String, v: Val) extends Val
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
   
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
// some convenience for typing in regular expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
def charlist2rexp(s: List[Char]): Rexp = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  case Nil => ONE
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case c::Nil => CHAR(c)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
implicit def string2rexp(s: String) : Rexp = charlist2rexp(s.toList)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
implicit def RexpOps(r: Rexp) = new {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  def | (s: Rexp) = ALT(r, s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  def % = STAR(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  def ~ (s: Rexp) = SEQ(r, s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
implicit def stringOps(s: String) = new {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  def | (r: Rexp) = ALT(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  def | (r: String) = ALT(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  def % = STAR(s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  def ~ (r: Rexp) = SEQ(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  def ~ (r: String) = SEQ(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  def $ (r: Rexp) = RECD(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
def size(r: Rexp) : Int = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  case ZERO => 1
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
  case ONE => 1
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
  case STAR(r) => 1 + size(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  case RECD(_, r) => 1 + size(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  case CHARSET(_) => 1
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
// Bitcoded + Annotation
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
//=======================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
//erase function: extracts the Rexp from ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
def erase(r:ARexp): Rexp = r match{
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  case AZERO => ZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case AONE(_) => ONE
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case AALTS(bs, Nil) => ZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case AALTS(bs, r::Nil) => erase(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  case AALTS(bs, r::rs) => ALT(erase(r), erase(AALTS(bs, rs)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case ASTAR(cs, r)=> STAR(erase(r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  case ACHARSET(bs, f) => CHARSET(f)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  case AZERO => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
  case AONE(cs) => AONE(bs ++ cs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case ACHARSET(cs, f) => ACHARSET(bs ++ cs, f)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
def internalise(r: Rexp) : ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  case ZERO => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  case ONE => AONE(Nil)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  case ALT(r1, r2) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
    AALT(Nil, fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
  case STAR(r) => ASTAR(Nil, internalise(r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case RECD(_, r) => internalise(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case CHARSET(f) => ACHARSET(Nil, f)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
// example
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
// internalise(("a" | "ab") ~ ("b" | ""))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
// decoding of a value from a bitsequence
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
// (this is not tail-recursive and therefore a potential bottleneck)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
def vdecode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
  case (ONE, bs) => (Empty, bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  case (ALT(r1, r2), Z::bs) => {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
    val (v, bs1) = vdecode_aux(r1, bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
    (Left(v), bs1)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
  case (ALT(r1, r2), S::bs) => {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
    val (v, bs1) = vdecode_aux(r2, bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
    (Right(v), bs1)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  case (SEQ(r1, r2), bs) => {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
    val (v1, bs1) = vdecode_aux(r1, bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
    val (v2, bs2) = vdecode_aux(r2, bs1)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
    (Sequ(v1, v2), bs2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
  case (STAR(r1), Z::bs) => {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
    val (v, bs1) = vdecode_aux(r1, bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
    val (Stars(vs), bs2) = vdecode_aux(STAR(r1), bs1)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
    (Stars(v::vs), bs2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  case (STAR(_), S::bs) => (Stars(Nil), bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
  case (RECD(s, r1), bs) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
    val (v, bs1) = vdecode_aux(r1, bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
    (Recd(s, v), bs1)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
  case (CHARSET(_), C(c)::bs) => (Chr(c), bs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
def vdecode(r: Rexp, bs: Bits) = vdecode_aux(r, bs) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
  case (v, Nil) => v
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
  case _ => throw new Exception("Not decodable")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
// decoding of sequence of string tokens from a bitsequence
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
// tail-recursive version using an accumulator (alternative for 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
// vdecode)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
@tailrec
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
def sdecode_aux(rs: List[Rexp], bs: Bits, acc: List[String]) : List[String] = (rs, bs) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  case (Nil, _) => acc
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  case (_, Nil) => acc
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
  case (ONE::rest, bs) => sdecode_aux(rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  case (ALT(r1, r2)::rest, Z::bs) => sdecode_aux(r1::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
  case (ALT(r1, r2)::rest, S::bs) => sdecode_aux(r2::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
  case (SEQ(r1, r2)::rest, bs) => sdecode_aux(r1::r2::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
  case (STAR(r1)::rest, Z::bs) => sdecode_aux(r1::STAR(r1)::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
  case (STAR(_)::rest, S::bs) => sdecode_aux(rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
  case (RECD(s, r1)::rest, bs) => sdecode_aux(r1::rest, bs, s"$s:"::acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
  case (CHARSET(_)::rest, C(c)::bs) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
    sdecode_aux(rest, bs, (acc.head :+ c)::acc.tail)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
def sdecode(r: Rexp, bs: Bits) : List[String] = 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  sdecode_aux(List(r), bs, List("")).reverse.tail
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
// nullable function: tests whether the a (annotated) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
// regular expression can recognise the empty string
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
def bnullable (r: ARexp) : Boolean = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  case AZERO => false
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  case AONE(_) => true
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
  case AALTS(_, rs) => rs.exists(bnullable)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  case ASTAR(_, _) => true
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
  case ACHARSET(_, _) => false
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
def bmkeps(r: ARexp) : Bits = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
  case AONE(bs) => bs
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
  case AALTS(bs, r::Nil) => bs ++ bmkeps(r) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
  case AALTS(bs, r::rs) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
    if (bnullable(r)) bs ++ bmkeps(r) else bmkeps(AALTS(bs, rs))  
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
  case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
  case ASTAR(bs, r) => bs ++ List(S)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
  case _ => throw new Exception("trying to bmkeps from non-nullable regex")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
// derivative of a regular expression w.r.t. a character
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
def bder(c: Char, r: ARexp) : ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
  case AZERO => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
  case AONE(_) => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
  case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
  case ASEQ(bs, r1, r2) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
    if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
    else ASEQ(bs, bder(c, r1), r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(Z), bder(c, r)), ASTAR(Nil, r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  case ACHARSET(bs, f) => if(f(c)) AONE(bs :+ C(c)) else AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
// derivative w.r.t. a string (iterates bder)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
@tailrec
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
def bders (s: List[Char], r: ARexp) : ARexp = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
  case Nil => r
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  case c::s => bders(s, bder(c, r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
// main unsimplified lexing function (produces a bitsequence)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
def blex(r: ARexp, s: List[Char]) : Bits = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
  case Nil => if (bnullable(r)) bmkeps(r) else throw new Exception("Not matched")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
  case c::cs => blex(bder(c, r), cs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
// calls blex and decodes the value
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
def blexing(r: Rexp, s: String) : Val = 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
  vdecode(r, blex(internalise(r), s.toList))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
// example by Tudor
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
//val reg = (STAR("a") ~ ("b" | "c")).%
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
//println(blexing(reg, "aab"))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
//=======================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
// simplification 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
def flts(rs: List[ARexp]) : List[ARexp] = rs match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
  case Nil => Nil
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
  case AZERO :: rs => flts(rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
  case AALTS(bs, rs1) :: rs => rs1.map(fuse(bs, _)) ++ flts(rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
  case r1 :: rs => r1 :: flts(rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
def bsimp(r: ARexp): ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
  case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
      case (AZERO, _) => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
      case (_, AZERO) => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
      // needed in order to keep the size down
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
      case (AALTS(bs, rs), r2) => AALTS(bs1 ++ bs, rs.map(ASEQ(Nil, _, r2)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
  // distinctBy deletes copies of the same  "erased" regex
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
  case AALTS(bs1, rs) => (flts(rs.map(bsimp))).distinctBy(erase) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
      case Nil => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
      case r::Nil => fuse(bs1, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
      case rs => AALTS(bs1, rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
  case r => r
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
  case Nil => if (bnullable(r)) bmkeps(r) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
              else throw new Exception("Not matched")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
  case c::cs => blex_simp(bsimp(bder(c, r)), cs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   286
// blexing_simp decodes a value from the bitsequence (potentially slow)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
// blexing2_simp decodes a string-list from the bitsequence
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
def blexing_simp(r: Rexp, s: String) : Val = 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   289
  vdecode(r, blex_simp(internalise(r), s.toList))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   290
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   291
def blexing2_simp(r: Rexp, s: String) : List[String] = 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
  sdecode(r, blex_simp(internalise(r), s.toList))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   293
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
//println(blexing_simp(reg, "aab"))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   296
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   297
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   298
// extracts a string from value
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   299
def flatten(v: Val) : String = v match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   300
  case Empty => ""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   301
  case Chr(c) => c.toString
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   302
  case Left(v) => flatten(v)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   303
  case Right(v) => flatten(v)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   304
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   305
  case Stars(vs) => vs.map(flatten).mkString
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   306
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   307
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   308
// extracts an environment from a value
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   309
def env(v: Val) : List[(String, String)] = v match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   310
  case Empty => Nil
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   311
  case Chr(c) => Nil
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   312
  case Left(v) => env(v)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   313
  case Right(v) => env(v)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   314
  case Sequ(v1, v2) => env(v1) ::: env(v2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   315
  case Stars(vs) => vs.flatMap(env)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   316
  case Recd(x, v) => (x, flatten(v))::env(v)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   317
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   318
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   319
def bsize(a: ARexp) = size(erase(a))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   320
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   321
// Some Tests
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   322
//============
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   323
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   324
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   325
def time_needed[T](i: Int, code: => T) = {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   326
  val start = System.nanoTime()
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   327
  for (j <- 1 to i) code
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   328
  val end = System.nanoTime()
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   329
  (end - start)/(i * 1.0e9)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   330
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   331
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   332
val evil1 = STAR(STAR("a")) ~ "b"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   333
val evil2 = STAR(STAR(STAR("a"))) ~ "b"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   334
val evil3 = STAR("aa" | "a")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   335
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   336
/*
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   337
println("evil1")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   338
for(i <- 0 to 10000 by 1000) {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   339
    println(time_needed(1, blexing2_simp(evil1, "a"*i ++ "b")))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   340
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   341
*/
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   342
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   343
/*
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   344
println("evil2")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   345
for(i <- 0 to 10000 by 1000) {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   346
    println(time_needed(1, blexing2_simp(evil2, "a"*i ++ "b")))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   347
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   348
*/
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   349
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   350
/*
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   351
println("evil3")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   352
for(i <- 0 to 10000 by 1000) {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   353
    println(time_needed(1, blexing2_simp(evil3, "a"*i)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   354
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   355
*/
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   356
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   357
// WHILE LANGUAGE
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   358
//================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   359
def PLUS(r: Rexp) = r ~ r.%
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   360
def RANGE(s: String) = CHARSET(s.toSet)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   361
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   362
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   363
val DIGIT = RANGE("0123456789")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   364
val ID = SYM ~ (SYM | DIGIT).% 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   365
val NUM = PLUS(DIGIT)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   366
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   367
val SEMI: Rexp = ";"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   368
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   369
val WHITESPACE = PLUS(" " | "\n" | "\t")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   370
val RPAREN: Rexp = "{"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   371
val LPAREN: Rexp = "}"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   372
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   373
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   374
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   375
val WHILE_REGS = (("k" $ KEYWORD) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   376
                  ("i" $ ID) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   377
                  ("o" $ OP) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   378
                  ("n" $ NUM) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   379
                  ("s" $ SEMI) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   380
                  ("str" $ STRING) |
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   381
                  ("p" $ (LPAREN | RPAREN)) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   382
                  ("w" $ WHITESPACE)).%
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   383
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   384
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   385
// Some Simple While Tests
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   386
//========================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   387
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   388
val prog0 = """read n"""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   389
println(s"test: $prog0")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   390
println(env(blexing_simp(WHILE_REGS, prog0)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   391
println(blexing2_simp(WHILE_REGS, prog0))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   392
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   393
val prog1 = """read  n; write n"""  
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   394
println(s"test: $prog1")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   395
println(env(blexing_simp(WHILE_REGS, prog1)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   396
println(blexing2_simp(WHILE_REGS, prog1))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   397
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   398
val prog2 = """
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   399
write "Fib";
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   400
read n;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   401
minus1 := 0;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   402
minus2 := 1;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   403
while n > 0 do {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   404
  temp := minus2;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   405
  minus2 := minus1 + minus2;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   406
  minus1 := temp;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   407
  n := n - 1
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   408
};
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   409
write "Result";
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   410
write minus2
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   411
"""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   412
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   413
println("lexing fib program (once)")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   414
println(blexing2_simp(WHILE_REGS, prog2).filter(s => s == "" || !s.startsWith("w")))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   415
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   416
val n = 200
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   417
println(s"lexing fib program ($n times, size ${prog2.length * n})")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   418
println(time_needed(1, blexing2_simp(WHILE_REGS, prog2 * n)))