progs/scala/re-basic.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 17 May 2016 10:53:34 +0100
changeset 182 2e70c1b06ac0
parent 178 2835d13be702
child 238 2dc1647eab9e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
     1
/* lexer without simplification */
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
     2
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scala.language.implicitConversions    
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import scala.language.reflectiveCalls
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
import scala.annotation.tailrec   
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
     6
import scala.io.Source
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
abstract class Rexp 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
     9
case object ZERO extends Rexp
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    10
case object ONE extends Rexp
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class CHAR(c: Char) extends Rexp
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class STAR(r: Rexp) extends Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
case class RECD(x: String, r: Rexp) extends Rexp
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
abstract class Val
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    18
case object Empty extends Val
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
case class Chr(c: Char) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
case class Sequ(v1: Val, v2: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
case class Left(v: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
case class Right(v: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
case class Stars(vs: List[Val]) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
case class Rec(x: String, v: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
   
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
// some convenience for typing in regular expressions
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
def charlist2rexp(s : List[Char]): Rexp = s match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    28
  case Nil => ONE
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  case c::Nil => CHAR(c)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
implicit def RexpOps(r: Rexp) = new {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def | (s: Rexp) = ALT(r, s)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def % = STAR(r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def ~ (s: Rexp) = SEQ(r, s)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
implicit def stringOps(s: String) = new {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def | (r: Rexp) = ALT(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def | (r: String) = ALT(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  def % = STAR(s)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def ~ (r: Rexp) = SEQ(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  def ~ (r: String) = SEQ(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  def $ (r: Rexp) = RECD(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    49
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: 157
diff changeset
    50
  case Nil => ZERO
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    51
  case r::Nil => r
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    52
  case r::rs => ALT(r, Alts(rs))
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    53
}
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    54
def ALTS(rs: Rexp*) = Alts(rs.toList)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    55
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    56
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: 157
diff changeset
    57
  case Nil => ONE
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    58
  case r::Nil => r
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    59
  case r::rs => SEQ(r, Seqs(rs))
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    60
}
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    61
def SEQS(rs: Rexp*) = Seqs(rs.toList)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    62
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
// nullable function: tests whether the regular 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
// expression can recognise the empty string
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
def nullable (r: Rexp) : Boolean = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    66
  case ZERO => false
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    67
  case ONE => true
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  case CHAR(_) => false
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  case STAR(_) => true
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  case RECD(_, r1) => nullable(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
// derivative of a regular expression w.r.t. a character
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
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: 34
diff changeset
    77
  case ZERO => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    78
  case ONE => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
    79
  case CHAR(d) => if (c == d) ONE else ZERO
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  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:
diff changeset
    82
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
    else SEQ(der(c, r1), r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case STAR(r) => SEQ(der(c, r), STAR(r))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  case RECD(_, r1) => der(c, r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
// derivative w.r.t. a string (iterates der)
33065bde3bbd added a file for calculating all answers...still incomplete
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 {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  case Nil => r
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  case c::s => ders(s, 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:
diff changeset
    92
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
// extracts a string from value
33065bde3bbd added a file for calculating all answers...still incomplete
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: 34
diff changeset
    96
  case Empty => ""
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case Chr(c) => c.toString
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  case Left(v) => flatten(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  case Right(v) => flatten(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
  case Stars(vs) => vs.map(flatten).mkString
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  case Rec(_, v) => flatten(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
// extracts an environment from a value
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
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: 34
diff changeset
   107
  case Empty => Nil
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  case Chr(c) => Nil
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  case Left(v) => env(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  case Right(v) => env(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  case Sequ(v1, v2) => env(v1) ::: env(v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  case Stars(vs) => vs.flatMap(env)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  case Rec(x, v) => (x, flatten(v))::env(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   116
// injection part
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   117
def mkeps(r: Rexp) : Val = r match {
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   118
  case ONE => Empty
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   119
  case ALT(r1, r2) => 
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   120
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   121
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   122
  case STAR(r) => Stars(Nil)
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   123
  case RECD(x, r) => Rec(x, mkeps(r))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   124
}
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   125
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  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: 34
diff changeset
   133
  case (CHAR(d), Empty) => Chr(c) 
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
// main lexing function (produces a value)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
def lex(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:
diff changeset
   140
  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:
diff changeset
   141
  case c::cs => inj(r, c, lex(der(c, r), cs))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
def lexing(r: Rexp, s: String) : Val = lex(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:
diff changeset
   145
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
// Examples
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
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:
diff changeset
   148
val K: Rexp = "a" | "b"
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
val I: Rexp = "ab" | "ba"  
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   151
println(lexing((K | I).%, "abab"))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   153
val K2: Rexp = ("key" $ "a" | "b")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   154
val I2: Rexp = ("id" $ ("ab" | "ba"))  
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   156
println(lexing((K2 | I2).%, "abaa"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   157
println(env(lexing((K2 | I2).%, "abaa")))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   159
val r1: Rexp = "abc"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   160
val r2: Rexp = der('a', r1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   161
val r3: Rexp = der('b', r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   162
val r4: Rexp = der('c', r3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   163
println(r1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   164
println(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   165
println(r3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   166
println(r4)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   167
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 166
diff changeset
   168
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   169
// time keeping
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   170
def time_needed[T](i: Int, code: => T) = {
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   171
  val start = System.nanoTime()
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   172
  for (j <- 1 to i) code
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   173
  val end = System.nanoTime()
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   174
  (end - start)/(i * 1.0e9)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   175
}
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   176
166
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   177
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   178
// first benchmark regex 
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   179
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   180
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: 157
diff changeset
   181
                  "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: 157
diff changeset
   182
                  "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: 157
diff changeset
   183
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   184
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   185
val reWordStar = STAR(reWord)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   186
val reWordPlus = reWord ~ reWordStar
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   187
val optionSet1 = "-" | "+" | "."
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   188
val optionSet2 = "-" | "."
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   189
val atTheRate = "@"
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   190
val period = "."
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   191
val optionSet3 = "," | ";"
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   192
val whitespace = " "
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   193
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   194
val re01 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   195
val re02 = STAR(optionSet1 ~ reWordPlus)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   196
val re03 = atTheRate
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   197
val re04 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   198
val re05 = STAR(optionSet2 ~ reWordPlus)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   199
val re06 = period
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   200
val re07 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   201
val re08 = re05
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   202
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   203
val re09 = optionSet3
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   204
val re10 = STAR(whitespace)
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   205
val re11 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   206
val re12 = re02
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   207
val re13 = atTheRate
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   208
val re14 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   209
val re15 = re05
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   210
val re16 = period
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   211
val re17 = reWordPlus
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   212
val re18 = re05
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   213
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   214
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   215
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: 157
diff changeset
   216
val re09_10 = re09 ~ re10
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   217
val re11_18 = re01_08
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   218
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   219
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: 157
diff changeset
   220
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   221
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   222
def process(s: String, i: Int) : Unit = {
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   223
  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: 157
diff changeset
   224
}
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   225
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   226
val filename = "../tests/emails.txt"
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   227
val filelines = Source.fromFile(filename).getLines.take(22).zipWithIndex
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   228
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   229
cab1ae6f339a added benchmark from Fahad
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   230
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: 157
diff changeset
   231