progs/scala/re-ext.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 11 Mar 2017 19:33:38 +0000
changeset 235 f476c98cad28
parent 234 18d19d039ac9
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
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
     5
import scala.util._
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
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
     8
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
     9
case object ONE extends Rexp
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    10
case class CHARS(f: Char => Boolean) extends Rexp
167
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 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
    12
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
    13
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
    14
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
    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
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    31
  case c::Nil => CHARS(_ == c)
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    32
  case c::s => SEQ(CHARS(_ == c), charlist2rexp(s))
167
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
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    71
  case CHARS(_) => false
167
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 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
    77
  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
    78
  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
    79
}
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
// 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
    82
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
    83
  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
    84
  case ONE => ZERO
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    85
  case CHARS(f) => if (f(c)) ONE else ZERO
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  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
    87
  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
    88
    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
    89
    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
    90
  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
    91
  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
    92
  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
    93
  case OPT(r) => ALT(der(c, r), ZERO)
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    94
  case NTIMES(r, n) => if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
// 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
    98
@tailrec
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
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
   100
  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
   101
  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
   102
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
// 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
   105
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
   106
  case Empty => ""
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  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
   108
  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
   109
  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
   110
  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
   111
  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
   112
  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
   113
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
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
// 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
   117
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
   118
  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
   119
  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
   120
  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
   121
  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
   122
  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
   123
  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
   124
  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
   125
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
// injection part
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
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
   129
  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
   130
  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
   131
    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
   132
  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
   133
  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
   134
  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
   135
  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
   136
  case OPT(_) => Right(Empty)
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   137
  case NTIMES(r, n) => Stars(List.fill(n)(mkeps(r)))
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
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
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
   142
  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
   143
  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
   144
  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
   145
  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
   146
  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
   147
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   148
  case (CHARS(_), Empty) => Chr(c) 
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
  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
   150
  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
   151
  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
   152
  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
   153
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
// 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
   156
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
   157
  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
   158
  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
   159
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   161
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   162
234
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   163
def lexing(r: Rexp, s: String) : Try[Val] = Try(lex(r, s.toList))
167
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
// 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
   167
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
   168
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
   169
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
   170
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
   171
  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
   172
  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
   173
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
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
   175
  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
   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_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
   178
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
   179
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
   180
  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
   181
}
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_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
   183
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
// 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
   185
