progs/scala/re-annotated2.sc
author Chengsong
Sat, 01 Oct 2022 12:06:46 +0100
changeset 608 37b6fd310a16
parent 361 8bb064045b4e
child 644 9f984ff20020
permissions -rw-r--r--
added related work chap
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// uses bitcode sequences and annotated regular 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// for basic regular expressions and RECD
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// the main difference is that it includes
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
// also character sets
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// uses a non-standard extraction method for generating
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// tokens (this is tail-recursive) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
// can match 200 copies of the fib-program (size 35000)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
// in about 20 secs
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
import scala.language.implicitConversions    
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
import scala.language.reflectiveCalls
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
import scala.annotation.tailrec   
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// standard regular expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
abstract class Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case object ZERO extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case object ONE extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class STAR(r: Rexp) extends Rexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class RECD(x: String, r: Rexp) extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class CHARSET(f: Char => Boolean) extends Rexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
//abbreviation for single characters
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
def CHAR(c: Char) = CHARSET(_ == c)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
// bit-codes with additional information about 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
// matched characters
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
abstract class Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
case object Z extends Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
case object S extends Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
case class C(c: Char) extends Bit
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
type Bits = List[Bit]
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
// annotated regular expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
abstract class ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
case object AZERO extends ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
case class AONE(bs: Bits) extends ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
case class ACHARSET(bs: Bits, f: Char => Boolean) extends ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
// an abbreviation for binary alternatives
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    54
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
// some convenience for typing in regular expressions
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
def charlist2rexp(s: List[Char]): Rexp = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
  case Nil => ONE
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case c::Nil => CHAR(c)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
implicit def string2rexp(s: String) : Rexp = charlist2rexp(s.toList)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
implicit def RexpOps(r: Rexp) = new {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  def | (s: Rexp) = ALT(r, s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  def % = STAR(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  def ~ (s: Rexp) = SEQ(r, s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
implicit def stringOps(s: String) = new {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  def | (r: Rexp) = ALT(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  def | (r: String) = ALT(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  def % = STAR(s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  def ~ (r: Rexp) = SEQ(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  def ~ (r: String) = SEQ(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  def $ (r: Rexp) = RECD(s, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
// Bitcoded + Annotation
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
//=======================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
//erase function: extracts the Rexp from ARexp
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
def erase(r:ARexp): Rexp = r match{
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  case AZERO => ZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  case AONE(_) => ONE
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  case AALTS(bs, Nil) => ZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  case AALTS(bs, r::Nil) => erase(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
  case AALTS(bs, r::rs) => ALT(erase(r), erase(AALTS(bs, rs)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
  case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
  case ASTAR(cs, r)=> STAR(erase(r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
  case ACHARSET(bs, f) => CHARSET(f)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  case AZERO => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  case AONE(cs) => AONE(bs ++ cs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  case ACHARSET(cs, f) => ACHARSET(bs ++ cs, f)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
def internalise(r: Rexp) : ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case ZERO => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case ONE => AONE(Nil)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case ALT(r1, r2) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
    AALT(Nil, fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case STAR(r) => ASTAR(Nil, internalise(r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  case RECD(_, r) => internalise(r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  case CHARSET(f) => ACHARSET(Nil, f)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
// example
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
// internalise(("a" | "ab") ~ ("b" | ""))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
// decoding of sequence of string tokens from a bitsequence
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   119
// tail-recursive version using an accumulator
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
@tailrec
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
def sdecode_aux(rs: List[Rexp], bs: Bits, acc: List[String]) : List[String] = (rs, bs) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case (Nil, _) => acc
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  case (_, Nil) => acc
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  case (ONE::rest, bs) => sdecode_aux(rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  case (ALT(r1, r2)::rest, Z::bs) => sdecode_aux(r1::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  case (ALT(r1, r2)::rest, S::bs) => sdecode_aux(r2::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case (SEQ(r1, r2)::rest, bs) => sdecode_aux(r1::r2::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
  case (STAR(r1)::rest, Z::bs) => sdecode_aux(r1::STAR(r1)::rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case (STAR(_)::rest, S::bs) => sdecode_aux(rest, bs, acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case (RECD(s, r1)::rest, bs) => sdecode_aux(r1::rest, bs, s"$s:"::acc)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case (CHARSET(_)::rest, C(c)::bs) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
    sdecode_aux(rest, bs, (acc.head :+ c)::acc.tail)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
def sdecode(r: Rexp, bs: Bits) : List[String] = 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  sdecode_aux(List(r), bs, List("")).reverse.tail
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
// nullable function: tests whether the a (annotated) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
// regular expression can recognise the empty string
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
def bnullable (r: ARexp) : Boolean = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  case AZERO => false
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  case AONE(_) => true
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
  case AALTS(_, rs) => rs.exists(bnullable)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
  case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
  case ASTAR(_, _) => true
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
  case ACHARSET(_, _) => false
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
def bmkeps(r: ARexp) : Bits = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  case AONE(bs) => bs
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  case AALTS(bs, r::Nil) => bs ++ bmkeps(r) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
  case AALTS(bs, r::rs) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
    if (bnullable(r)) bs ++ bmkeps(r) else bmkeps(AALTS(bs, rs))  
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  case ASTAR(bs, r) => bs ++ List(S)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
  case _ => throw new Exception("trying to bmkeps from non-nullable regex")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
// derivative of a regular expression w.r.t. a character
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
def bder(c: Char, r: ARexp) : ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
  case AZERO => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
  case AONE(_) => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
  case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  case ASEQ(bs, r1, r2) => 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
    if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    else ASEQ(bs, bder(c, r1), r2)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(Z), bder(c, r)), ASTAR(Nil, r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
  case ACHARSET(bs, f) => if(f(c)) AONE(bs :+ C(c)) else AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
// derivative w.r.t. a string (iterates bder)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
@tailrec
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   175
def bders(s: List[Char], r: ARexp) : ARexp = s match {
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  case Nil => r
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  case c::s => bders(s, bder(c, r))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
// main unsimplified lexing function (produces a bitsequence)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
def blex(r: ARexp, s: List[Char]) : Bits = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
  case Nil => if (bnullable(r)) bmkeps(r) else throw new Exception("Not matched")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
  case c::cs => blex(bder(c, r), cs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
// calls blex and decodes the value
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   187
def blexing(r: Rexp, s: String) = 
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   188
  sdecode(r, blex(internalise(r), s.toList))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
// example by Tudor
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
//val reg = (STAR("a") ~ ("b" | "c")).%
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
//println(blexing(reg, "aab"))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
//=======================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
// simplification 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
//
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
def flts(rs: List[ARexp]) : List[ARexp] = rs match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
  case Nil => Nil
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
  case AZERO :: rs => flts(rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
  case AALTS(bs, rs1) :: rs => rs1.map(fuse(bs, _)) ++ flts(rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
  case r1 :: rs => r1 :: flts(rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
def bsimp(r: ARexp): ARexp = r match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
  case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
      case (AZERO, _) => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
      case (_, AZERO) => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
      // needed in order to keep the size down
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
      case (AALTS(bs, rs), r2) => AALTS(bs1 ++ bs, rs.map(ASEQ(Nil, _, r2)))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
  // distinctBy deletes copies of the same  "erased" regex
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
  case AALTS(bs1, rs) => (flts(rs.map(bsimp))).distinctBy(erase) match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
      case Nil => AZERO
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
      case r::Nil => fuse(bs1, r)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
      case rs => AALTS(bs1, rs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
  }
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  case r => r
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
  case Nil => if (bnullable(r)) bmkeps(r) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
              else throw new Exception("Not matched")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
  case c::cs => blex_simp(bsimp(bder(c, r)), cs)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   232
// blexing_simp decodes a string-list from the bitsequence
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   233
def blexing_simp(r: Rexp, s: String) : List[String] = 
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
  sdecode(r, blex_simp(internalise(r), s.toList))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
//println(blexing_simp(reg, "aab"))
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   240
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   241
def size(r: Rexp) : Int = r match {
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   242
  case ZERO => 1
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   243
  case ONE => 1
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   244
  case CHARSET(_) => 1
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   245
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   246
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   247
  case STAR(r) => 1 + size(r)
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   248
  case RECD(_, r) => 1 + size(r)
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   251
def bsize(r: ARexp) = size(erase(r))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
// Some Tests
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
//============
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
def time_needed[T](i: Int, code: => T) = {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
  val start = System.nanoTime()
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
  for (j <- 1 to i) code
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
  val end = System.nanoTime()
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
  (end - start)/(i * 1.0e9)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   263
val ones = SEQ(SEQ(CHAR('/'), CHAR('*')), SEQ(STAR(CHAR('1')), SEQ(CHAR('*'), CHAR('/'))))
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   264
println("sizes unsimplified")
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   265
println(bsize(bders("/*".toList, internalise(ones))))     // 12
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   266
println(bsize(bders("/*1".toList, internalise(ones))))    // 25
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   267
println(bsize(bders("/*11".toList, internalise(ones))))   // 34
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   268
println(bsize(bders("/*111".toList, internalise(ones))))  // 43
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   269
println(bsize(bders("/*1111".toList, internalise(ones)))) // 52
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   270
println("sizes simplified")
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   271
println(bsize(bsimp(bders("/*".toList, internalise(ones)))))     // 6
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   272
println(bsize(bsimp(bders("/*1".toList, internalise(ones)))))    // 6
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   273
println(bsize(bsimp(bders("/*11".toList, internalise(ones)))))   // 6
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   274
println(bsize(bsimp(bders("/*111".toList, internalise(ones)))))  // 6
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   275
println(bsize(bsimp(bders("/*1111".toList, internalise(ones))))) // 6
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   276
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   277
println("ones:")
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   278
for(i <- 0 to 10000 by 1000) {
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   279
    println(s"$i: ${time_needed(1, blexing_simp(ones, "/*" ++ "1" * i ++ "*/"))}")
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   280
}
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   281
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   282
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   283
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   284
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   285
System.exit(1)
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   286
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
val evil1 = STAR(STAR("a")) ~ "b"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
val evil2 = STAR(STAR(STAR("a"))) ~ "b"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   289
val evil3 = STAR("aa" | "a")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   290
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   291
println("evil1")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
for(i <- 0 to 10000 by 1000) {
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   293
    println(time_needed(1, blexing_simp(evil1, "a" * i ++ "b")))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   296
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   297
println("evil2")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   298
for(i <- 0 to 10000 by 1000) {
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   299
    println(time_needed(1, blexing_simp(evil2, "a" * i ++ "b")))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   300
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   301
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   302
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   303
println("evil3")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   304
for(i <- 0 to 10000 by 1000) {
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   305
    println(time_needed(1, blexing_simp(evil3, "a" * i)))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   306
}
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   307
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   308
// WHILE LANGUAGE
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   309
//================
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   310
def PLUS(r: Rexp) = r ~ r.%
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   311
def RANGE(s: String) = CHARSET(s.toSet)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   312
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   313
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   314
val DIGIT = RANGE("0123456789")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   315
val ID = SYM ~ (SYM | DIGIT).% 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   316
val NUM = PLUS(DIGIT)
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   317
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   318
val SEMI: Rexp = ";"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   319
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   320
val WHITESPACE = PLUS(" " | "\n" | "\t")
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   321
val RPAREN: Rexp = "{"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   322
val LPAREN: Rexp = "}"
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   323
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   324
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   325
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   326
val WHILE_REGS = (("k" $ KEYWORD) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   327
                  ("i" $ ID) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   328
                  ("o" $ OP) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   329
                  ("n" $ NUM) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   330
                  ("s" $ SEMI) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   331
                  ("str" $ STRING) |
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   332
                  ("p" $ (LPAREN | RPAREN)) | 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   333
                  ("w" $ WHITESPACE)).%
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   334
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   335
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   336
// Some Simple While Tests
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   337
//========================
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   338
println("WHILE TESTS")
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   339
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   340
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   341
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   342
val prog0 = """read n"""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   343
println(s"test: $prog0")
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   344
println(blexing_simp(WHILE_REGS, prog0))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   345
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   346
val prog1 = """read  n; write n"""  
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   347
println(s"test: $prog1")
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   348
println(blexing_simp(WHILE_REGS, prog1))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   349
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   350
val prog2 = """
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   351
write "Fib";
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   352
read n;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   353
minus1 := 0;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   354
minus2 := 1;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   355
while n > 0 do {
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   356
  temp := minus2;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   357
  minus2 := minus1 + minus2;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   358
  minus1 := temp;
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   359
  n := n - 1
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   360
};
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   361
write "Result";
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   362
write minus2
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   363
"""
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   364
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   365
println("lexing fib program (once)")
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   366
println(blexing_simp(WHILE_REGS, prog2).filter(s => s == "" || !s.startsWith("w")))
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   367
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   368
val n = 200
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   369
println(s"lexing fib program ($n times, size ${prog2.length * n})")
361
8bb064045b4e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   370
println(time_needed(1, blexing_simp(WHILE_REGS, prog2 * n)))