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