// 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
   186
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
   187
  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
   188
    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
   189
    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
   190
    (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
   191
      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
   192
      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
   193
      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
   194
                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
   195
    }
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
  }
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
  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
   198
    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
   199
    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
   200
    (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
   201
      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
   202
      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
   203
      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
   204
      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
   205
      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
   206
    }
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
  }
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
  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
   209
    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
   210
    (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
   211
  }
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
  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
   213
}
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
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
   216
  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
   217
  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
   218
    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
   219
    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
   220
  }
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   223
def lexing_simp(r: Rexp, s: String) : Try[Val] = Try(lex_simp(r, s.toList))
167
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
// Some Tests
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
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
   230
  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
   231
  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
   232
  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
   233
  (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
   234
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
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
   238
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
   239
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
   240
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
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
   242
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
   243
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
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
   245
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
   246
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
   247
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
234
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   249
// Some more Tests
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   250
//=================
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   251
val ALL = CHARS((c) => true)
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   252
val reg = ALL.% ~ "a" ~ NTIMES(ALL, 3) ~ "bc"
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   253
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   254
println(lexing(reg, "axaybzbc"))                     // true
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   255
println(lexing(reg, "aaaaxaybzbc"))                  // true
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   256
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   257
println(nullable(ders("axaybzbd".toList, reg)))      // false
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   258
println(lexing(reg, "axaybzbd"))                     // false
234
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   259
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   260
18d19d039ac9 updated
Christian Urban <urbanc@in.tum.de>
parents: 231
diff changeset
   261
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
// 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
   264
//========================================
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
// Lexing Rules 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
231
02fd56e2019e updated the re-ext-scala file
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   268
val SYM = CHARS ("abcdefghijklmnopqrstuvwxyz" contains _)
02fd56e2019e updated the re-ext-scala file
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   269
val DIGIT = CHARS("0123456789" contains _)
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
val ID = SYM ~ (SYM | DIGIT).% 
231
02fd56e2019e updated the re-ext-scala file
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   271
val NUM = PLUS(DIGIT)
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
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
   273
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
   274
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
231
02fd56e2019e updated the re-ext-scala file
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   275
val WHITESPACE = PLUS(" " | "\n" | "\t")
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
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
   277
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
   278
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
   279
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
   280
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
   281
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
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
   284
                  ("i" $ ID) | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
                  ("o" $ OP) | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
                  ("n" $ NUM) | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
                  ("s" $ SEMI) | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
                  ("str" $ STRING) |
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
                  ("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
   290
                  ("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
   291
                  ("w" $ WHITESPACE)).%
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
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
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
   295
                  ID | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
                  OP | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
                  NUM | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
                  SEMI | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
                  LPAREN | RPAREN | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
                  BEGIN | END | 
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
                  WHITESPACE).%
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
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   304
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   305
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
   306
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
val prog0 = """read n"""
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   308
println(env(lexing_simp(WHILE_REGS, prog0).get))
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
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
   311
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
val prog1 = """read  n; write (n)"""
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   313
println(env(lexing_simp(WHILE_REGS, prog1).get))
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
// Bigger Test
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
//=============
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
val prog2 = """
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
i := 2;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
max := 100;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
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
   322
  isprime := 1;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
  j := 2;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
  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
   325
    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
   326
    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
   327
  };
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
  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
   329
  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
   330
}"""
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
println("prog2 test - tokens")
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   333
println(env(lexing_simp(WHILE_REGS, prog2).get))
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   334
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   335
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   336
// replicate prog2
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   337
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   338
println("prog2 replicated test")
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   339
for (i <- 1 to 88 by 5) {
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   340
  println(i + ": " + "%.5f".format(time_needed(2, lexing_simp(WHILE_REGS, prog2 * i))))
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   341
}
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
val prog3 = """
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
write "fib";
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
read n;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
minus1 := 0;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
minus2 := 1;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
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
   350
  temp := minus2;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
  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
   352
  minus1 := temp;
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
  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
   354
};
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
write "result";
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
write minus2
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
"""
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
println("prog3 test - tokens")
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   360
println(env(lexing_simp(WHILE_REGS, prog3).get))
231
02fd56e2019e updated the re-ext-scala file
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   361
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   362
231
02fd56e2019e updated the re-ext-scala file
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   363
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   364
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
// 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
   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
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
   369
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
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
   371
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
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
   373
  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
   374
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   375
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   376
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
   377
  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
   378
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   379
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
// 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
   382
//=======================
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
229
81c85f2746f5 updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   384
val reWord = CHARS("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" contains _)
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
val reWordStar = STAR(reWord)
235
f476c98cad28 updated
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   387
val reWordPlus = PLUS(reWord)
167
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
val optionSet1 = "-" | "+" | "."
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   389
val optionSet2 = "-" | "."
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   390
val atTheRate = "@"
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   391
val period = "."
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   392
val optionSet3 = "," | ";"
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
val whitespace = " "
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   394
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   395
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
   396
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
   397
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
   398
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
   399
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
   400
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
   401
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
   402
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
   403
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 = optionSet3
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   405
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
   406
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
   407
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
   408
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
   409
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
   410
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
   411
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
   412
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
   413
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
   414
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
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
   417
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
   418
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
   419
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
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
   421
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
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
   424
  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
   425
}
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   426
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   427
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
   428
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
   429
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   430
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   431
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
   432
1b2b9000be88 added an extended version of re-simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433