progs/matcher/re1.sc
changeset 725 f345e89895f5
child 745 7dc3643a0cc5
equal deleted inserted replaced
724:61a936be50c4 725:f345e89895f5
       
     1 // A simple matcher for basic regular expressions
       
     2 //
       
     3 // Call the test cases with X = {1,2,3}
       
     4 //
       
     5 //   amm re1.sc testX
       
     6 //
       
     7 // or 
       
     8 //
       
     9 //   amm re1.sc all
       
    10 
       
    11 // regular expressions
       
    12 abstract class Rexp
       
    13 case object ZERO extends Rexp                    // matches nothing
       
    14 case object ONE extends Rexp                     // matches an empty string
       
    15 case class CHAR(c: Char) extends Rexp            // matches a character c
       
    16 case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
       
    17 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
       
    18 case class STAR(r: Rexp) extends Rexp            // star
       
    19 
       
    20 // nullable function: tests whether a regular 
       
    21 // expression can recognise the empty string
       
    22 def nullable(r: Rexp) : Boolean = r match {
       
    23   case ZERO => false
       
    24   case ONE => true
       
    25   case CHAR(_) => false
       
    26   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    27   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    28   case STAR(_) => true
       
    29 }
       
    30 
       
    31 // the derivative of a regular expression w.r.t. a character
       
    32 def der (c: Char, r: Rexp) : Rexp = r match {
       
    33   case ZERO => ZERO
       
    34   case ONE => ZERO
       
    35   case CHAR(d) => if (c == d) ONE else ZERO
       
    36   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    37   case SEQ(r1, r2) => 
       
    38     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    39     else SEQ(der(c, r1), r2)
       
    40   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
       
    41 }
       
    42 
       
    43 // the derivative w.r.t. a string (iterates der)
       
    44 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    45   case Nil => r
       
    46   case c::s => ders(s, der(c, r))
       
    47 }
       
    48 
       
    49 // the main matcher function
       
    50 def matcher(r: Rexp, s: String) : Boolean = 
       
    51   nullable(ders(s.toList, r))
       
    52 
       
    53 
       
    54 // some examples from the homework
       
    55 
       
    56 val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
       
    57 der('a', r)
       
    58 der('b', r)
       
    59 der('c', r)
       
    60 
       
    61 val r2 = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z'))
       
    62 der('x', r2)
       
    63 der('y', der('x', r2))
       
    64 der('z', der('y', der('x', r2)))
       
    65 
       
    66 
       
    67 // the optional regular expression (one or zero times)
       
    68 def OPT(r: Rexp) = ALT(r, ONE)
       
    69 
       
    70 // the n-times regular expression (explicitly expanded)
       
    71 def NTIMES(r: Rexp, n: Int) : Rexp = n match {
       
    72   case 0 => ONE
       
    73   case 1 => r
       
    74   case n => SEQ(r, NTIMES(r, n - 1))
       
    75 }
       
    76 
       
    77 
       
    78 // Test Cases
       
    79 //============
       
    80 
       
    81 // the evil regular expression  a?{n} a{n}
       
    82 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
       
    83 
       
    84 // the evil regular expression (a*)* b
       
    85 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
       
    86 
       
    87 // for measuring time
       
    88 def time_needed[T](i: Int, code: => T) = {
       
    89   val start = System.nanoTime()
       
    90   for (j <- 1 to i) code
       
    91   val end = System.nanoTime()
       
    92   (end - start) / (i * 1.0e9)
       
    93 }
       
    94 
       
    95 
       
    96 // test: (a?{n}) (a{n})
       
    97 @doc("Test (a?{n}) (a{n})")
       
    98 @main
       
    99 def test1() = {
       
   100   println("Test (a?{n}) (a{n})")
       
   101 
       
   102   for (i <- 0 to 20 by 2) {
       
   103     println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f")
       
   104   }
       
   105 }
       
   106 
       
   107 // test: (a*)* b
       
   108 @doc("Test (a*)* b")
       
   109 @main
       
   110 def test2() = {
       
   111   println("Test (a*)* b")
       
   112 
       
   113   for (i <- 0 to 20 by 2) {
       
   114     println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
       
   115   }
       
   116 }
       
   117 
       
   118 // the size of a regular expressions - for testing purposes 
       
   119 def size(r: Rexp) : Int = r match {
       
   120   case ZERO => 1
       
   121   case ONE => 1
       
   122   case CHAR(_) => 1
       
   123   case ALT(r1, r2) => 1 + size(r1) + size(r2)
       
   124   case SEQ(r1, r2) => 1 + size(r1) + size(r2)
       
   125   case STAR(r) => 1 + size(r)
       
   126 }
       
   127 
       
   128 // the expicit expansion in EVIL1(n) increases
       
   129 // drastically its size
       
   130 
       
   131 size(EVIL1(1))  // 5
       
   132 size(EVIL1(3))  // 17
       
   133 size(EVIL1(5))  // 29
       
   134 size(EVIL1(7))  // 41
       
   135 size(EVIL1(20)) // 119
       
   136 
       
   137 // given a regular expression and building successive
       
   138 // derivatives might result into bigger and bigger
       
   139 // regular expressions...here is an example for this:
       
   140 
       
   141 // (a+b)* o a o b o (a+b)*
       
   142 val BIG_aux = STAR(ALT(CHAR('a'), CHAR('b')))
       
   143 val BIG = SEQ(BIG_aux, SEQ(CHAR('a'),SEQ(CHAR('b'), BIG_aux)))
       
   144 
       
   145 size(ders("".toList, BIG))              // 13
       
   146 size(ders("ab".toList, BIG))            // 51
       
   147 size(ders("abab".toList, BIG))          // 112
       
   148 size(ders("ababab".toList, BIG))        // 191
       
   149 size(ders("abababab".toList, BIG))      // 288
       
   150 size(ders("ababababab".toList, BIG))    // 403
       
   151 size(ders("abababababab".toList, BIG))  // 536
       
   152 
       
   153 
       
   154 size(ders(("ab" * 200).toList, BIG))    // 366808
       
   155 
       
   156 @doc("Test (a + b)* o (a o b) o (a + b)*")
       
   157 @main
       
   158 def test3() = {
       
   159   println("Test (a + b)* o (a o b) o (a + b)*")
       
   160 
       
   161   for (i <- 0 to 200 by 10) {
       
   162     println(f"$i: ${time_needed(2, matcher(BIG, "ab" * i))}%.5f")
       
   163   }
       
   164 }
       
   165 
       
   166 
       
   167 
       
   168 
       
   169 @doc("All tests.")
       
   170 @main
       
   171 def all() = { test1(); test2() ; test3() }