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