progs/parser-combinators/comb1.sc
changeset 1026 ee39fd6df150
parent 1024 c54cba4e59e7
--- a/progs/parser-combinators/comb1.sc	Fri Nov 14 12:38:59 2025 +0000
+++ b/progs/parser-combinators/comb1.sc	Mon Nov 17 23:28:27 2025 +0000
@@ -97,7 +97,7 @@
 // (needs "new" because MapParser is not a case class)
 
 val NumParserInt = MapParser(NumParser, (s: String) => s.toInt)
-NumParserInt.parse("123abc")
+NumParserInt.parse("a123abc")
 
 // the following string interpolation allows us to write
 // StrParser(_some_string_) more conveniently as
@@ -139,6 +139,17 @@
 
 // A parser for palindromes (just returns them as string)
 //  since the parser is recursive it needs to be lazy
+val foo = foo + foo
+
+lazy val Foo : Parser[String, String] = p"a" || p"b" || p""
+
+
+lazy val Pal : Parser[String, String] = {
+   (p"a" ~ Pal ~ p"a") ||
+   (p"b" ~ Pal ~ p"b") ||
+    p"a" || p"b" || p""
+}
+
 lazy val Pal : Parser[String, String] = {
    (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" } ||
@@ -146,7 +157,7 @@
 }
 
 // examples
-Pal.parse("abaaba")
+Pal.parse_all("abaaba")
 Pal.parse("abacaaba")
 
 println("Palindrome: " + Pal.parse_all("abaaaba"))
@@ -178,8 +189,8 @@
   (p"(" ~ E ~ p")").map{ case ((_, y), _) => y } || NumParserInt }
 
 println(E.parse_all("2*2*2"))
+println(E.parse_all("5+3+4"))
 println(E.parse_all("1+3+4"))
-println(E.parse("1+3+4"))
 println(E.parse_all("4*2+3"))
 println(E.parse_all("4*(2+3)"))
 println(E.parse_all("(4)*(((2+3)))"))