progs/comb1.scala
changeset 673 715b46eee102
parent 672 e0d76f7f0688
child 683 c6c79d21f8a8
--- a/progs/comb1.scala	Sun Oct 27 11:57:57 2019 +0000
+++ b/progs/comb1.scala	Sun Oct 27 13:03:58 2019 +0000
@@ -102,12 +102,22 @@
 P.parse_all("()")
 
 // just counts parentheses
-lazy val PC : Parser[String, Int] = 
-  ("(" ~ PC ~ ")" ~ PC ==> { case (((_, x), _), y) => x + y + 2 } || 
-   "" ==> { (s) => 0 })
+lazy val P2 : Parser[String, Int] = 
+  ("(" ~ P2 ~ ")" ~ P2 ==> { case (((_, x), _), y) => x + y + 2 } || 
+   "" ==> { _ => 0 })
+
+P2.parse_all("(((()()))())")
+P2.parse_all("(((()()))()))")
 
-PC.parse_all("(((()()))())")
-P.parse_all("(((()()))()))")
+// counts opening and closing parentheses
+lazy val P3 : Parser[String, Int] = 
+  ("(" ~ P3 ==> { case (_, x) => x + 1 } ||
+   ")" ~ P3 ==> { case (_, x) => x - 1 } || 
+   "" ==> { _ => 0 })
+
+P3.parse_all("(((()()))())")
+P3.parse_all("(((()()))()))")
+P3.parse_all(")(")
 
 // Arithmetic Expressions (Terms and Factors)
 // (because it is mutually recursive, you need :paste