progs/comb1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 06 Nov 2015 08:52:16 +0000
changeset 366 5a83336a9690
parent 362 57ea439feaff
child 367 04127a5aad23
permissions -rw-r--r--
updated
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
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     4
/* Note, in the lectures I did not show the type consraint
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     5
 * I <% Seq[_] , which means that the input type I can be
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     6
 * treated, or seen, as a sequence. */
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     7
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
abstract class Parser[I <% Seq[_], T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  def parse(ts: I): Set[(T, I)]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
    11
  def parse_all(ts: I) : Set[T] =
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    12
    for ((head, tail) <- parse(ts); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    13
        if (tail.isEmpty)) yield head
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    16
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
    17
                                   q: => Parser[I, S]) extends Parser[I, (T, S)] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    23
class AltParser[I <% Seq[_], T](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    24
                                q: => Parser[I, T]) extends Parser[I, T] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    28
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
    29
                                   f: T => S) extends Parser[I, S] {
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
    34
// atomic parsers  
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    35
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
    36
  def parse(sb: String) = 
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    37
    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
    38
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    39
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
case class StringParser(s: String) extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def parse(sb: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
    val (prefix, suffix) = sb.splitAt(s.length)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
    if (prefix == s) Set((prefix, suffix)) else Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
case object NumParser extends Parser[String, String] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  val reg = "[0-9]+".r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  def parse(sb: String) = reg.findPrefixOf(sb) match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
    case None => Set()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
    case Some(s) => Set(sb.splitAt(s.length)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  }
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
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    55
// convenience
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
implicit def string2parser(s : String) = StringParser(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
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
    59
  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
    60
  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
    61
  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
    62
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
implicit def StringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  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
    66
  def || (r: String) = new AltParser[String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  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
    68
  def ~[S] (q : => Parser[String, S]) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
    new SeqParser[String, String, S](s, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  def ~ (r: String) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
    new SeqParser[String, String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    74
// a parse palindromes
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
lazy val Pal : Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
  (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
   ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    79
println("Palindrom: " + Pal.parse_all("ababbaba"))
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    81
// well-nested parenthesis parser
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
lazy val P : Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
P.parse_all("(((()()))())")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
P.parse_all("(((()()))()))")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
P.parse_all(")(")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
    88
P.parse_all("()")
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    90
// arithmetic expressions
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
lazy val E: Parser[String, String] = 
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
    92
  (F ~ "*" ~ T) ==> { case ((x, y), z) => x + y + z } || F  
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
lazy val F: Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + y + z } ||
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
   (T ~ "-" ~ T) ==> { case ((x, y), z) => x + y + z } || T)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
lazy val T: Parser[String, String] = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => x + y + z } || NumParser
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
185
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    99
println(E.parse_all("1*2+3"))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   100
println(E.parse_all("1+2*3"))
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   101
println(E.parse_all("(1+2)+3"))
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   102
println(E.parse_all("1+2+3"))  // this is not parsed, because of 
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   103
                               // how the grammar is set up
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   105
// non-ambiguous vs ambiguous grammars
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
lazy val S : Parser[String, String] =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || ""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
S.parse_all("1" * 15)
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   110
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   111
lazy val U : Parser[String, String] =
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   112
  ("1" ~ U) ==> { case (x, y) => x + y  } || ""
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   113
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   114
U.parse("11")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   115
U.parse("11111")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   116
U.parse("11011")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   117
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   118
U.parse_all("1" * 100 + "0")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   119
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   120
lazy val UCount : Parser[String, Int] =
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   121
  ("1" ~ UCount) ==> { case (x, y) => y + 1 } || 
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   122
  "" ==> { (x) => 0 }
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   123
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   124
UCount.parse("11111")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   125
UCount.parse_all("11111")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   126
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   127
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   128
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   129
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   130
// Single Character parser
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   131
lazy val One : Parser[String, String] = "1"
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   132
lazy val Two : Parser[String, String] = "2"
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   133
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   134
One.parse("1")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   135
One.parse("111")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   136
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   137
(One ~ One).parse("111")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   138
(One ~ One ~ One).parse("111")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   139
(One ~ One ~ One ~ One).parse("1111")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   140
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   141
(One || Two).parse("111")