progs/comb1.scala
changeset 366 5a83336a9690
parent 362 57ea439feaff
child 367 04127a5aad23
--- a/progs/comb1.scala	Fri Nov 06 04:54:41 2015 +0000
+++ b/progs/comb1.scala	Fri Nov 06 08:52:16 2015 +0000
@@ -89,7 +89,7 @@
 
 // arithmetic expressions
 lazy val E: Parser[String, String] = 
-  (F ~ "*" ~ F) ==> { case ((x, y), z) => x + y + z } || F  
+  (F ~ "*" ~ T) ==> { case ((x, y), z) => x + y + z } || F  
 lazy val F: Parser[String, String] = 
   ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + y + z } ||
    (T ~ "-" ~ T) ==> { case ((x, y), z) => x + y + z } || T)
@@ -103,41 +103,39 @@
                                // how the grammar is set up
 
 // non-ambiguous vs ambiguous grammars
-lazy val U : Parser[String, String] =
-  ("1" ~ U) ==> { case (x, y) => x + y } || ""
-
-U.parse_all("1" * 100 + "0")
-
 lazy val S : Parser[String, String] =
   ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || ""
 
 S.parse_all("1" * 15)
 
-
+lazy val U : Parser[String, String] =
+  ("1" ~ U) ==> { case (x, y) => x + y  } || ""
 
-
-
+U.parse("11")
+U.parse("11111")
+U.parse("11011")
 
-
-
+U.parse_all("1" * 100 + "0")
 
+lazy val UCount : Parser[String, Int] =
+  ("1" ~ UCount) ==> { case (x, y) => y + 1 } || 
+  "" ==> { (x) => 0 }
 
-
+UCount.parse("11111")
+UCount.parse_all("11111")
 
 
 
 
-
-
-
-
-
-
+// Single Character parser
+lazy val One : Parser[String, String] = "1"
+lazy val Two : Parser[String, String] = "2"
 
-
-
-
+One.parse("1")
+One.parse("111")
 
-
+(One ~ One).parse("111")
+(One ~ One ~ One).parse("111")
+(One ~ One ~ One ~ One).parse("1111")
 
-
+(One || Two).parse("111")