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