progs/scala/re-bit.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 20 Jul 2016 14:30:07 +0100
changeset 204 cd9e40280784
parent 159 940530087f30
child 286 804fbb227568
permissions -rw-r--r--
added paper about size derivatives
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions    
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scala.annotation.tailrec   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
abstract class Rexp 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     6
case object ZERO extends Rexp
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     7
case object ONE extends Rexp
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    12
case class RECD(x: String, r: Rexp) extends Rexp
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    13
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    14
abstract class ARexp 
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    15
case object AZERO extends ARexp
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    16
case class AONE(bs: List[Boolean]) extends ARexp
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    17
case class ACHAR(bs: List[Boolean], c: Char) extends ARexp
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    18
case class AALT(bs: List[Boolean], r1: ARexp, r2: ARexp) extends ARexp 
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    19
case class ASEQ(bs: List[Boolean], r1: ARexp, r2: ARexp) extends ARexp 
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    20
case class ASTAR(bs: List[Boolean], r: ARexp) extends ARexp 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
abstract class Val
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    23
case object Empty extends Val
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
case class Chr(c: Char) extends Val
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
case class Sequ(v1: Val, v2: Val) extends Val
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    26
case class Left(v: Val) extends Val
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    27
case class Right(v: Val) extends Val
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
case class Stars(vs: List[Val]) extends Val
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    29
case class Rec(x: String, v: Val) extends Val
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
// some convenience for typing in regular expressions
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
def charlist2rexp(s : List[Char]): Rexp = s match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    33
  case Nil => ONE
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  case c::Nil => CHAR(c)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
implicit def RexpOps(r: Rexp) = new {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def | (s: Rexp) = ALT(r, s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def % = STAR(r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def ~ (s: Rexp) = SEQ(r, s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
implicit def stringOps(s: String) = new {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  def | (r: Rexp) = ALT(s, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  def | (r: String) = ALT(s, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  def % = STAR(s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  def ~ (r: Rexp) = SEQ(s, r)
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
    50
  def ~ (r: String) = SEQ(s, r)
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    51
  def $ (r: Rexp) = RECD(s, r)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    52
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    53
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    54
// translation into ARexps
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    55
def fuse(bs: List[Boolean], r: ARexp) : ARexp = r match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    56
  case AZERO => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    57
  case AONE(cs) => AONE(bs ++ cs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    58
  case ACHAR(cs, c) => ACHAR(bs ++ cs, c)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    59
  case AALT(cs, r1, r2) => AALT(bs ++ cs, r1, r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    60
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    61
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    62
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    63
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    64
def internalise(r: Rexp) : ARexp = r match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    65
  case ZERO => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    66
  case ONE => AONE(Nil)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    67
  case CHAR(c) => ACHAR(Nil, c)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    68
  case ALT(r1, r2) => AALT(Nil, fuse(List(false), internalise(r1)), fuse(List(true), internalise(r2)))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    69
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    70
  case STAR(r) => ASTAR(Nil, internalise(r))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    71
  case RECD(x, r) => internalise(r)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    74
internalise(("a" | "ab") ~ ("b" | ""))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    75
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    76
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    77
def decode_aux(r: Rexp, bs: List[Boolean]) : (Val, List[Boolean]) = (r, bs) match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    78
  case (ONE, bs) => (Empty, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    79
  case (CHAR(c), bs) => (Chr(c), bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    80
  case (ALT(r1, r2), false::bs) => {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    81
    val (v, bs1) = decode_aux(r1, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    82
    (Left(v), bs1)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    83
  }
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    84
  case (ALT(r1, r2), true::bs) => {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    85
    val (v, bs1) = decode_aux(r2, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    86
    (Right(v), bs1)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    87
  }
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    88
  case (SEQ(r1, r2), bs) => {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    89
    val (v1, bs1) = decode_aux(r1, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    90
    val (v2, bs2) = decode_aux(r2, bs1)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    91
    (Sequ(v1, v2), bs2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    92
  }
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    93
  case (STAR(r1), false::bs) => {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    94
    val (v, bs1) = decode_aux(r1, bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    95
    val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    96
    (Stars(v::vs), bs2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    97
  }
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    98
  case (STAR(_), true::bs) => (Stars(Nil), bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    99
  case (RECD(x, r1), bs) => {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   100
    val (v, bs1) = decode_aux(r1, bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   101
    (Rec(x, v), bs1)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   102
  }
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   103
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   104
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   105
def decode(r: Rexp, bs: List[Boolean]) = decode_aux(r, bs) match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   106
  case (v, Nil) => v
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   107
  case _ => throw new Exception("Not decodable")
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   108
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   109
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   110
// nullable function: tests whether the aregular 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
// expression can recognise the empty string
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   112
def nullable (r: ARexp) : Boolean = r match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   113
  case AZERO => false
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   114
  case AONE(_) => true
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   115
  case ACHAR(_,_) => false
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   116
  case AALT(_, r1, r2) => nullable(r1) || nullable(r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   117
  case ASEQ(_, r1, r2) => nullable(r1) && nullable(r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   118
  case ASTAR(_, _) => true
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   119
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   120
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   121
def mkepsBC(r: ARexp) : List[Boolean] = r match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   122
  case AONE(bs) => bs
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   123
  case AALT(bs, r1, r2) => 
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   124
    if (nullable(r1)) bs ++ mkepsBC(r1) else bs ++ mkepsBC(r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   125
  case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   126
  case ASTAR(bs, r) => bs ++ List(true)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
// derivative of a regular expression w.r.t. a character
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   130
def der (c: Char, r: ARexp) : ARexp = r match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   131
  case AZERO => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   132
  case AONE(_) => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   133
  case ACHAR(bs, d) => if (c == d) AONE(bs) else AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   134
  case AALT(bs, r1, r2) => AALT(bs, der(c, r1), der(c, r2))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   135
  case ASEQ(bs, r1, r2) => 
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   136
    if (nullable(r1)) AALT(bs, ASEQ(Nil, der(c, r1), r2), fuse(mkepsBC(r1), der(c, r2)))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   137
    else ASEQ(bs, der(c, r1), r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   138
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(false), der(c, r)), ASTAR(Nil, r))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
// derivative w.r.t. a string (iterates der)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   142
@tailrec
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   143
def ders (s: List[Char], r: ARexp) : ARexp = s match {
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
  case Nil => r
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
  case c::s => ders(s, der(c, r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   148
// main unsimplified lexing function (produces a value)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   149
def lex(r: ARexp, s: List[Char]) : List[Boolean] = s match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   150
  case Nil => if (nullable(r)) mkepsBC(r) else throw new Exception("Not matched")
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   151
  case c::cs => lex(der(c, r), cs)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
204
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   154
def pre_lexing(r: Rexp, s: String) = lex(internalise(r), s.toList)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   155
def lexing(r: Rexp, s: String) : Val = decode(r, lex(internalise(r), s.toList))
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   156
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   157
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   158
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   159
def simp(r: ARexp): ARexp = r match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   160
  case ASEQ(bs1, r1, r2) => (simp(r1), simp(r2)) match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   161
      case (AZERO, _) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   162
      case (_, AZERO) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   163
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   164
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   165
  }
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   166
  case AALT(bs1, r1, r2) => (simp(r1), simp(r2)) match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   167
      case (AZERO, r2s) => fuse(bs1, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   168
      case (r1s, AZERO) => fuse(bs1, r1s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   169
      case (r1s, r2s) => AALT(bs1, r1s, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   170
  }
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   171
  case r => r
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   172
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   173
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   174
def lex_simp(r: ARexp, s: List[Char]) : List[Boolean] = s match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   175
  case Nil => if (nullable(r)) mkepsBC(r) else throw new Exception("Not matched")
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   176
  case c::cs => lex(simp(der(c, r)), cs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   177
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   178
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   179
def lexing_simp(r: Rexp, s: String) : Val = decode(r, lex_simp(internalise(r), s.toList))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   180
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   181
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   182
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   183
// extracts a string from value
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   184
def flatten(v: Val) : String = v match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   185
  case Empty => ""
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   186
  case Chr(c) => c.toString
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   187
  case Left(v) => flatten(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   188
  case Right(v) => flatten(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   189
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   190
  case Stars(vs) => vs.map(flatten).mkString
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   191
  case Rec(_, v) => flatten(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   192
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   193
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   194
// extracts an environment from a value
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   195
def env(v: Val) : List[(String, String)] = v match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   196
  case Empty => Nil
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   197
  case Chr(c) => Nil
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   198
  case Left(v) => env(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   199
  case Right(v) => env(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   200
  case Sequ(v1, v2) => env(v1) ::: env(v2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   201
  case Stars(vs) => vs.flatMap(env)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   202
  case Rec(x, v) => (x, flatten(v))::env(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   203
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   204
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   205
// Some Tests
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   206
//============
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   207
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   208
def time_needed[T](i: Int, code: => T) = {
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   209
  val start = System.nanoTime()
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   210
  for (j <- 1 to i) code
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   211
  val end = System.nanoTime()
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   212
  (end - start)/(i * 1.0e9)
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   213
}
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   214
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   215
204
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   216
val rf = ("a" | "ab") ~ ("ab" | "")
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   217
println(pre_lexing(rf, "ab"))
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   218
println(lexing(rf, "ab"))
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   219
println(lexing_simp(rf, "ab"))
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   220
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   221
val r0 = ("a" | "ab") ~ ("b" | "")
204
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   222
println(pre_lexing(r0, "ab"))
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   223
println(lexing(r0, "ab"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   224
println(lexing_simp(r0, "ab"))
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   225
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   226
val r1 = ("a" | "ab") ~ ("bcd" | "cd")
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   227
println(lexing(r1, "abcd"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   228
println(lexing_simp(r1, "abcd"))
78
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   229
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   230
println(lexing((("" | "a") ~ ("ab" | "b")), "ab"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   231
println(lexing_simp((("" | "a") ~ ("ab" | "b")), "ab"))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   232
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   233
println(lexing((("" | "a") ~ ("b" | "ab")), "ab"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   234
println(lexing_simp((("" | "a") ~ ("b" | "ab")), "ab"))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   235
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   236
println(lexing((("" | "a") ~ ("c" | "ab")), "ab"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   237
println(lexing_simp((("" | "a") ~ ("c" | "ab")), "ab"))
78
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   238
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
   239
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   240
// Two Simple Tests for the While Language
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   241
//========================================
78
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   242
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   243
// Lexing Rules 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
def PLUS(r: Rexp) = r ~ r.%
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
val ID = SYM ~ (SYM | DIGIT).% 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
val NUM = PLUS(DIGIT)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
val SEMI: Rexp = ";"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
val WHITESPACE = PLUS(" " | "\n" | "\t")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
val RPAREN: Rexp = ")"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
val LPAREN: Rexp = "("
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
val BEGIN: Rexp = "{"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
val END: Rexp = "}"
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   258
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   260
val WHILE_REGS = (("k" $ KEYWORD) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   261
                  ("i" $ ID) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   262
                  ("o" $ OP) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   263
                  ("n" $ NUM) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   264
                  ("s" $ SEMI) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   265
                  ("str" $ STRING) |
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   266
                  ("p" $ (LPAREN | RPAREN)) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   267
                  ("b" $ (BEGIN | END)) | 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   268
                  ("w" $ WHITESPACE)).%
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   270
println("prog0 test")
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   271
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
val prog0 = """read n"""
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   273
println(env(lexing(WHILE_REGS, prog0)))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   274
println(env(lexing_simp(WHILE_REGS, prog0)))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   276
println("prog1 test")
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   277
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
val prog1 = """read  n; write (n)"""
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   279
println(env(lexing(WHILE_REGS, prog1)))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   280
println(env(lexing_simp(WHILE_REGS, prog1)))
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   281
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   282
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   283
// Sulzmann's tests
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   284
//==================
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   285
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   286
val sulzmann = ("a" | "b" | "ab").%
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   287
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   288
println(lexing(sulzmann, "a" * 10))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   289
println(lexing_simp(sulzmann, "a" * 10))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   290
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   291
for (i <- 1 to 6501 by 500) {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   292
  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "a" * i))))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   293
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   294
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   295
for (i <- 1 to 16 by 5) {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   296
  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "ab" * i))))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   297
}