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