regexp.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 04 Oct 2012 09:07:33 +0100
changeset 18 d48cfc286cb1
parent 7 73cf4406b773
child 29 774007c4b1b3
permissions -rw-r--r--
added
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
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    12
implicit def string2rexp(s : String) : Rexp = {
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    13
  s.foldRight (EMPTY: Rexp) ( (c, r) => SEQ(CHAR(c), r) )
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    14
}
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    15
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    16
// for example
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    17
println(STAR("abc"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    19
// produces STAR(SEQ(CHAR(a),SEQ(CHAR(b),SEQ(CHAR(c),EMPTY))))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    20
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    21
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    23
// a simple-minded regular expression matcher:
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    24
// it loops for examples like STAR(EMPTY) with
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    25
// strings this regular expression does not match
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    26
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    27
def smatchers(rs: List[Rexp], s: List[Char]) : Boolean = (rs, s) match {
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    28
  case (NULL::rs, s) => false
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    29
  case (EMPTY::rs, s) => smatchers(rs, s)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    30
  case (CHAR(c)::rs, Nil) => false
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    31
  case (CHAR(c)::rs, d::s) => (c ==d) && smatchers(rs, s) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    32
  case (ALT(r1, r2)::rs, s) => smatchers(r1::rs, s) || smatchers(r2::rs, s)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    33
  case (SEQ(r1, r2)::rs, s) => smatchers(r1::r2::rs, s) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    34
  case (STAR(r)::rs, s) => smatchers(rs, s) || smatchers(r::STAR(r)::rs, s) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    35
  case (Nil, s) => s == Nil
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    36
}
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    37
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    38
def smatcher(r: Rexp, s: String) = smatchers(List(r), s.toList)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    39
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    40
// regular expression: a
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    41
println(smatcher(CHAR('a'), "ab")) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    42
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    43
// regular expression: a + (b o c)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    44
println(smatcher(ALT(CHAR('a'), SEQ(CHAR('b'), CHAR('c'))), "ab")) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    46
// regular expression: a + (b o c)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    47
println(smatcher(ALT(CHAR('a'), SEQ(CHAR('b'), CHAR('c'))), "bc")) 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    48
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    49
// loops for regular expression epsilon* 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    50
//println(smatcher(STAR(EMPTY), "a"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    51
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    52
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    54
// Regular expression matcher that works properly
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
// nullable function: tests whether the regular 
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    58
// expression can recognise the empty string
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def nullable (r: Rexp) : Boolean = r match {
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  case NULL => false
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  case EMPTY => true
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  case CHAR(_) => false
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case STAR(_) => true
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    68
// derivative of a regular expression w.r.t. a character
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    69
def der (c: Char, r: Rexp) : Rexp = r match {
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  case NULL => NULL
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  case EMPTY => NULL
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  case CHAR(d) => if (c == d) EMPTY else NULL
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    73
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case SEQ(r1, r2) => 
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    75
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    76
    else SEQ(der(c, r1), r2)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    77
  case STAR(r) => SEQ(der(c, r), STAR(r))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    80
// derivative w.r.t. a string (iterates der)
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    81
def ders (s: List[Char], r: Rexp) : Rexp = s match {
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  case Nil => r
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    83
  case c::s => ders(s, der(c, r))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    86
// main matcher function
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    87
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, 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
//examples
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
18
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    92
println(matcher(SEQ(STAR("a"), STAR("b")), "bbaaa"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    93
println(matcher(ALT(STAR("a"), STAR("b")), ""))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    94
println(matcher("abc", ""))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    95
println(matcher(STAR(ALT(EMPTY, "a")), ""))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    96
println(matcher(STAR(EMPTY), "a"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    97
println(matcher("cab","cab"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    98
println(matcher(STAR("a"),"aaa"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
    99
println(matcher("cab" ,"cab"))
Christian Urban <urbanc@in.tum.de>
parents: 7
diff changeset
   100
println(matcher(STAR("a"),"aaa"))
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101