diff -r 75d9f9e5906f -r 05cfce0fdef7 progs/comb1.scala --- a/progs/comb1.scala Thu Nov 07 00:07:16 2019 +0000 +++ b/progs/comb1.scala Thu Nov 14 01:21:02 2019 +0000 @@ -60,18 +60,18 @@ implicit def char2parser(c: Char) = CharParser(c) implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = 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) } @@ -131,15 +131,6 @@ lazy val F: Parser[String, Int] = ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParserInt -/* same parser but producing a string -lazy val E: Parser[String, String] = - (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T -lazy val T: Parser[String, String] = - (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F -lazy val F: Parser[String, String] = - ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser -*/ - println(E.parse_all("1+3+4")) println(E.parse("1+3+4")) println(E.parse_all("4*2+3")) @@ -150,7 +141,14 @@ println(E.parse_all("(1+2)+3")) println(E.parse_all("1+2+3")) - +/* same parser but producing a string +lazy val E: Parser[String, String] = + (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T +lazy val T: Parser[String, String] = + (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F +lazy val F: Parser[String, String] = + ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser +*/ // no left-recursion allowed, otherwise will loop lazy val EL: Parser[String, Int] = @@ -166,7 +164,7 @@ // ambiguous lazy val S : Parser[String, String] = - ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || "" + ("1" ~ S ~ S ~ S) ==> { case (((x, y), z), v) => x + y + z } || "" S.parse("1" * 10) @@ -199,14 +197,14 @@ One.parse("111") (One ~ One).parse("111") -(One ~ One ~ One).parse("111") +(One ~ One ~ One).parse("1111") (One ~ One ~ One ~ One).parse("1111") (One || Two).parse("111") // a problem with the arithmetic expression parser -> gets -// slow with deep nestedness +// slow with deeply nested parentheses E.parse("1") E.parse("(1)") E.parse("((1))") @@ -214,3 +212,15 @@ E.parse("((((1))))") E.parse("((((((1))))))") E.parse("(((((((1)))))))") + + + + +/* + +starting symbols +tokenise/detokenise +:paste +pairs in sequences + +*/ \ No newline at end of file