progs/scala/re-simp.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 166 cab1ae6f339a
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   
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
     4
import scala.io.Source
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
abstract class Rexp 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     7
case object ZERO extends Rexp
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     8
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
     9
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
    10
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
    11
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
    12
case class STAR(r: Rexp) extends Rexp 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class RECD(x: String, r: Rexp) extends Rexp
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
abstract class Val
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    16
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
    17
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
    18
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
    19
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
    20
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
    21
case class Stars(vs: List[Val]) extends Val
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
case class Rec(x: String, v: Val) extends Val
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
// 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
    25
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
    26
  case Nil => ONE
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  case c::Nil => CHAR(c)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  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
    29
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
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
    31
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
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
    33
  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
    34
  def % = STAR(r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  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
    36
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
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
    39
  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
    40
  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
    41
  def % = STAR(s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def ~ (r: Rexp) = SEQ(s, r)
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
    43
  def ~ (r: String) = SEQ(s, r)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def $ (r: Rexp) = RECD(s, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    47
def Alts(rs: List[Rexp]) : Rexp = rs match {
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    48
  case Nil => ZERO
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    49
  case r::Nil => r
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    50
  case r::rs => ALT(r, Alts(rs))
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    51
}
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    52
def ALTS(rs: Rexp*) = Alts(rs.toList)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    53
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    54
def Seqs(rs: List[Rexp]) : Rexp = rs match {
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    55
  case Nil => ONE
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    56
  case r::Nil => r
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    57
  case r::rs => SEQ(r, Seqs(rs))
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    58
}
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    59
def SEQS(rs: Rexp*) = Seqs(rs.toList)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    60
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
    61
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
// nullable function: tests whether the regular 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
// expression can recognise the empty string
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
def nullable (r: Rexp) : Boolean = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    65
  case ZERO => false
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    66
  case ONE => true
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  case CHAR(_) => false
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  case STAR(_) => true
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  case RECD(_, r1) => nullable(r1)
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
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
// derivative of a regular expression w.r.t. a character
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
def der (c: Char, r: Rexp) : Rexp = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    76
  case ZERO => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    77
  case ONE => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    78
  case CHAR(d) => if (c == d) ONE else ZERO
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case SEQ(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
    else SEQ(der(c, r1), r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  case STAR(r) => SEQ(der(c, r), STAR(r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case RECD(_, r1) => der(c, r1)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
// 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
    88
@tailrec
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
def ders (s: List[Char], r: Rexp) : Rexp = s match {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  case Nil => r
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  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
    92
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
// extracts a string from value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
def flatten(v: Val) : String = v match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    96
  case Empty => ""
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case Chr(c) => c.toString
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    98
  case Left(v) => flatten(v)
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    99
  case Right(v) => flatten(v)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   101
  case Stars(vs) => vs.map(flatten(_)).mkString
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  case Rec(_, v) => flatten(v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
77
4b4c677501e7 proved some basic properties (totality and trichonomity) for the orderings
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   105
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
// extracts an environment from a value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
def env(v: Val) : List[(String, String)] = v match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   108
  case Empty => Nil
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  case Chr(c) => Nil
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   110
  case Left(v) => env(v)
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   111
  case Right(v) => env(v)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  case Sequ(v1, v2) => env(v1) ::: env(v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  case Stars(vs) => vs.flatMap(env)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  case Rec(x, v) => (x, flatten(v))::env(v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
48
652861c9473f added a function for calculating values
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   117
// injection part
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
def mkeps(r: Rexp) : Val = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   119
  case ONE => Empty
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  case ALT(r1, r2) => 
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   121
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
  case STAR(r) => Stars(Nil)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  case RECD(x, r) => Rec(x, mkeps(r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   127
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   133
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   135
  case (CHAR(d), Empty) => Chr(c) 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   139
// main unsimplified lexing function (produces a value)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
def lex(r: Rexp, s: List[Char]) : Val = s match {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
  case c::cs => inj(r, c, lex(der(c, r), cs))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
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
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   148
// some "rectification" functions for simplification
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   149
def F_ID(v: Val): Val = v
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   150
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   151
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   152
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   153
  case Right(v) => Right(f2(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   154
  case Left(v) => Left(f1(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   155
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   156
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   157
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   158
}
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   159
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(Empty), f2(v))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   160
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(v), f2(Empty))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   161
def F_RECD(f: Val => Val) = (v:Val) => v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   162
  case Rec(x, v) => Rec(x, f(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   163
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   164
def F_ERROR(v: Val): Val = throw new Exception("error")
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   165
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   166
// simplification of regular expressions returning also an
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   167
// rectification function; no simplification under STAR 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   168
def simp(r: Rexp): (Rexp, Val => Val) = r match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   169
  case ALT(r1, r2) => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   170
    val (r1s, f1s) = simp(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   171
    val (r2s, f2s) = simp(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   172
    (r1s, r2s) match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   173
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   174
      case (_, ZERO) => (r1s, F_LEFT(f1s))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   175
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   176
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   177
    }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   178
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   179
  case SEQ(r1, r2) => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   180
    val (r1s, f1s) = simp(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   181
    val (r2s, f2s) = simp(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   182
    (r1s, r2s) match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   183
      case (ZERO, _) => (ZERO, F_ERROR)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   184
      case (_, ZERO) => (ZERO, F_ERROR)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   185
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   186
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   187
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   188
    }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   189
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   190
  case RECD(x, r1) => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   191
    val (r1s, f1s) = simp(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   192
    (RECD(x, r1s), F_RECD(f1s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   193
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   194
  case r => (r, F_ID)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   195
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   196
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   197
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   198
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   199
  case c::cs => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   200
    val (r_simp, f_simp) = simp(der(c, r))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   201
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   202
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   203
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   204
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   205
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   206
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   207
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   208
// Some Tests
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   209
//============
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   210
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   211
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
   212
  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
   213
  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
   214
  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
   215
  (end - start)/(i * 1.0e9)
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   216
}
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   217
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   218
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   219
val r0 = ("a" | "ab") ~ ("b" | "")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   220
println(lexing(r0, "ab"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   221
println(lexing_simp(r0, "ab"))
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   222
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   223
val r1 = ("a" | "ab") ~ ("bcd" | "cd")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   224
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
   225
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   226
println(lexing_simp((("" | "a") ~ ("ab" | "b")), "ab"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   227
println(lexing_simp((("" | "a") ~ ("b" | "ab")), "ab"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   228
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
   229
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   230
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
   231
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   232
// 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
   233
//========================================
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
   234
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   235
// Lexing Rules 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
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
   238
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
   239
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
   240
val ID = SYM ~ (SYM | DIGIT).% 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
val NUM = PLUS(DIGIT)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
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
   243
val SEMI: Rexp = ";"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
val WHITESPACE = PLUS(" " | "\n" | "\t")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
val RPAREN: Rexp = ")"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
val LPAREN: Rexp = "("
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
val BEGIN: Rexp = "{"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
val END: Rexp = "}"
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   250
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   252
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   253
val WHILE_REGS = (("k" $ KEYWORD) | 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
                  ("i" $ ID) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
                  ("o" $ OP) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
                  ("n" $ NUM) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
                  ("s" $ SEMI) | 
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   258
                  ("str" $ STRING) |
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
                  ("p" $ (LPAREN | RPAREN)) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
                  ("b" $ (BEGIN | END)) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
                  ("w" $ WHITESPACE)).%
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   263
/*
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
val WHILE_REGS = (KEYWORD | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
                  ID | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
                  OP | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
                  NUM | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
                  SEMI | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
                  LPAREN | RPAREN | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
                  BEGIN | END | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
                  WHITESPACE).%
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   272
*/
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   275
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
   276
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
val prog0 = """read n"""
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   278
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
   279
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   280
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
   281
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
val prog1 = """read  n; write (n)"""
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   283
println(env(lexing_simp(WHILE_REGS, prog1)))
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   284
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   286
// Bigger Test
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   287
//=============
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
val prog2 = """
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
i := 2;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
max := 100;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
while i < max do {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
  isprime := 1;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
  j := 2;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
  while (j * j) <= i + 1  do {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
    if i % j == 0 then isprime := 0  else skip;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
    j := j + 1
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
  };
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
  if isprime == 1 then write i else skip;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
  i := i + 1
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
}"""
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   301
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   302
println("prog2 test - tokens")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   303
println(env(lexing_simp(WHILE_REGS, prog2)))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   304
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   305
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   306
val prog3 = """
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   307
write "fib";
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   308
read n;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   309
minus1 := 0;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   310
minus2 := 1;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   311
while n > 0 do {
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   312
  temp := minus2;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   313
  minus2 := minus1 + minus2;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   314
  minus1 := temp;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   315
  n := n - 1
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   316
};
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   317
write "result";
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   318
write minus2
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   319
"""
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   320
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   321
println("prog3 test - tokens")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   322
println(env(lexing_simp(WHILE_REGS, prog3)))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   324
/*
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   325
for (i <- 1 to 80) {
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
  print(i.toString + ":  ")
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   327
  time(lexing_simp(WHILE_REGS, prog2 * i))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
}
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   329
*/
73
6e035162345a solved one case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 66
diff changeset
   330
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   331
// Sulzmann's tests
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   332
//==================
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   333
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   334
val sulzmann = ("a" | "b" | "ab").%
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   335
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   336
println(lexing_simp(sulzmann, "a" * 10))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   337
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   338
for (i <- 1 to 4501 by 500) {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   339
  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "a" * i))))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   340
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   341
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   342
for (i <- 1 to 2001 by 500) {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   343
  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "ab" * i))))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   344
}
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   345
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   346
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   347
// first benchmark regex 
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   348
//=======================
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   349
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   350
val reWord = ALTS("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   351
                  "w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R",
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   352
                  "S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9")
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   353
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   354
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   355
val reWordStar = STAR(reWord)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   356
val reWordPlus = reWord ~ reWordStar
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   357
val optionSet1 = "-" | "+" | "."
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   358
val optionSet2 = "-" | "."
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   359
val atTheRate = "@"
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   360
val period = "."
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   361
val optionSet3 = "," | ";"
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   362
val whitespace = " "
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   363
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   364
val re01 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   365
val re02 = STAR(optionSet1 ~ reWordPlus)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   366
val re03 = atTheRate
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   367
val re04 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   368
val re05 = STAR(optionSet2 ~ reWordPlus)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   369
val re06 = period
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   370
val re07 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   371
val re08 = re05
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   372
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   373
val re09 = optionSet3
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   374
val re10 = STAR(whitespace)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   375
val re11 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   376
val re12 = re02
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   377
val re13 = atTheRate
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   378
val re14 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   379
val re15 = re05
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   380
val re16 = period
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   381
val re17 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   382
val re18 = re05
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   383
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   384
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   385
val re01_08 = SEQS(re01, re02, re03, re04, re05, re06, re07, re08)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   386
val re09_10 = re09 ~ re10
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   387
val re11_18 = re01_08
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   388
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   389
val re = re01_08 ~ STAR(re09_10 ~ re11_18)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   390
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   391
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   392
def process(s: String, i: Int) : Unit = {
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   393
  println(i + " " + "%.5f".format(time_needed(1, lexing(re, s))))
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   394
}
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   395
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   396
val filename = "../tests/emails.txt"
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   397
val filelines = Source.fromFile(filename).getLines.take(26).zipWithIndex
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   398
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   399
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   400
filelines.foreach({ case (s: String, i: Int) => process(s, i) })
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   401
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   402