main_testing3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 457 9cf317975ae7
child 475 59e005dcf163
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
     1
// Main Part 3 about Regular Expression Matching
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     2
//==============================================
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     4
object M3 {
249
1997cfcd6334 updated
Christian Urban <urbanc@in.tum.de>
parents: 236
diff changeset
     5
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
abstract class Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case object ZERO extends Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case object ONE extends Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class CHAR(c: Char) extends Rexp
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    10
case class ALTs(rs: List[Rexp]) extends Rexp  // alternatives 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    11
case class SEQs(rs: List[Rexp]) extends Rexp  // sequences
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    12
case class STAR(r: Rexp) extends Rexp         // star
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    15
//the usual binary choice and binary sequence can be defined 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    16
//in terms of ALTs and SEQs
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    17
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    18
def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    19
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    20
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    21
// some convenience for typing regular expressions
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    22
import scala.language.implicitConversions    
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    23
import scala.language.reflectiveCalls 
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
    24
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def charlist2rexp(s: List[Char]): Rexp = s match {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case Nil => ONE
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case c::Nil => CHAR(c)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
implicit def RexpOps (r: Rexp) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  def | (s: Rexp) = ALT(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  def % = STAR(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def ~ (s: Rexp) = SEQ(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
implicit def stringOps (s: String) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  def | (r: Rexp) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  def | (r: String) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  def % = STAR(s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  def ~ (r: Rexp) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  def ~ (r: String) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    46
// examples for the implicits:
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    47
// ALT(CHAR('a'), CHAR('b'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    48
// val areg : Rexp = "a" | "b"
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    49
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    50
// SEQ(CHAR('a'), CHAR('b')) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    51
// val sreg : Rexp = "a" ~ "b"
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    52
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    53
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    54
// ADD YOUR CODE BELOW
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    55
//======================
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    56
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    57
// (1)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
    58
def nullable (r: Rexp) : Boolean = r match {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    59
  case ZERO     => false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    60
  case ONE      => true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    61
  case CHAR(_)  => false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    62
  case ALTs(rs) => (for(reg <- rs) yield nullable(reg)).exists(_ == true)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    63
  case SEQs(rs) => (for(reg <- rs) yield nullable(reg)).forall(_ == true)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    64
  case STAR(_)  => true
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    67
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    68
nullable(ZERO) == false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    69
nullable(ONE) == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    70
nullable(CHAR('a')) == false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    71
nullable(ZERO | ONE) == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    72
nullable(ZERO | CHAR('a')) == false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    73
nullable(ONE ~ ONE) == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    74
nullable(ONE ~ CHAR('a')) == false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    75
nullable(STAR(ZERO)) == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    76
nullable(ALTs(List(ONE, CHAR('a'), ZERO))) == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    77
nullable(SEQs(List(ONE, ALTs(List(ONE, CHAR('a'), ZERO)), STAR(ZERO)))) == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    78
*/
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    79
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    80
// (2) 
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    81
def der (c: Char, r: Rexp) : Rexp = r match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    82
  case ZERO           => ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    83
  case ONE            => ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    84
  case CHAR(d)        => if(c == d) ONE else ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    85
  case ALTs(rs)       => ALTs(for(reg <- rs) yield der(c, reg))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    86
  case SEQs(Nil)      => ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    87
  case SEQs(r :: rs)  => if(nullable(r)) ALT(SEQs(der(c, r) :: rs), der(c, SEQs(rs))) else SEQs(der(c, r) :: rs)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    88
  case STAR(r)        => SEQ(der(c,r), STAR(r))
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    89
}
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    90
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    91
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    92
der('a', ZERO | ONE) == (ZERO | ZERO)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    93
der('a', (CHAR('a') | ONE) ~ CHAR('a')) == ALT((ONE | ZERO) ~ CHAR('a'), SEQs(List(ONE)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    94
der('a', (CHAR('a') | CHAR('a')) ~ CHAR('a')) == (ONE | ONE) ~ CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    95
der('a', STAR(CHAR('a'))) == (ONE ~ STAR(CHAR('a')))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    96
der('b', STAR(CHAR('a'))) == (ZERO ~ STAR(CHAR('a')))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
    97
*/
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
    98
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    99
// (3) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   100
def denest(rs: List[Rexp]) : List[Rexp] = rs match {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   101
  case Nil                => Nil
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   102
  case ZERO :: rest       => denest(rest)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   103
  case ALTs(rgs) :: rest  => rgs ::: denest(rest)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   104
  case r :: rest          => r :: denest(rest)
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   105
}
420
4edc1a308652 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 403
diff changeset
   106
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   107
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   108
denest(List(ONE, ZERO, ALTs(List(ONE, CHAR('a'))))) == List(ONE, ONE, CHAR('a'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   109
denest(List(ONE ~ ONE, ZERO, ZERO | ONE)) == List(ONE ~ ONE, ZERO, ONE)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   110
*/
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   111
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   112
// (4)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   113
def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = rs match {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   114
  case Nil                => acc
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   115
  case ZERO :: rest       => List(ZERO)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   116
  case ONE :: rest        => flts(rest, acc)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   117
  case SEQs(rgs) :: rest  => flts(rest, acc ::: rgs)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   118
  case r :: rest          => flts(rest, acc ::: List(r)) 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   121
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   122
flts(List(CHAR('a'), ZERO, ONE), Nil) == List(ZERO)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   123
flts(List(CHAR('a'), ONE, ONE, CHAR('b')), Nil) == List(CHAR('a'), CHAR('b'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   124
flts(List(ONE ~ CHAR('a'), CHAR('b') ~ ONE), Nil) == List(ONE, CHAR('a'), CHAR('b'), ONE)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   125
*/
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   126
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   127
// (5)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   128
def ALTs_smart(rs: List[Rexp]) : Rexp = rs match {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   129
  case Nil      => ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   130
  case List(r)  => r
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   131
  case _        => ALTs(rs)
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   132
}
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   133
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   134
def SEQs_smart(rs: List[Rexp]) : Rexp = rs match {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   135
  case Nil      => ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   136
  case List(r)  => r
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   137
  case _        => SEQs(rs)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   138
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   139
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   140
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   141
SEQs_smart(Nil) == ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   142
SEQs_smart(List(ZERO)) == ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   143
SEQs_smart(List(CHAR('a'))) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   144
SEQs_smart(List(ONE ~ ONE)) == ONE ~ ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   145
SEQs_smart(List(ONE, ONE)) == SEQs(List(ONE, ONE))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   146
ALTs_smart(Nil) == ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   147
ALTs_smart(List(ONE ~ ONE)) == ONE ~ ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   148
ALTs_smart(List(ZERO, ZERO)) == ALTs(List(ZERO, ZERO))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   149
*/
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   150
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   151
// (6)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   152
def simp(r: Rexp) : Rexp = r match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   153
  case ALTs(rs) => ALTs_smart(denest(for(reg <- rs) yield simp(reg)).distinct)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   154
  case SEQs(rs) => SEQs_smart(flts(for(reg <- rs) yield simp(reg)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   155
  case _        => r
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   156
}
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   157
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   158
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   159
simp(ZERO | ONE) == ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   160
simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   161
simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   162
simp(((ONE ~ ONE) ~ ONE) ~ CHAR('a')) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   163
simp(((ONE | ONE) ~ ONE) ~ CHAR('a')) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   164
simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   165
simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   166
simp(CHAR('a') | CHAR('a')) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   167
simp(CHAR('a') ~ CHAR('a')) == CHAR('a') ~ CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   168
simp(ONE | CHAR('a')) == (ONE | CHAR('a'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   169
simp(ALT((CHAR('a') | ZERO) ~ ONE,((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   170
simp((ZERO | ((ZERO | ZERO) | (ZERO | ZERO))) ~ ((ONE | ZERO) | ONE ) ~ (CHAR('a'))) == ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   171
simp(ALT(ONE | ONE, ONE | ONE)) == ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   172
simp(ALT(ZERO | CHAR('a'), CHAR('a') | ZERO)) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   173
simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) == ALT(ONE, CHAR('a'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   174
simp(ALTs(Nil)) == ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   175
simp(SEQs(List(CHAR('a')))) == CHAR('a')
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   176
*/
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   177
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   178
// (7)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   179
def ders (s: List[Char], r: Rexp) : Rexp = s match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   180
  case Nil      => r
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   181
  case c :: cs  => ders(cs, simp(der(c, r)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   182
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   183
def matcher(r: Rexp, s: String): Boolean = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   184
  val derivatives = ders(s.toList, r)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   185
  nullable(derivatives)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   188
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   189
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   190
ders("aaaaa".toList, EVIL) == SEQs(List(STAR(CHAR('a')), STAR(STAR(CHAR('a'))), CHAR('b')))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   191
ders(List('b'), EVIL) == ONE
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   192
ders("bb".toList, EVIL) == ZERO
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   193
matcher(EVIL, "a" * 5 ++ "b") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   194
matcher(EVIL, "b") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   195
matcher(EVIL, "bb") == false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   196
matcher("abc", "abc") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   197
matcher(("ab" | "a") ~ (ONE | "bc"), "abc") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   198
matcher(ONE, "") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   199
matcher(ZERO, "") == false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   200
matcher(ONE | CHAR('a'), "") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   201
matcher(ONE | CHAR('a'), "a") == true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   202
*/
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   203
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   204
// (8) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   205
def size(r: Rexp): Int = r match {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   206
  case ZERO     => 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   207
  case ONE      => 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   208
  case CHAR(_)  => 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   209
  case ALTs(rs) => 1 + (for(reg <- rs) yield size(reg)).sum
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   210
  case SEQs(rs) => 1 + (for(reg <- rs) yield size(reg)).sum
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   211
  case STAR(r)  => 1 + size(r)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   212
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   213
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   214
/*
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   215
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   216
size(der('a', der('a', EVIL))) == 36
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   217
size(der('a', der('a', der('a', EVIL)))) == 83
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   218
size(ders("aaaaaa".toList, EVIL)) == 7
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   219
size(ders(("a" * 50).toList, EVIL)) == 7
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   220
*/
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   221
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   222
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   223
// Some testing data
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   224
//===================
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   225
/*
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   226
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   227
simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE))   // => ALTs(List(ONE, CHAR(a)))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   228
simp(((CHAR('a') | ZERO) ~ ONE) | (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO)))   // => CHAR(a)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   230
matcher(("a" ~ "b") ~ "c", "ab")   // => false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   231
matcher(("a" ~ "b") ~ "c", "abc")  // => true
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   232
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   233
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   234
// the supposedly 'evil' regular expression (a*)* b
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   235
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   236
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   237
matcher(EVIL, "a" * 1000)          // => false
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   238
matcher(EVIL, "a" * 1000 ++ "b")   // => true
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   239
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
// size without simplifications
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   242
size(der('a', der('a', EVIL)))             // => 36
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   243
size(der('a', der('a', der('a', EVIL))))   // => 83
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
// size with simplification
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   246
size(simp(der('a', der('a', EVIL))))           // => 7
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   247
size(simp(der('a', der('a', der('a', EVIL))))) // => 7
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   248
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   249
// Python needs around 30 seconds for matching 28 a's with EVIL. 
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   250
// Java 9 and later increase this to an "astonishing" 40000 a's in
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   251
// 30 seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
//
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   253
// Lets see how long it really takes to match strings with 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   254
// 5 Million a's...it should be in the range of a few
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   255
// of seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   257
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   258
  val start = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   259
  for (j <- 1 to i) code
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   260
  val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   261
  "%.5f".format((end - start)/(i * 1.0e9))
Christian Urban <christian.urban@kcl.ac.uk>
parents: 420
diff changeset
   262
}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   264
for (i <- 0 to 5000000 by 500000) {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   265
  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   266
}
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   267
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
   268
// another "power" test case 
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   269
simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next()) == ONE
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   270
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   271
// the Iterator produces the rexp
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   272
//
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   273
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   274
//
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   275
//    where SEQ is nested 50 times.
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   276
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   277
*/
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
   278
300
72688efdf17c updated testing files
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   279
}
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   280
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   281
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   282
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   283
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   284
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   285
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   286
// This template code is subject to copyright 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   287
// by King's College London, 2022. Do not 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   288
// make the template code public in any shape 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   289
// or form, and do not exchange it with other 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 457
diff changeset
   290
// students under any circumstance.