progs/comb1.scala
changeset 673 4e254e201ce9
parent 672 9d4c5abff8c5
child 683 9acbe46df3fd
equal deleted inserted replaced
672:9d4c5abff8c5 673:4e254e201ce9
   100 P.parse_all("(((()()))()))")
   100 P.parse_all("(((()()))()))")
   101 P.parse_all(")(")
   101 P.parse_all(")(")
   102 P.parse_all("()")
   102 P.parse_all("()")
   103 
   103 
   104 // just counts parentheses
   104 // just counts parentheses
   105 lazy val PC : Parser[String, Int] = 
   105 lazy val P2 : Parser[String, Int] = 
   106   ("(" ~ PC ~ ")" ~ PC ==> { case (((_, x), _), y) => x + y + 2 } || 
   106   ("(" ~ P2 ~ ")" ~ P2 ==> { case (((_, x), _), y) => x + y + 2 } || 
   107    "" ==> { (s) => 0 })
   107    "" ==> { _ => 0 })
   108 
   108 
   109 PC.parse_all("(((()()))())")
   109 P2.parse_all("(((()()))())")
   110 P.parse_all("(((()()))()))")
   110 P2.parse_all("(((()()))()))")
       
   111 
       
   112 // counts opening and closing parentheses
       
   113 lazy val P3 : Parser[String, Int] = 
       
   114   ("(" ~ P3 ==> { case (_, x) => x + 1 } ||
       
   115    ")" ~ P3 ==> { case (_, x) => x - 1 } || 
       
   116    "" ==> { _ => 0 })
       
   117 
       
   118 P3.parse_all("(((()()))())")
       
   119 P3.parse_all("(((()()))()))")
       
   120 P3.parse_all(")(")
   111 
   121 
   112 // Arithmetic Expressions (Terms and Factors)
   122 // Arithmetic Expressions (Terms and Factors)
   113 // (because it is mutually recursive, you need :paste 
   123 // (because it is mutually recursive, you need :paste 
   114 //  for munching this definition in the REPL)
   124 //  for munching this definition in the REPL)
   115 
   125