--- a/progs/comb1a.scala Thu Nov 07 00:07:16 2019 +0000
+++ b/progs/comb1a.scala Thu Nov 14 01:21:02 2019 +0000
@@ -66,18 +66,18 @@
implicit def char2parser(c: Char) = CharParser(c)
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
- def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
+ def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
}
implicit def StringOps(s: String) = new {
- def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
- def || (r: String) = new AltParser[String, String](s, r)
+ def ||(q : => Parser[String, String]) = new AltParser[String, String](s, q)
+ def ||(r: String) = new AltParser[String, String](s, r)
def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
- def ~[S] (q : => Parser[String, S]) =
+ def ~[S](q : => Parser[String, S]) =
new SeqParser[String, String, S](s, q)
- def ~ (r: String) =
+ def ~(r: String) =
new SeqParser[String, String, String](s, r)
}
@@ -106,20 +106,20 @@
// Arithmetic Expressions (Terms and Factors)
lazy val E: Parser[String, Int] =
- (T ~ "+" ~ E) ==> { case x ~ y ~ z => x + z } ||
- (T ~ "-" ~ E) ==> { case x ~ y ~ z => x - z } || T
+ (T ~ "+" ~ E) ==> { case x ~ _ ~ z => x + z } ||
+ (T ~ "-" ~ E) ==> { case x ~ _ ~ z => x - z } || T
lazy val T: Parser[String, Int] =
- (F ~ "*" ~ T) ==> { case x ~ y ~ z => x * z } || F
+ (F ~ "*" ~ T) ==> { case x ~ _ ~ z => x * z } || F
lazy val F: Parser[String, Int] =
- ("(" ~ E ~ ")") ==> { case x ~ y ~ z => y } || NumParserInt
+ ("(" ~ E ~ ")") ==> { case _ ~ y ~ _ => y } || NumParserInt
/* same parser but producing a string
lazy val E: Parser[String, String] =
- (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T
+ (T ~ "+" ~ E) ==> { case x ~ y ~ z => "(" + x + ")+(" + z + ")"} || T
lazy val T: Parser[String, String] =
- (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F
+ (F ~ "*" ~ T) ==> { case x ~ y ~ z => "(" + x + ")*("+ z + ")"} || F
lazy val F: Parser[String, String] =
- ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
+ ("(" ~ E ~ ")") ==> { case x ~ y ~ z => y } || NumParser
*/
println(E.parse_all("1+3+4"))
@@ -191,7 +191,7 @@
// a problem with the arithmetic expression parser -> gets
-// slow with deep nestedness
+// slow with deeply nested parentheses
println("Runtime problem")
E.parse("1")
E.parse("(1)")