main_solution3/re.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 13:53:37 +0000
changeset 474 b528d1d3d3c3
parent 470 86a456f8cb92
child 475 59e005dcf163
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
     1
// Main Part 3 about Regular Expression Matching
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     2
//==============================================
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     4
object M3 {
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// Regular Expressions
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
abstract class Rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
case object ZERO extends Rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case object ONE extends Rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    11
case class ALTs(rs: List[Rexp]) extends Rexp  // alternatives 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    12
case class SEQs(rs: List[Rexp]) extends Rexp  // sequences
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    13
case class STAR(r: Rexp) extends Rexp         // star
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    15
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    16
//the usual binary choice and binary sequence can be defined 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    17
//in terms of ALTs and SEQs
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    18
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    19
def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    20
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
    21
// some convenience for typing in regular expressions
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
import scala.language.implicitConversions    
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
import scala.language.reflectiveCalls 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
def charlist2rexp(s: List[Char]): Rexp = s match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  case Nil => ONE
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  case c::Nil => CHAR(c)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
implicit def RexpOps (r: Rexp) = new {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  def | (s: Rexp) = ALT(r, s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  def % = STAR(r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  def ~ (s: Rexp) = SEQ(r, s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
implicit def stringOps (s: String) = new {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  def | (r: Rexp) = ALT(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  def | (r: String) = ALT(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  def % = STAR(s)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  def ~ (r: Rexp) = SEQ(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  def ~ (r: String) = SEQ(s, r)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    46
// (1) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
def nullable (r: Rexp) : Boolean = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  case ZERO => false
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  case ONE => true
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  case CHAR(_) => false
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    51
  case ALTs(rs) => rs.exists(nullable)
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    52
  case SEQs(rs) => rs.forall(nullable)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
  case STAR(_) => true
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    56
// (2) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    57
def der(c: Char, r: Rexp) : Rexp = r match {
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case ZERO => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case ONE => ZERO
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  case CHAR(d) => if (c == d) ONE else ZERO
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    61
  case ALTs(rs) => ALTs(rs.map(der(c, _)))
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    62
  case SEQs(Nil) => ZERO
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    63
  case SEQs(r1::rs) => 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    64
    if (nullable(r1)) ALT(SEQs(der(c, r1)::rs), der(c, SEQs(rs)))
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 456
diff changeset
    65
    else SEQs(der(c, r1) :: rs)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    69
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    70
// (3) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    71
def denest(rs: List[Rexp]) : List[Rexp] = rs match {
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    72
  case Nil => Nil
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    73
  case ZERO::tl => denest(tl)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    74
  case ALTs(rs1)::rs2 => rs1 ::: denest(rs2)  
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    75
  case r::rs => r :: denest(rs) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    76
}
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    77
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    78
// (4)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    79
def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = rs match {
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    80
  case Nil => acc
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    81
  case ZERO::rs => ZERO::Nil
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    82
  case ONE::rs => flts(rs, acc)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    83
  case SEQs(rs1)::rs => flts(rs, acc ::: rs1)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    84
  case r::rs => flts(rs, acc :+ r) 
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    85
}
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    86
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    87
// (5)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    88
def ALTs_smart(rs: List[Rexp]) : Rexp = rs match {
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    89
  case Nil => ZERO
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    90
  case r::Nil => r  
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    91
  case rs => ALTs(rs)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    92
}
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    94
def SEQs_smart(rs: List[Rexp]) : Rexp = rs match {
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    95
  case Nil => ONE
456
d076cb2e0b75 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    96
  case ZERO::Nil => ZERO
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    97
  case r::Nil => r
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    98
  case rs => SEQs(rs) 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    99
}
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   100
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   101
// (6) 
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   102
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
def simp(r: Rexp) : Rexp = r match {
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   104
  case ALTs(rs) => 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   105
    ALTs_smart(denest(rs.map(simp)).distinct)
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   106
  case SEQs(rs) => 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   107
    SEQs_smart(flts(rs.map(simp)))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case r => r
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   111
//println("Simp tests")
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   112
//println(simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)))
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   113
//println(simp(((CHAR('a') | ZERO) ~ ONE) | 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   114
//              (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   116
// (7) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
def ders (s: List[Char], r: Rexp) : Rexp = s match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case Nil => r
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case c::s => ders(s, simp(der(c, r)))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
// main matcher function
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   126
// (8) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
def size(r: Rexp): Int = r match {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case ZERO => 1
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case ONE => 1
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case CHAR(_) => 1
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   132
  case ALTs(rs) => 1 + rs.map(size).sum
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   133
  case SEQs(rs) => 1 + rs.map(size).sum
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  case STAR(r1) => 1 + size(r1)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
// some testing data
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   140
/*
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   141
println(matcher(("a" ~ "b") ~ "c", "abc"))  // => true
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   142
println(matcher(("a" ~ "b") ~ "c", "ab"))   // => false
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
// the supposedly 'evil' regular expression (a*)* b
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   145
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   147
println(matcher(EVIL, "a" * 1000 ++ "b"))   // => true
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   148
println(matcher(EVIL, "a" * 1000))          // => false
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
// size without simplifications
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   151
println(size(der('a', der('a', EVIL))))             // => 36
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   152
println(size(der('a', der('a', der('a', EVIL)))))   // => 83
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
// size with simplification
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   155
println(simp(der('a', der('a', EVIL))))          
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   156
println(simp(der('a', der('a', der('a', EVIL)))))
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   157
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   158
println(size(simp(der('a', der('a', EVIL)))))           // => 7
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   159
println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 7
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
// Python needs around 30 seconds for matching 28 a's with EVIL. 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
// Java 9 and later increase this to an "astonishing" 40000 a's in
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
// around 30 seconds.
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
//
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
// Lets see how long it takes to match strings with 
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
// 5 Million a's...it should be in the range of a 
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   167
// few seconds.
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
def time_needed[T](i: Int, code: => T) = {
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
  val start = System.nanoTime()
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
  for (j <- 1 to i) code
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
  val end = System.nanoTime()
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
   173
  "%.5f".format((end - start)/(i * 1.0e9))
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
}
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   176
for (i <- 0 to 5000000 by 500000) {
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   177
  println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   178
}
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
// another "power" test case 
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   181
println(simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next()) == ONE)
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
// the Iterator produces the rexp
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
//
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
//
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   187
//    where SEQ is nested 100 times.
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   188
*/ 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
}
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   192
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   193
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   194
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   195
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   196
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   197
/*
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   198
// if nullable(r1)  
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   199
  ALTs(SEQs(der(c, r1)::rs)::(rs filter what is nullable) .map(der(c,_)))
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 428
diff changeset
   200
456
d076cb2e0b75 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   201
*/