progs/token.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 01 Jun 2018 15:28:37 +0100
changeset 550 71fc4a7a7039
parent 549 352d15782d35
child 552 40fa0f628dc4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     1
import scala.language.implicitConversions    
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     4
abstract class Rexp 
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
     5
case object ZERO extends Rexp
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
     6
case object ONE extends Rexp
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case class CHAR(c: Char) extends Rexp
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
     8
case class ALTS(rs: List[Rexp]) extends Rexp 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     9
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    10
case class STAR(r: Rexp) extends Rexp 
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    12
// ALT is now an abbreviation
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    13
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    14
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    15
// proj is now used instead for Left and Right
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    16
abstract class Val
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    17
case object Empty extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    18
case class Chr(c: Char) extends Val
520
2849c305b12d updated
cu
parents: 450
diff changeset
    19
case class Sequ(v1: Val, v2: Val) extends Val
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    20
case class Proj(n: Int, v: Val) extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    21
case class Stars(vs: List[Val]) extends Val
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    22
case class Rec(s: String, v: Val) extends Val
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    23
 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    24
// for manipulating projections  
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    25
def IncProj(m: Int, v: Val) = v match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    26
  case Proj(n, v) => Proj(n + m, v)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    27
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    28
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    29
def DecProj(m: Int, v: Val) = v match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    30
  case Proj(n, v) => Proj(n - m, v)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    31
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    32
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    33
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    34
// some convenience for typing in regular expressions
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    35
def charlist2rexp(s : List[Char]): Rexp = s match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    36
  case Nil => ONE
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  case c::Nil => CHAR(c)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
implicit def RexpOps(r: Rexp) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  def | (s: Rexp) = ALT(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def % = STAR(r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  def ~ (s: Rexp) = SEQ(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
implicit def stringOps(s: String) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  def | (r: Rexp) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  def | (r: String) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  def % = STAR(s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  def ~ (r: Rexp) = SEQ(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  def ~ (r: String) = SEQ(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    56
// string of a regular expression - for testing purposes 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    57
def string(r: Rexp) : String = r match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    58
  case ZERO => "0"
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    59
  case ONE => "1"
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    60
  case CHAR(c) => c.toString 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    61
  case ALTS(rs) => rs.map(string).mkString("[", "|", "]")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    62
  case SEQ(CHAR(c), CHAR(d)) => s"${c}${d}"
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    63
  case SEQ(ONE, CHAR(c)) => s"1${c}"
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    64
  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    65
  case STAR(r) => s"(${string(r)})*"
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    66
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    67
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    68
// size of a regular expression - for testing purposes 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    69
def size(r: Rexp) : Int = r match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    70
  case ZERO => 1
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    71
  case ONE => 1
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    72
  case CHAR(_) => 1
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    73
  case ALTS(rs) => 1 + rs.map(size).sum
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    74
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    75
  case STAR(r) => 1 + size(r)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    76
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    77
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    78
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    79
// nullable function: tests whether the regular 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    80
// expression can recognise the empty string
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
def nullable (r: Rexp) : Boolean = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    82
  case ZERO => false
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    83
  case ONE => true
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case CHAR(_) => false
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    85
  case ALTS(rs) => rs.exists(nullable)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  case STAR(_) => true
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    90
// derivative of a regular expression w.r.t. a character
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
def der (c: Char, r: Rexp) : Rexp = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    92
  case ZERO => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    93
  case ONE => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    94
  case CHAR(d) => if (c == d) ONE else ZERO
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
    95
  case ALTS(rs) => ALTS(rs.map(der(c, _)))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  case SEQ(r1, r2) => 
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
    else SEQ(der(c, r1), r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  case STAR(r) => SEQ(der(c, r), STAR(r))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   100
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   101
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   102
// derivative w.r.t. a string (iterates der)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   103
def ders (s: List[Char], r: Rexp) : Rexp = s match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   104
  case Nil => r
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   105
  case c::s => ders(s, der(c, r))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   106
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   107
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   108
val test : Rexp= STAR("a" | "aa")
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   109
size(test)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   110
size(der('a', test))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   111
size(der('a', der('a', test)))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   112
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   113
size(ders("aaaaaa".toList, test)) 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   114
string(ders("aaaaaa".toList, test))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   115
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 549
diff changeset
   116
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   117
// extracts a string from value
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   118
def flatten(v: Val) : String = v match {
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   119
  case Empty => ""
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   120
  case Chr(c) => c.toString
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   121
  case Proj(_, v) => flatten(v)
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   122
  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   123
  case Stars(vs) => vs.map(flatten).mkString
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   127
// mkeps
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   128
def mkeps(r: Rexp) : Val = r match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   129
  case ONE => Empty
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   130
  case ALTS(r1::rs) => 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   131
    if (nullable(r1)) Proj(0, mkeps(r1)) 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   132
    else IncProj(1, mkeps(ALTS(rs)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   133
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   134
  case STAR(r) => Stars(Nil)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   135
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   136
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   137
// injection 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   138
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   139
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
2849c305b12d updated
cu
parents: 450
diff changeset
   140
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   141
  case (SEQ(r1, r2), Proj(0, Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   142
  case (SEQ(r1, r2), Proj(1, v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   143
  case (ALTS(rs), Proj(n, v1)) => Proj(n, inj(rs(n), c, v1))
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   144
  case (CHAR(d), Empty) => Chr(c) 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   145
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   146
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   147
// main lexing function (produces a value)
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   148
//  - does  not simplify
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   149
def lex(r: Rexp, s: List[Char]) : Val = s match {
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   150
  case Nil => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   151
    //println(s"Size of the last regex: ${size(r)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   152
    if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   153
  }
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   154
  case c::cs => inj(r, c, lex(der(c, r), cs))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   155
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   156
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   157
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   158
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   159
lexing("a" | ZERO, "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   160
lexing(ZERO | "a", "a")
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   161
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   162
lexing(("ab" | "a") ~ ("b" | ONE), "ab")
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   163
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   164
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   165
// removing duplicate regular expressions
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   166
val unit = (ZERO, F_ERROR(_))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   167
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   168
def dups2(xs: List[(Rexp, Val => Val)], 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   169
	 acc: List[(Rexp, Val => Val)] = Nil) : List[(Rexp, Val => Val)] = xs match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   170
  case Nil => acc
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   171
  case (x, y)::xs => 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   172
	     if (acc.map(_._1).contains(x)) dups2(xs, acc :+ unit)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   173
	     else dups2(xs, acc :+ (x, y))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   174
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   175
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   176
def dups(xs: List[(Rexp, Val => Val)]) : List[(Rexp, Val => Val)] = {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   177
  val out = dups2(xs)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   178
  //if (out != xs) {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   179
  //  println()
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   180
  //  println(s"Input ${string(ALTS(xs.map(_._1)))}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   181
  //  println(s"Ouput ${string(ALTS(out.map(_._1)))}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   182
  //}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   183
  out
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   184
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   185
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   186
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   187
// some "rectification" functions for simplification
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   188
def F_ID(v: Val): Val = v
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   189
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   190
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   191
}
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   192
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   193
  (v:Val) => Sequ(f1(Empty), f2(v))
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   194
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   195
  (v:Val) => Sequ(f1(v), f2(Empty))
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   196
def F_ERROR(v: Val): Val = throw new Exception("error")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   197
def F_PRINT(v: Val): Val = {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   198
  println(s"value is ${v}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   199
  throw new Exception("caught error")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   200
}
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   201
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   202
def flats(rs: List[Rexp], seen: Set[Rexp]) : (List[Rexp], Val => Val) = {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   203
  //println(s"I am flats: ${string(ALTS(rs))}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   204
  //println(s"The size of seen is ${seen.size}, ${seen.map(string)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   205
  rs match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   206
  case Nil => (Nil, F_ERROR)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   207
  case r::rs1 if seen.contains(simp(r)._1) => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   208
    //println(s" I remove ${string(r)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   209
    val (rs2, f) = flats(rs1, seen)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   210
    (rs2, (v:Val) => IncProj(1, f(v)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   211
  }
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   212
  case ZERO::rs1 => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   213
    val (rs2, f) = flats(rs1, seen)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   214
    (rs2, (v:Val) => IncProj(1, f(v)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   215
  }
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   216
  case ALTS(rs0)::rs1 => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   217
    val (rss, f1) = flats(rs0, seen)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   218
    val (rs2, f2) = flats(rs1, rss.toSet ++ seen)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   219
    (rss:::rs2, (v:Val) => v match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   220
      case Proj(n, vn) => 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   221
     	if (n < rss.length) Proj(0, f1(Proj(n, vn)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   222
  	else IncProj(1, f2(Proj(n - rss.length, vn)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   223
    })
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   224
  }
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   225
  case r1::rs2 => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   226
    val (r1s, f1) = simp(r1)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   227
    val (rs3, f2) = flats(rs2, seen + r1s)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   228
    (r1s::rs3, (v:Val) => v match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   229
      case Proj(0, vn) => Proj(0, f1(vn))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   230
      case Proj(n, vn) => IncProj(1, f2(Proj(n - 1, vn)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   231
    })
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   232
  }
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   233
}}
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   234
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   235
// simplification of regular expressions returning also an
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   236
// rectification function; no simplification under STAR 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   237
def simp(r: Rexp): (Rexp, Val => Val) = r match {
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   238
  case ALTS(rs) => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   239
    val (rs_s, fs_s) = flats(rs, Set())
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   240
    rs_s match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   241
      case Nil => (ZERO, F_ERROR) 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   242
      case r::Nil => (r, (v:Val) => fs_s(Proj(0, v)))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   243
      case rs_sd => (ALTS(rs_sd), fs_s)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   244
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   245
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   246
  case SEQ(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   247
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   248
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   249
    (r1s, r2s) match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   250
      case (ZERO, _) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   251
      case (_, ZERO) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   252
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   253
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   254
      case (ALTS(rs), r2s) => (ALTS(rs.map(SEQ(_, r2s))), 
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   255
			       (v:Val) => v match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   256
				 case Proj(n, Sequ(v1, v2)) => Sequ(f1s(Proj(n, v1)), f2s(v2))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   257
			       }
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   258
			      )
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   259
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   260
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   261
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   262
  case r => (r, F_ID)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   263
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   264
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   265
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   266
  case Nil => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   267
    //println(s"Size of the last regex: ${size(r)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   268
    //println(s"${string(r)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   269
    if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   270
  }
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   271
  case c::cs => {
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   272
    val rd = der(c, r)     
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   273
    val (r_simp, f_simp) = simp(rd)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   274
    //println(s"BEFORE ${string(rd)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   275
    //println(s"AFTER  ${string(r_simp)}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   276
    val rec = lex_simp(r_simp, cs)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   277
    inj(r, c, f_simp(rec))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
  }
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   281
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   282
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   283
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   284
lexing_simp("ab" | "aa", "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   285
lexing_simp("ab" | "aa", "aa")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   286
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   287
lexing(STAR("a" | "aa"), "aaaaa")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   288
lexing_simp(STAR("a" | "aa"), "aaaaa")
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   289
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   290
lexing(STAR("a" | "aa"), "aaaaaaaaaaa")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   291
lexing_simp(STAR("a" | "aa"), "aaaaaaaaaaa")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   292
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   293
lexing_simp(STAR("a" | "aa"), "a" * 2)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   294
lexing_simp(STAR("a" | "aa"), "a" * 3)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   295
lexing_simp(STAR("a" | "aa"), "a" * 4)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   296
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   297
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   298
lexing_simp(STAR("a" | "aa"), "a" * 20)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   299
lexing_simp(STAR("a" | "aa"), "a" * 2000)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   300
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   301
lexing(ALTS(List("aa", "aa", "aa", "ab", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   302
lexing_simp(ALTS(List("aa", "aa", "aa", "ab", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   303
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   304
lexing(ALTS(List(("aa" | "ab" | "aa"), "aa", "ab", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   305
lexing_simp(ALTS(List(("aa" | "ab" | "aa"), "aa", "ab", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   306
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   307
lexing(ALTS(List(ZERO, ZERO, ONE, "aa", ZERO, "aa", "aa")), "aa")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   308
lexing_simp(ALTS(List(ZERO, ZERO, ONE, "aa", ZERO, "aa", "aa")), "aa")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   309
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   310
lexing_simp(ONE | ZERO, "")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   311
lexing_simp(ZERO | ONE, "")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   312
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   313
lexing("a" | ZERO, "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   314
lexing_simp("a" | ZERO, "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   315
lexing(ZERO | "a", "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   316
lexing_simp(ZERO | "a", "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   317
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   318
lexing(ALTS(List(ZERO, ZERO, ONE, "a", ZERO, "a")), "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   319
lexing_simp(ALTS(List(ZERO, ZERO, ONE, "a", ZERO, "a")), "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   320
lexing(ALTS(List("a")), "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   321
lexing_simp(ALTS(List("a")), "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   322
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   323
lexing_simp(("a" | ZERO) | ZERO, "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   324
lexing_simp("a" | (ZERO | ZERO), "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   325
lexing_simp(ZERO | ("a" | ZERO), "a")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   326
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   327
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   328
lexing_simp("abc", "abc")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   329
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   330
lexing_simp("abc" | ONE, "abc")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   331
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   332
lexing(("a" | "ab") ~ ("b" | ""), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   333
lexing_simp(("a" | "ab") ~ ("b" | ""), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   334
lexing_simp(("ba" | "c" | "ab"), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   335
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   336
lexing(ALTS(List(ALTS(Nil), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   337
lexing_simp(ALTS(List(ALTS(Nil), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   338
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   339
lexing(ALTS(List(ALTS("ab"::Nil), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   340
lexing_simp(ALTS(List(ALTS("ab"::Nil), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   341
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   342
lexing(ALTS(List(ALTS(List("a","ab")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   343
lexing_simp(ALTS(List(ALTS(List("a","ab")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   344
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   345
lexing(ALTS(List(ALTS(List("ab","a")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   346
lexing_simp(ALTS(List(ALTS(List("ab","a")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   347
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   348
lexing(ALTS(List(ALTS(List("ab","a","a")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   349
lexing_simp(ALTS(List(ALTS(List("ab","a","a")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   350
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   351
lexing(ALTS(List(ALTS(List("a","ab","a")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   352
lexing_simp(ALTS(List(ALTS(List("a","ab","a")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   353
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   354
lexing(ALTS(List(ALTS(List("b","a","ab")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   355
lexing_simp(ALTS(List(ALTS(List("b","a","ab")), "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   356
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   357
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   358
lexing_simp(ALTS(List("ba", "c", "ab")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   359
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   360
lexing(ALTS(List("a", "ab", "a")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   361
lexing_simp(ALTS(List("a", "ab", "a")), "ab")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   362
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   363
lexing(STAR("a" | "aa"), "aa")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   364
lexing_simp(STAR("a" | "aa"), "aa")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   365
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   366
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   367
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   368
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   369
def enum(n: Int, s: String) : Set[Rexp] = n match {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   370
  case 0 => Set(ZERO, ONE) ++ s.toSet.map(CHAR)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   371
  case n => {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   372
    val rs = enum(n - 1, s)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   373
    rs ++
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   374
    (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) ++
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   375
    (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) ++
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   376
    (for (r1 <- rs) yield STAR(r1))
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   377
  }
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   378
}
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   379
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   380
def strs(n: Int, cs: String) : Set[String] = {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   381
  if (n == 0) Set("")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   382
  else {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   383
    val ss = strs(n - 1, cs)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   384
    ss ++
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   385
    (for (s <- ss; c <- cs.toList) yield c + s)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   386
  }
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   387
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   388
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   389
import scala.util.Try
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   390
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   391
def tests(n: Int, m: Int, s: String) = {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   392
  val rs = enum(n, s)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   393
  val ss = strs(m, s)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   394
  println(s"cases generated: ${rs.size} regexes and ${ss.size} strings")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   395
  for (r1 <- rs.par; s1 <- ss.par) yield {
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   396
    val res1 = Try(Some(lexing(r1, s1))).getOrElse(None)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   397
    val res2 = Try(Some(lexing_simp(r1, s1))).getOrElse(None)
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   398
    if (res1 != res2) println(s"Disagree on ${r1} and ${s1}")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   399
    if (res1 != res2) Some((r1, s1)) else None
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   400
  }
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   401
}
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   402
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   403
println("Testing")
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   404
println(tests(2,7,"abc"))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   405
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   406
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   407