progs/comb1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 23 Oct 2015 00:16:00 +0100
changeset 360 c6c574d2ca0c
parent 185 ea8b94d4755e
child 362 57ea439feaff
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Parser[I <% Seq[_], T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
     7
  def parse_all(ts: I) : Set[T] =
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
     8
    for ((head, tail) <- parse(ts); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
     9
        if (tail.isEmpty)) yield head
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    12
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    13
                                   q: => Parser[I, S]) extends Parser[I, (T, S)] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    19
class AltParser[I <% Seq[_], T](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    20
                                q: => Parser[I, T]) extends Parser[I, T] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    24
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    25
                                   f: T => S) extends Parser[I, S] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
    30
// atomic parsers  
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    31
case class CharParser(c: Char) extends Parser[String, Char] {
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    32
  def parse(sb: String) = 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    33
    if (sb.head == c) Set((c, sb.tail)) else Set()
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    34
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    35
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
case class StringParser(s: String) extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def parse(sb: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    val (prefix, suffix) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    if (prefix == s) Set((prefix, suffix)) else Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
case object NumParser extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  val reg = "[0-9]+".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
    case Some(s) => Set(sb.splitAt(s.length)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    51
// convenience
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
implicit def string2parser(s : String) = StringParser(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
implicit def StringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  def || (r: String) = new AltParser[String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  def ~[S] (q : => Parser[String, S]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    new SeqParser[String, String, S](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  def ~ (r: String) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
    new SeqParser[String, String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    70
// palindromes
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
lazy val Pal : Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
   ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    75
println("Palindrom: " + Pal.parse_all("ababbaba"))
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    77
// well-nested parenthesis
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
lazy val P : Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
P.parse_all("(((()()))())")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
P.parse_all("(((()()))()))")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
P.parse_all(")(")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    85
// arithmetic expressions
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
lazy val E: Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  (F ~ "*" ~ F) ==> { case ((x, y), z) => x + y + z } || F  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
lazy val F: Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + y + z } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
   (T ~ "-" ~ T) ==> { case ((x, y), z) => x + y + z } || T)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
lazy val T: Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => x + y + z } || NumParser
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
185
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    94
println(E.parse_all("1*2+3"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    95
println(E.parse_all("1+2*3"))
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
println(E.parse_all("1+2+3"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
// non-ambiguous vs ambiguous
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
lazy val U : Parser[String, String] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
  ("1" ~ U) ==> { case (x, y) => x + y } || ""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
U.parse_all("1" * 100 + "0")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
lazy val S : Parser[String, String] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
  ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || ""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
S.parse_all("1" * 15)