progs/parser-combinators/comb1.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 27 Jul 2020 11:02:48 +0100
changeset 741 e66bd5c563eb
parent 732 c7bdd7eac4cb
child 742 b5b5583a3a08
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Parser Combinators: Simple Version
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//====================================
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
/* 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
  Note, in the lectures I did not show the implicit type constraint
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
  I : IsSeq, which means that the input type 'I' needs to be
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  a sequence. 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
*/
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
type IsSeq[A] = A => Seq[_]
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
abstract class Parser[I : IsSeq, T]{
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  def parse(in: I): Set[(T, I)]
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  def parse_all(in: I) : Set[T] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
    for ((hd, tl) <- parse(in); 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
        if tl.isEmpty) yield hd
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// parser combinators
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
// sequence parser
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
                                 q: => Parser[I, S]) extends Parser[I, (T, S)] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  def parse(in: I) = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
    for ((hd1, tl1) <- p.parse(in); 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
         (hd2, tl2) <- q.parse(tl1)) yield ((hd1, hd2), tl2)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
// alternative parser
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
class AltParser[I : IsSeq, T](p: => Parser[I, T], 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
                              q: => Parser[I, T]) extends Parser[I, T] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  def parse(in: I) = p.parse(in) ++ q.parse(in)   
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
// parser map
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
class MapParser[I : IsSeq, T, S](p: => Parser[I, T], 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
                                 f: T => S) extends Parser[I, S] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
// an example of an atomic parser for characters
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
case class CharParser(c: Char) extends Parser[String, Char] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  def parse(in: String) = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
    if (in != "" && in.head == c) Set((c, in.tail)) else Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
// an atomic parser for parsing strings according to a regex
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
import scala.util.matching.Regex
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
case class RegexParser(reg: Regex) extends Parser[String, String] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  def parse(in: String) = reg.findPrefixMatchOf(in) match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    case None => Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    case Some(m) => Set((m.matched, m.after.toString))  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
// atomic parsers for numbers and "verbatim" strings 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
val NumParser = RegexParser("[0-9]+".r)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
def StrParser(s: String) = RegexParser(Regex.quote(s).r)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
// NumParserInt transforms a "string integer" into a propper Int
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
// (needs "new" because MapParser is not a case class)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
val NumParserInt = new MapParser(NumParser, (s: String) => s.toInt)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
// the following string interpolation allows us to write 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
// StrParser(_some_string_) more conveniently as 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
//
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
// p"<_some_string_>" 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
implicit def parser_interpolation(sc: StringContext) = new {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  def p(args: Any*) = StrParser(sc.s(args:_*))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
// more convenient syntax for parser combinators
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
// these implicits allow us to use an infix notation for
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
// sequences and alternatives; we also can write the usual
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
// map for a MapParser
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
// with this NumParserInt can now be written more conveniently
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// as:
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
val NumParserInt2 = NumParser.map(s => s.toInt)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
// A parser for palindromes (just returns them as string)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
lazy val Pal : Parser[String, String] = {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } || 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  p"a" || p"b" || p""
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
}  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
// examples
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
Pal.parse_all("abaaaba")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
Pal.parse("abaaaba")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
println("Palindrome: " + Pal.parse_all("abaaaba"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
// A parser for wellnested parentheses (transforms '(' -> '{' , ')' -> '}' )
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
lazy val P : Parser[String, String] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  (p"(" ~ P ~ p")" ~ P).map{ case (((_, x), _), y) => "{" + x + "}" + y } || p""
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
println(P.parse_all("(((()()))())"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
println(P.parse_all("(((()()))()))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
println(P.parse_all(")("))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
println(P.parse_all("()"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
// A parser for arithmetic expressions (Terms and Factors)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
lazy val E: Parser[String, Int] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  (T ~ p"+" ~ E).map{ case ((x, _), z) => x + z } ||
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  (T ~ p"-" ~ E).map{ case ((x, _), z) => x - z } || T 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
lazy val T: Parser[String, Int] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  (F ~ p"*" ~ T).map{ case ((x, _), z) => x * z } || F
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
lazy val F: Parser[String, Int] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  (p"(" ~ E ~ p")").map{ case ((_, y), _) => y } || NumParserInt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
/* same parser but producing a string
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
lazy val E: Parser[String, String] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  (T ~ "+" ~ E).map{ case x ~ y ~ z => "(" + x + ")+(" + z + ")"} || T 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
lazy val T: Parser[String, String] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  (F ~ "*" ~ T).map{ case x ~ y ~ z => "(" + x + ")*("+ z + ")"} || F
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
lazy val F: Parser[String, String] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
  ("(" ~ E ~ ")").map{ case x ~ y ~ z => y } || NumParser
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
*/
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
println(E.parse_all("1+3+4"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
println(E.parse("1+3+4"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
println(E.parse_all("4*2+3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
println(E.parse_all("4*(2+3)"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
println(E.parse_all("(4)*((2+3))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
println(E.parse_all("4/2+3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
println(E.parse("1 + 2 * 3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
println(E.parse_all("(1+2)+3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
println(E.parse_all("1+2+3"))  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
// with parser combinators (and other parsing algorithms)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
// no left-recursion is allowed, otherwise the will loop
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
lazy val EL: Parser[String, Int] = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
  ((EL ~ p"+" ~ EL).map{ case ((x, y), z) => x + z} || 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
   (EL ~ p"*" ~ EL).map{ case ((x, y), z) => x * z} ||
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
   (p"(" ~ EL ~ p")").map{ case ((x, y), z) => y} ||
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
   NumParserInt)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
// this will run forever:
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
//println(EL.parse_all("1+2+3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
// non-ambiguous vs ambiguous grammars
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
// ambiguous
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
lazy val S : Parser[String, String] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
  (p"1" ~ S ~ S).map{ case ((x, y), z) => x + y + z } || p""
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
println(time(S.parse("1" * 10)))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
println(time(S.parse_all("1" * 10)))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
// non-ambiguous
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
lazy val U : Parser[String, String] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
  (p"1" ~ U).map{ case (x, y) => x + y } || p""
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
println(time(U.parse("1" * 10)))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
println(time(U.parse_all("1" * 10)))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
println(U.parse("1" * 25))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
U.parse("11")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
U.parse("11111")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
U.parse("11011")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
U.parse_all("1" * 100)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
U.parse_all("1" * 100 + "0")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
// you can see the difference in second example
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
//S.parse_all("1" * 100)         // succeeds
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
//S.parse_all("1" * 100 + "0")   // fails
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
// A variant which counts how many 1s are parsed
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
lazy val UCount : Parser[String, Int] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  (p"1" ~ UCount).map[Int]{ case (_, y) => y + 1 } || p"".map[Int]{ _ => 0 }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
println(UCount.parse("11111"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
println(UCount.parse_all("11111"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
// Two single character parsers
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
lazy val One : Parser[String, String] = p"a"
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
lazy val Two : Parser[String, String] = p"b"
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
One.parse("a")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
One.parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
// note how the pairs nest to the left with sequence parsers
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
(One ~ One).parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
(One ~ One ~ One).parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
(One ~ One ~ One ~ One).parse("aaaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
(One || Two).parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
// a problem with the arithmetic expression parser: it 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
// gets vert slow with deeply nested parentheses
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
println("Runtime problem")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
println(E.parse("1"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
println(E.parse("(1)"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
println(E.parse("((1))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
//println(E.parse("(((1)))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
//println(E.parse("((((1))))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
//println(E.parse("((((((1))))))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
//println(E.parse("(((((((1)))))))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
//println(E.parse("((((((((1)))))))"))