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