testing4/re.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 01 Dec 2018 15:09:37 +0000
changeset 228 33c2655be47d
parent 221 9e7897f25e13
child 229 5549016ab10f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about Regular Expression Matching
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
     4
// Regular Expressions
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
abstract class Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case object ZERO extends Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case object ONE extends Rexp
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    11
case class STAR(r: Rexp) extends Rexp             // star
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    12
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    14
// some convenience for typing regular expressions
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    16
import scala.language.implicitConversions
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    17
import scala.language.reflectiveCalls
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def charlist2rexp(s: List[Char]): Rexp = s match {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case Nil => ONE
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  case c::Nil => CHAR(c)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
implicit def RexpOps (r: Rexp) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  def | (s: Rexp) = ALT(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  def % = STAR(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  def ~ (s: Rexp) = SEQ(r, s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
}
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 stringOps (s: String) = new {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  def | (r: Rexp) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  def | (r: String) = ALT(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def % = STAR(s)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  def ~ (r: Rexp) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  def ~ (r: String) = SEQ(s, r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    40
// (1) Complete the function nullable according to
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    41
// the definition given in the coursework; this
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
// function checks whether a regular expression
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    43
// can match the empty string and Returns a boolean
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    44
// accordingly.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    46
def nullable (r: Rexp) : Boolean = r match{
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  case ZERO => false
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  case ONE => true
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  case CHAR(_) => false
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    50
  case ALT(a,b)=>nullable(a)||nullable(b)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    51
  case SEQ(a,b) => nullable(a) && nullable(b)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  case STAR(_) => true
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    55
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    56
/*val rex = "1~0.%|11"
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    57
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    58
assert(der('1',rex) == SEQ(ONE,SEQ(CHAR(~),SEQ(CHAR(0),SEQ(CHAR(.),SEQ(CHAR(%),SEQ(CHAR(|),SEQ(CHAR(1),CHAR(1)))))))))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    59
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    60
assert(der('1',der('1',rex)) ==
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    61
        ALT(SEQ(ZERO,SEQ(CHAR(~),SEQ(CHAR(0),SEQ(CHAR(.),SEQ(CHAR(%),SEQ(CHAR(|),
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    62
        SEQ(CHAR(1),CHAR(1)))))))),SEQ(ZERO,SEQ(CHAR(0),SEQ(CHAR(.),SEQ(CHAR(%),
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    63
        SEQ(CHAR(|),SEQ(CHAR(1),CHAR(1))))))))*/
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    64
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    65
// (2) Complete the function der according to
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// the definition given in the coursework; this
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    67
// function calculates the derivative of a
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    68
// regular expression w.r.t. a character.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    70
def der (c: Char, r: Rexp) : Rexp = r match{
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  case ZERO => ZERO
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  case ONE => ZERO
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    73
  case CHAR(d) => if (c==d) ONE else ZERO
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    74
  case ALT(a,b) => der(c,a)|der(c,b)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    75
  case SEQ(a,b) => if(nullable(a)) {(der(c,a)~b)|der(c,b)}
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    76
                   else der(c,a)~b
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    77
  case STAR(a) => der(c,a)~STAR(a)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    80
println(der('a', ZERO | ONE))// == (ZERO | ZERO)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    81
println(der('a', (CHAR('a') | ONE) ~ CHAR('a')))// ==ALT((ONE | ZERO) ~ CHAR('a'), ONE)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    82
println(der('a', STAR(CHAR('a'))))// == (ONE ~ STAR(CHAR('a')))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    83
println(der('b', STAR(CHAR('a'))))// == (ZERO ~ STAR(CHAR('a'))))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    84
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    85
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    86
//ALT(SEQ(ZERO,ZERO),ZERO)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    87
//ALT(ALT(ZERO,ZERO),ALT(ZERO,ZERO))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    88
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    89
// * == |
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    90
// + == ~
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    91
// (3) Complete the simp function according to
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// the specification given in the coursework; this
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    93
// function simplifies a regular expression from
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    94
// the inside out, like you would simplify arithmetic
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    95
// expressions; however it does not simplify inside
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
    96
// STAR-regular expressions.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    98
/*
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
    99
def simp(r: Rexp) : Rexp = r match{
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   100
  case SEQ(ZERO,_) => ZERO
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   101
  case SEQ(_,ZERO) => ZERO
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   102
  case SEQ(ONE,a) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   103
  case SEQ(a,ONE) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   104
  case ALT(ZERO,a) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   105
  case ALT(a,ZERO) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   106
  case ALT(a,b) => if(a == b) simp(a) else r
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   107
  case _ => r
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   108
}*/
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   109
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   110
def simp(r: Rexp) : Rexp = r match{
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   111
  case SEQ(a,b) =>{ val sa = simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   112
                    val sb = simp(b)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   113
                    if(sa == ZERO || sb == ZERO) ZERO
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   114
                    else if(sa == ONE) sb
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   115
                    else if(sb == ONE) sa
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   116
                    else SEQ(sa,sb)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   117
                    }
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   118
  case ALT(a,b) =>{ val sa = simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   119
                    val sb = simp(b)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   120
                    if(sa == ONE || sb == ONE) ONE
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   121
                    else if(sa == ZERO) sb
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   122
                    else if(sb == ZERO) sa
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   123
                    else if(sa == sb) sa
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   124
                    else ALT(sa,sb)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   125
                    }
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   126
  //case STAR(STAR(a)) => simp(STAR(a))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   127
  //case STAR(a) => STAR(simp(a))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   128
  case _ => r
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   129
  /*
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   130
  case SEQ(ZERO,_) => ZERO
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   131
  case SEQ(_,ZERO) => ZERO
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   132
  case SEQ(ONE,a) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   133
  case SEQ(a,ONE) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   134
  case SEQ(a,b) => SEQ(simp(a),simp(b))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   135
  //case ALT(ZERO,a) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   136
  case ALT(a,ZERO) => simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   137
  case ALT(ONE,_) => ONE
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   138
  case ALT(_,ONE) => ONE
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   139
  case ALT(a,b) => {val sa = simp(a)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   140
                    if(sa == simp(b)) sa else r
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   141
                    }
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   142
  case STAR(STAR(a)) => simp(STAR(a))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   143
  case STAR(a) => STAR(simp(a))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   144
  case _ => r*/
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   147
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   148
/*val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   149
println("TEST: " + simp(der('a', der('a', EVIL))))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   150
println(simp(ONE))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   151
val r1 = ALT(ZERO,ONE)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   152
val r2 = SEQ(ONE,ZERO)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   153
val r3 = SEQ(r1,SEQ(r2,r1))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   154
println("R1 = " + simp(r1))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   155
println(simp(r2))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   156
println(simp(r3))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   157
*/
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   158
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   159
// (4) Complete the two functions below; the first
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
// calculates the derivative w.r.t. a string; the second
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
// is the regular expression matcher taking a regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
// expression and a string and checks whether the
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   163
// string matches the regular expression
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   165
def ders (s: List[Char], r: Rexp ="") : Rexp = s match{
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  case Nil => r
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   167
  case a::z => ders(z,simp(der(a,r)))
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   170
def matcher(r: Rexp, s: String): Boolean = {
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   171
  val derivatives = simp(ders(s.toList,r))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   172
  nullable(derivatives)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   173
}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   175
// (5) Complete the size function for regular
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   176
// expressions according to the specification
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
// given in the coursework.
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   179
def size(r: Rexp): Int = r match{
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
  case ZERO => 1
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  case ONE => 1
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  case CHAR(_) => 1
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   183
  case SEQ(a,b) => 1 + size(a) + size(b)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   184
  case ALT(a,b) => 1 + size(a) + size(b)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   185
  case STAR(a) => 1 + size(a)
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
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   188
println(der('a', ZERO | ONE))// == (ZERO | ZERO)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   189
println(der('a', (CHAR('a') | ONE) ~ CHAR('a')))// ==ALT((ONE | ZERO) ~ CHAR('a'), ONE)
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   190
println(der('a', STAR(CHAR('a'))))// == (ONE ~ STAR(CHAR('a')))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   191
println(der('b', STAR(CHAR('a'))))// == (ZERO ~ STAR(CHAR('a'))))
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
// some testing data
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
/*
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   194
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   195
assert(matcher(("a" ~ "b") ~ "c", "abc") == true)  // => true
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   196
assert(matcher(("a" ~ "b") ~ "c", "ab") == false)   // => false
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   197
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
// the supposedly 'evil' regular expression (a*)* b
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   200
//val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   201
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   203
assert(matcher(EVIL, "a" * 1000 ++ "b") == true) // => true
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   204
assert(matcher(EVIL, "a" * 1000) == false)          // => false
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
// size without simplifications
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   207
assert("28 " + size(der('a', der('a', EVIL)))             ==28)// => 28
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   208
assert("58 " + size(der('a', der('a', der('a', EVIL))))   ==58)// => 58
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
// size with simplification
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   211
assert("8 " + size(simp(der('a', der('a', EVIL))))           ==8)// => 8
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   212
assert("8 " + size(simp(der('a', der('a', der('a', EVIL))))) ==8) // => 8
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   214
*/
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   215
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   216
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   217
/*
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   218
// 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
   219
// Java 9 and later increase this to an "astonishing" 40000 a's in
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   220
// 30 seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
//
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   222
// Lets see how long it really takes to match strings with
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   223
// 5 Million a's...it should be in the range of a couple
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   224
// of seconds.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
def time_needed[T](i: Int, code: => T) = {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
  val start = System.nanoTime()
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
  for (j <- 1 to i) code
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
  val end = System.nanoTime()
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
  (end - start)/(i * 1.0e9)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
for (i <- 0 to 5000000 by 500000) {
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
}
221
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   236
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   237
// another "power" test case
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   238
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
   239
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   240
// the Iterator produces the rexp
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   241
//
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   242
//      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
9e7897f25e13 updated
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   243
//
228
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   244
//    where SEQ is nested 50 times.
33c2655be47d updated
Christian Urban <urbanc@in.tum.de>
parents: 221
diff changeset
   245
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
*/