regexp.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 01 Nov 2012 10:04:30 +0000
changeset 55 cceed8d66b28
parent 29 774007c4b1b3
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
     1
// regular expressions
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
abstract class Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
case object NULL extends Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case object EMPTY extends Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case class CHAR(c: Char) extends Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class STAR(r: Rexp) extends Rexp
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    11
// some convenience for typing in regular expressions
29
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    12
def charlist2rexp(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    13
  case Nil => EMPTY
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    14
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    15
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    16
}
29
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    17
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    18
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    19
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    20
// for example
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    21
println(STAR("abc"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    23
// produces STAR(SEQ(CHAR(a),SEQ(CHAR(b),SEQ(CHAR(c),EMPTY))))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    24
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    25
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    26
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    27
// a simple-minded regular expression matcher:
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    28
// it loops for examples like STAR(EMPTY) with
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    29
// strings this regular expression does not match
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    30
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    31
def smatchers(rs: List[Rexp], s: List[Char]) : Boolean = (rs, s) match {
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    32
  case (NULL::rs, s) => false
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    33
  case (EMPTY::rs, s) => smatchers(rs, s)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    34
  case (CHAR(c)::rs, Nil) => false
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    35
  case (CHAR(c)::rs, d::s) => (c ==d) && smatchers(rs, s) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    36
  case (ALT(r1, r2)::rs, s) => smatchers(r1::rs, s) || smatchers(r2::rs, s)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    37
  case (SEQ(r1, r2)::rs, s) => smatchers(r1::r2::rs, s) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    38
  case (STAR(r)::rs, s) => smatchers(rs, s) || smatchers(r::STAR(r)::rs, s) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    39
  case (Nil, s) => s == Nil
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    40
}
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    41
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    42
def smatcher(r: Rexp, s: String) = smatchers(List(r), s.toList)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    43
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    44
// regular expression: a
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    45
println(smatcher(CHAR('a'), "ab")) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    47
// regular expression: a + (b o c)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    48
println(smatcher(ALT(CHAR('a'), SEQ(CHAR('b'), CHAR('c'))), "ab")) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    49
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    50
// regular expression: a + (b o c)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    51
println(smatcher(ALT(CHAR('a'), SEQ(CHAR('b'), CHAR('c'))), "bc")) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    52
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    53
// loops for regular expression epsilon* 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    54
//println(smatcher(STAR(EMPTY), "a"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    55
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    56
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    57
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    58
// Regular expression matcher that works properly
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    59
//================================================
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    60
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    61
// nullable function: tests whether the regular 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    62
// expression can recognise the empty string
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
def nullable (r: Rexp) : Boolean = r match {
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case NULL => false
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case EMPTY => true
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  case CHAR(_) => false
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  case STAR(_) => true
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    72
// derivative of a regular expression w.r.t. a character
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    73
def der (c: Char, r: Rexp) : Rexp = r match {
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case NULL => NULL
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  case EMPTY => NULL
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  case CHAR(d) => if (c == d) EMPTY else NULL
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    77
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  case SEQ(r1, r2) => 
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    79
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    80
    else SEQ(der(c, r1), r2)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    81
  case STAR(r) => SEQ(der(c, r), STAR(r))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    84
// derivative w.r.t. a string (iterates der)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    85
def ders (s: List[Char], r: Rexp) : Rexp = s match {
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  case Nil => r
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    87
  case c::s => ders(s, der(c, r))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    90
// main matcher function
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    91
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    94
//examples
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    96
println(matcher(SEQ(STAR("a"), STAR("b")), "bbaaa"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    97
println(matcher(ALT(STAR("a"), STAR("b")), ""))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    98
println(matcher("abc", ""))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    99
println(matcher(STAR(ALT(EMPTY, "a")), ""))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
   100
println(matcher(STAR(EMPTY), "a"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
   101
println(matcher("cab","cab"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
   102
println(matcher(STAR("a"),"aaa"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
   103
println(matcher("cab" ,"cab"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
   104
println(matcher(STAR("a"),"aaa"))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
29
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   106