progs/parser-combinators/comb1.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 29 May 2024 13:25:30 +0100
changeset 960 c7009356ddd8
parent 959 64ec1884d860
child 961 c0600f8b6427
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
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     8
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
     9
//  Note, in the lectures I did not show the type bound
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    10
//  using is: I => Seq[_], which means that the input
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    11
//  type 'I' needs to be a sequence.
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    13
type IsSeq[I] = I => Seq[?]
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    14
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    15
abstract class Parser[I: IsSeq, T](using is: IsSeq[I])  {
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
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] =
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    19
    for ((hd, tl) <- parse(in);
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    20
        if is(tl).isEmpty) yield hd
732
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
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    23
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
// parser combinators
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    26
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    27
// alternative parser
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    28
class AltParser[I : IsSeq, T](p: => Parser[I, T],
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    29
                              q: => Parser[I, T]) extends Parser[I, T] {
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    30
  def parse(in: I) = p.parse(in) ++ q.parse(in)
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    31
}
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    32
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
// sequence parser
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    34
class SeqParser[I: IsSeq, T, S](p: => Parser[I, T],
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    35
                                q: => Parser[I, S]) extends Parser[I, (T, S)] {
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    36
  def parse(in: I) =
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    37
    for ((hd1, tl1) <- p.parse(in);
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
         (hd2, tl2) <- q.parse(tl1)) yield ((hd1, hd2), tl2)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    41
// map parser
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    42
class MapParser[I : IsSeq, T, S](p: => Parser[I, T],
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    43
                                 f: T => S) extends Parser[I, S] {
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  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
    45
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    47
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    48
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
// an example of an atomic parser for characters
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
case class CharParser(c: Char) extends Parser[String, Char] {
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    51
  def parse(in: String) =
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
    if (in != "" && in.head == c) Set((c, in.tail)) else Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    55
val ap = CharParser('a')
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    56
val bp = CharParser('b')
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    57
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    58
val abp = SeqParser(ap, bp)
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    59
MapParser(abp, ab => s"$ab").parse("abc")
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    60
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
// an atomic parser for parsing strings according to a regex
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
import scala.util.matching.Regex
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
case class RegexParser(reg: Regex) extends Parser[String, String] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  def parse(in: String) = reg.findPrefixMatchOf(in) match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    case None => Set()
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    67
    case Some(m) => Set((m.matched, m.after.toString))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  }
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    71
// atomic parsers for numbers and "verbatim" strings
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
val NumParser = RegexParser("[0-9]+".r)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
def StrParser(s: String) = RegexParser(Regex.quote(s).r)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    75
NumParser.parse("123a123bc")
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    76
StrParser("else").parse("elsethen")
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    77
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    78
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
// NumParserInt transforms a "string integer" into a propper Int
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
// (needs "new" because MapParser is not a case class)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
    82
val NumParserInt = MapParser(NumParser, (s: String) => s.toInt)
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 896
diff changeset
    83
NumParserInt.parse("123abc")
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    85
// the following string interpolation allows us to write
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    86
// StrParser(_some_string_) more conveniently as
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
//
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    88
// p"<_some_string_>"
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    90
extension (sc: StringContext)
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    91
  def p(args: Any*) = StrParser(sc.s(args*))
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 896
diff changeset
    92
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 896
diff changeset
    93
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    94
(p"else").parse("elsethen")
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    95
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
// more convenient syntax for parser combinators
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    97
extension [I: IsSeq, T](p: Parser[I, T]) {
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   100
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
732
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
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   103
// simple example of transforming the
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   104
// result into capital letters
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 896
diff changeset
   105
def toU(s: String) = s.map(_.toUpper)
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 896
diff changeset
   106
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   107
(p"else").map(toU(_)).parse("elseifthen")
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 896
diff changeset
   108
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
// these implicits allow us to use an infix notation for
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
// sequences and alternatives; we also can write the usual
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
// map for a MapParser
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
// with this NumParserInt can now be written more conveniently
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
// as:
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
val NumParserInt2 = NumParser.map(_.toInt)
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
   119
val x = 1 + 3
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
// A parser for palindromes (just returns them as string)
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   122
//  since the parser is recursive it needs to be lazy
803
d4fb8c7fc3bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 801
diff changeset
   123
lazy val Pal : Parser[String, String] = {
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   124
   (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } ||
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   125
   (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } ||
896
b7a6436c7758 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
   126
    p"a" || p"b" || p""
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   127
}
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
// examples
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
   130
Pal.parse_all("abacaba")
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
   131
Pal.parse("abacaaba")
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
println("Palindrome: " + Pal.parse_all("abaaaba"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   135
// A parser for wellnested parentheses
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   136
//
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   137
//   P ::= ( P ) P | epsilon
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   138
//
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   139
//   (transforms '(' -> '{' , ')' -> '}' )
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   140
lazy val P : Parser[String, String] = {
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   141
  (p"(" ~ P ~ p")" ~ P).map{ case (((_, x), _), y) => "{" + x + "}" + y } ||
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   142
  p""
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   143
}
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
println(P.parse_all("(((()()))())"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
println(P.parse_all("(((()()))()))"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
println(P.parse_all(")("))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
println(P.parse_all("()"))
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
// A parser for arithmetic expressions (Terms and Factors)
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 897
diff changeset
   151
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   152
lazy val E: Parser[String, Int] = {
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  (T ~ p"+" ~ E).map{ case ((x, _), z) => x + z } ||
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   154
  (T ~ p"-" ~ E).map{ case ((x, _), z) => x - z } || T }
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   155
lazy val T: Parser[String, Int] = {
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   156
  (F ~ p"*" ~ T).map{ case ((x, _), z) => x * z } || F }
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   157
lazy val F: Parser[String, Int] = {
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   158
  (p"(" ~ E ~ p")").map{ case ((_, y), _) => y } || NumParserInt }
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   159
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   160
println(E.parse_all("2*2*2"))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
println(E.parse_all("1+3+4"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
println(E.parse("1+3+4"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
println(E.parse_all("4*2+3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
println(E.parse_all("4*(2+3)"))
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 948
diff changeset
   165
println(E.parse_all("(4)*(((2+3)))"))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
println(E.parse_all("4/2+3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
println(E.parse("1 + 2 * 3"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
println(E.parse_all("(1+2)+3"))
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   169
println(E.parse_all("1+2+3"))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
// with parser combinators (and other parsing algorithms)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
// no left-recursion is allowed, otherwise the will loop
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   175
lazy val EL: Parser[String, Int] =
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   176
  ((EL ~ p"+" ~ EL).map{ case ((x, y), z) => x + z} ||
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
   (EL ~ p"*" ~ EL).map{ case ((x, y), z) => x * z} ||
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
   (p"(" ~ EL ~ p")").map{ case ((x, y), z) => y} ||
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
   NumParserInt)
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
// this will run forever:
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
//println(EL.parse_all("1+2+3"))
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
// non-ambiguous vs ambiguous grammars
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
// ambiguous
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
lazy val S : Parser[String, String] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
  (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
   190
850
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
   191
//println(time(S.parse("1" * 10)))
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
   192
//println(time(S.parse_all("1" * 10)))
732
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
// non-ambiguous
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
lazy val U : Parser[String, String] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
  (p"1" ~ U).map{ case (x, y) => x + y } || p""
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
850
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
   198
//println(time(U.parse("1" * 10)))
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
   199
//println(time(U.parse_all("1" * 10)))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
println(U.parse("1" * 25))
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
U.parse("11")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
U.parse("11111")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
U.parse("11011")
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
U.parse_all("1" * 100)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
U.parse_all("1" * 100 + "0")
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
// you can see the difference in second example
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
//S.parse_all("1" * 100)         // succeeds
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
//S.parse_all("1" * 100 + "0")   // fails
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
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
// A variant which counts how many 1s are parsed
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
lazy val UCount : Parser[String, Int] =
799
85267be9a5ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   216
  (p"1" ~ UCount).map{ case (_, y) => y + 1 } || p"".map{ _ => 0 }
732
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
println(UCount.parse("11111"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
println(UCount.parse_all("11111"))
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
// Two single character parsers
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
lazy val One : Parser[String, String] = p"a"
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
lazy val Two : Parser[String, String] = p"b"
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
One.parse("a")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
One.parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
// note how the pairs nest to the left with sequence parsers
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
(One ~ One).parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
(One ~ One ~ One).parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
(One ~ One ~ One ~ One).parse("aaaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
(One || Two).parse("aaa")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
960
c7009356ddd8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   237
// a problem with the arithmetic expression parser: it
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   238
// gets very slow with deeply nested parentheses
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   240
println("A runtime problem")
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
println(E.parse("1"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
println(E.parse("(1)"))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
println(E.parse("((1))"))
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   244
println(E.parse("(((1)))"))
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   245
println(E.parse("((((1))))"))
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   246
println(E.parse("((((((1))))))"))
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   247
println(E.parse("(((((((1)))))))"))
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   248
//println(E.parse("((((((((1))))))))"))
828
bdcaecdee9eb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 803
diff changeset
   249
bdcaecdee9eb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 803
diff changeset
   250
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   251
// faster because of merge in the +/- case
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   252
lazy val E2: Parser[String, Int] = {
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   253
  (T2 ~ (p"+" || p"-") ~ E2).map[Int]{ case ((x, y), z) => if (y == "+") x + z else x - z} || T2 }
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   254
lazy val T2: Parser[String, Int] = {
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   255
  (F2 ~ p"*" ~ T2).map[Int]{ case ((x, _), z) => x * z } || F2 }
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   256
lazy val F2: Parser[String, Int] = {
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   257
  (p"(" ~ E2 ~ p")").map[Int]{ case ((_, y), _) => y } || NumParserInt }
828
bdcaecdee9eb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 803
diff changeset
   258
bdcaecdee9eb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 803
diff changeset
   259
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   260
println("mitigated by merging clauses")
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   261
println(E2.parse("1"))
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   262
println(E2.parse("(1)"))
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   263
println(E2.parse("((1))"))
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   264
println(E2.parse("(((1)))"))
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
   265
println(E2.parse("((((1))))"))
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   266
println(E2.parse("((((((1))))))"))
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   267
println(E2.parse("(((((((1)))))))"))
937
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   268
println(E2.parse("((((((((1))))))))"))
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   269
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   270
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   271
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   272
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   273
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   274
/*
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   275
Try
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   276
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   277
6 / 2 * (2+1)
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   278
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   279
*/