# HG changeset patch # User Christian Urban # Date 1481886821 0 # Node ID 8df654e9eb4ef7db5b0bac116dde197275db068f # Parent 1f4e81950ab4e7d7c13d33bbadcd49ad7944f85f updated diff -r 1f4e81950ab4 -r 8df654e9eb4e progs/comb1.scala --- a/progs/comb1.scala Mon Nov 14 15:50:42 2016 +0000 +++ b/progs/comb1.scala Fri Dec 16 11:13:41 2016 +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 diff -r 1f4e81950ab4 -r 8df654e9eb4e progs/while.scala --- a/progs/while.scala Mon Nov 14 15:50:42 2016 +0000 +++ b/progs/while.scala Fri Dec 16 11:13:41 2016 +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] = diff -r 1f4e81950ab4 -r 8df654e9eb4e slides/slides06.pdf Binary file slides/slides06.pdf has changed