--- a/progs/parser-combinators/comb1.sc Fri Sep 15 10:49:33 2023 +0100
+++ b/progs/parser-combinators/comb1.sc Sun Sep 17 19:12:57 2023 +0100
@@ -89,12 +89,12 @@
extension [I, T](p: Parser[I, T])(using I => Seq[_]) {
def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
- def mapp[S](f: => T => S) = new MapParser[I, T, S](p, f)
+ def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
}
def toU(s: String) = s.map(_.toUpper)
-(p"ELSE").mapp(toU(_)).parse("ELSEifthen")
+(p"ELSE").map(toU(_)).parse("ELSEifthen")
// these implicits allow us to use an infix notation for
// sequences and alternatives; we also can write the usual
@@ -109,8 +109,8 @@
// A parser for palindromes (just returns them as string)
lazy val Pal : Parser[String, String] = {
- (p"a" ~ Pal ~ p"a").mapp{ case ((x, y), z) => s"$x$y$z" } ||
- (p"b" ~ Pal ~ p"b").mapp{ case ((x, y), z) => s"$x$y$z" } ||
+ (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } ||
+ (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } ||
p"a" || p"b" || p""
}
@@ -126,7 +126,7 @@
//
// (transforms '(' -> '{' , ')' -> '}' )
lazy val P : Parser[String, String] = {
- (p"(" ~ P ~ p")" ~ P).mapp{ case (((_, x), _), y) => "{" + x + "}" + y } ||
+ (p"(" ~ P ~ p")" ~ P).map{ case (((_, x), _), y) => "{" + x + "}" + y } ||
p""
}
@@ -224,32 +224,32 @@
// a problem with the arithmetic expression parser: it
// gets very slow with deeply nested parentheses
-println("Runtime problem")
+println("A runtime problem")
println(E.parse("1"))
println(E.parse("(1)"))
println(E.parse("((1))"))
println(E.parse("(((1)))"))
println(E.parse("((((1))))"))
-//println(E.parse("((((((1))))))"))
-//println(E.parse("(((((((1)))))))"))
+println(E.parse("((((((1))))))"))
+println(E.parse("(((((((1)))))))"))
//println(E.parse("((((((((1))))))))"))
-// faster because of merge
+// faster because of merge in the +/- case
lazy val E2: Parser[String, Int] = {
- (T2 ~ (p"+" || p"-") ~ E2).mapp[Int]{ case ((x, y), z) => if (y == "+") x + z else x - z} || T2 }
+ (T2 ~ (p"+" || p"-") ~ E2).map[Int]{ case ((x, y), z) => if (y == "+") x + z else x - z} || T2 }
lazy val T2: Parser[String, Int] = {
- (F2 ~ p"*" ~ T2).mapp[Int]{ case ((x, _), z) => x * z } || F2 }
+ (F2 ~ p"*" ~ T2).map[Int]{ case ((x, _), z) => x * z } || F2 }
lazy val F2: Parser[String, Int] = {
- (p"(" ~ E2 ~ p")").mapp[Int]{ case ((_, y), _) => y } || NumParserInt }
+ (p"(" ~ E2 ~ p")").map[Int]{ case ((_, y), _) => y } || NumParserInt }
-println("Runtime problem")
+println("mitigated by merging clauses")
println(E2.parse("1"))
println(E2.parse("(1)"))
println(E2.parse("((1))"))
println(E2.parse("(((1)))"))
println(E2.parse("((((1))))"))
-//println(E2.parse("((((((1))))))"))
-//println(E2.parse("(((((((1)))))))"))
-//println(E2.parse("((((((((1))))))))"))
\ No newline at end of file
+println(E2.parse("((((((1))))))"))
+println(E2.parse("(((((((1)))))))"))
+println(E2.parse("((((((((1))))))))"))
\ No newline at end of file