progs/scala/re2.scala
author fahad
Thu, 26 Feb 2015 12:51:02 +0000
changeset 70 7b78e093f559
parent 34 33065bde3bbd
child 156 6a43ea9305ba
permissions -rw-r--r--
merges
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions    
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scala.annotation.tailrec   
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
abstract class Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object NULL extends Rexp
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case object EMPTY extends Rexp
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class RECD(x: String, r: Rexp) extends Rexp
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
abstract class Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
case object Void extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
case class Chr(c: Char) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
case class Sequ(v1: Val, v2: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
case class Left(v: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
case class Right(v: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
case class Stars(vs: List[Val]) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
case class Rec(x: String, v: Val) extends Val
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
   
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// some convenience for typing in regular expressions
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
def charlist2rexp(s : List[Char]): Rexp = s match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  case Nil => EMPTY
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  case c::Nil => CHAR(c)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
implicit def RexpOps(r: Rexp) = new {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def | (s: Rexp) = ALT(r, s)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def % = STAR(r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  def ~ (s: Rexp) = SEQ(r, s)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
implicit def stringOps(s: String) = new {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def | (r: Rexp) = ALT(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def | (r: String) = ALT(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def % = STAR(s)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def ~ (r: Rexp) = SEQ(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def ~ (r: String) = SEQ(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  def $ (r: Rexp) = RECD(s, r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
// size of a regular expressions - for testing purposes 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
def size(r: Rexp) : Int = r match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  case NULL => 1
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  case EMPTY => 1
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  case CHAR(_) => 1
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case STAR(r) => 1 + size(r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case RECD(_, r) => 1 + size(r)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
// nullable function: tests whether the regular 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
// expression can recognise the empty string
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
def nullable (r: Rexp) : Boolean = r match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  case NULL => false
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  case EMPTY => true
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  case CHAR(_) => false
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  case STAR(_) => true
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  case RECD(_, r1) => nullable(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
// derivative of a regular expression w.r.t. a character
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
def der (c: Char, r: Rexp) : Rexp = r match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  case NULL => NULL
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
  case EMPTY => NULL
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  case CHAR(d) => if (c == d) EMPTY else NULL
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
  case SEQ(r1, r2) => 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
    else SEQ(der(c, r1), r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  case STAR(r) => SEQ(der(c, r), STAR(r))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case RECD(_, r1) => der(c, r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
// derivative w.r.t. a string (iterates der)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
def ders (s: List[Char], r: Rexp) : Rexp = s match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  case Nil => r
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  case c::s => ders(s, der(c, r))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
// extracts a string from value
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
def flatten(v: Val) : String = v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  case Void => ""
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
  case Chr(c) => c.toString
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  case Left(v) => flatten(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  case Right(v) => flatten(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  case Stars(vs) => vs.map(flatten).mkString
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case Rec(_, v) => flatten(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
// extracts an environment from a value
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
def env(v: Val) : List[(String, String)] = v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  case Void => Nil
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  case Chr(c) => Nil
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  case Left(v) => env(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
  case Right(v) => env(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
  case Sequ(v1, v2) => env(v1) ::: env(v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  case Stars(vs) => vs.flatMap(env)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  case Rec(x, v) => (x, flatten(v))::env(v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
def mkeps_all(r: Rexp) : Set[Val] = r match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  case EMPTY => Set(Void)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  case ALT(r1, r2) => (nullable(r1), nullable(r2)) match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
    case (true, true) => mkeps_all(r1).map(Left) ++ mkeps_all(r2).map(Right)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
    case (true, false) => mkeps_all(r1).map(Left)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
    case (false, true) => mkeps_all(r2).map(Right)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
  case SEQ(r1, r2) => for (v1 <- mkeps_all(r1);
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
                           v2 <- mkeps_all(r2)) yield Sequ(v1, v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  case STAR(r) => Set(Stars(Nil), Stars(List(mkeps(r))))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
  case RECD(x, r) => for (v <-  mkeps_all(r)) yield Rec(x, v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
  case (CHAR(d), Void) => Chr(c) 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
def inj_all(r: Rexp, c: Char, vs: Set[Val]) : Set[Val] =
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
  for (v <- vs) yield inj(r, c, v)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
// main lexing function (produces a value)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
def lex(r: Rexp, s: List[Char]) : Val = s match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  case c::cs => inj(r, c, lex(der(c, r), cs))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
// Examples
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
val K: Rexp = "a" | "b"
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
val I: Rexp = "ab" | "ba"  
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
val R0 = (K | I).%
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
lexing(R0, "abab")
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
val K: Rexp = ("key" $ "a" | "b")
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
val I: Rexp = ("id" $ ("ab" | "ba"))  
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
val R0 = (K | I).%
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
lexing(R0, "abaa")
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
env(lexing(R0, "abaa"))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
val r0: Rexp = (K | I).%
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
val r1 = der('a', r0)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
val r1_simp = simp2(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
val r2 = der('b', r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
val r2_simp = simp2(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
nullable(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
val v2 = mkeps(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
val v1 = inj(r1, 'b', v2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
val v0 = inj(r0, 'a', v1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
env(v0)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
env(lexing(r0, "abab"))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172