updated
authorChristian Urban <urbanc@in.tum.de>
Sat, 07 Jan 2017 14:56:45 +0000
changeset 472 372f9801b76d
parent 471 9476086849ad (current diff)
parent 470 8df654e9eb4e (diff)
child 473 dc528091eb70
child 503 f2d7b885b3e3
updated
progs/while.scala
slides/slides06.pdf
--- a/progs/comb1.scala	Sat Jan 07 14:52:26 2017 +0000
+++ b/progs/comb1.scala	Sat Jan 07 14:56:45 2017 +0000
@@ -92,14 +92,15 @@
 
 // arithmetic expressions
 lazy val E: Parser[String, Int] = 
-  (F ~ "*" ~ F) ==> { case ((x, y), z) => x * z } || F 
+  (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } ||
+  (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T 
+lazy val T: Parser[String, Int] = 
+  ((F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F)
 lazy val F: Parser[String, Int] = 
-  ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + z } ||
-   (T ~ "-" ~ T) ==> { case ((x, y), z) => x - z } || T)
-lazy val T: Parser[String, Int] = 
   ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
 
-println(E.parse("1*2+3"))
+println(E.parse("4*2+3"))
+println(E.parse_all("4*2+3"))
 println(E.parse("1 + 2 * 3"))
 println(E.parse_all("(1+2)+3"))
 println(E.parse_all("1+2+3"))  // this is not parsed, because of 
--- a/progs/while.scala	Sat Jan 07 14:52:26 2017 +0000
+++ b/progs/while.scala	Sat Jan 07 14:56:45 2017 +0000
@@ -87,6 +87,19 @@
   }
 }
 
+def len(xs: List[(Int, Int)]): Int = xs match {
+  case Nil => 0
+  case (1, x)::xs => len(xs) + 1
+  case (_, x)::xs => len(xs) 
+}
+
+def fst(p: (Int, Int)): Int = p match {
+  case Nil => 0
+  case (1, x)::xs => len(xs) + 1
+  case (_, x)::xs => len(xs) 
+}
+
+
 
 // arithmetic expressions
 lazy val AExp: Parser[List[Token], AExp] = 
Binary file slides/slides06.pdf has changed