updated
authorChristian Urban <urbanc@in.tum.de>
Mon, 24 Oct 2016 14:37:46 +0100
changeset 464 20346b9587ff
parent 463 97a5956dead4 (diff)
parent 460 6a60e5ddd548 (current diff)
child 465 b5e1cfaf3319
updated
slides/slides05.pdf
Binary file hws/hw06.pdf has changed
--- a/progs/comb1.scala	Sat Oct 22 18:29:54 2016 +0100
+++ b/progs/comb1.scala	Mon Oct 24 14:37:46 2016 +0100
@@ -5,44 +5,36 @@
  * I <% Seq[_] , which means that the input type I can be
  * treated, or seen, as a sequence. */
 
-trait Input { 
-  def isEmpty: Boolean
-}
-
+abstract class Parser[I <% Seq[_], T] {
+  def parse(ts: I): Set[(T, I)]
 
-abstract class Parser[T] {
-
-  def parse(ts: Input): Set[(T, Input)]
-
-  def parse_all(ts: Input) : Set[T] =
+  def parse_all(ts: I) : Set[T] =
     for ((head, tail) <- parse(ts); 
         if (tail.isEmpty)) yield head
 }
 
-class SeqParser[T, S](p: => Parser[T], 
-                      q: => Parser[S]) extends Parser[(T, S)] {
-  def parse(sb: Input) = 
+class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], 
+                                   q: => Parser[I, S]) extends Parser[I, (T, S)] {
+  def parse(sb: I) = 
     for ((head1, tail1) <- p.parse(sb); 
          (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
 }
 
-class AltParser[T](p: => Parser[T], 
-                   q: => Parser[T]) extends Parser[T] {
-  def parse(sb: Input) = p.parse(sb) ++ q.parse(sb)   
+class AltParser[I <% Seq[_], T](p: => Parser[I, T], 
+                                q: => Parser[I, T]) extends Parser[I, T] {
+  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
 }
 
-class FunParser[T, S](p: => Parser[T], 
-                      f: T => S) extends Parser[S] {
-  def parse(sb: Input) = 
+class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], 
+                                   f: T => S) extends Parser[I, S] {
+  def parse(sb: I) = 
     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
 }
 
 // atomic parsers  
-case class CharParser(c: Char) extends Parser[Char] {
-  type Input = String
-
+case class CharParser(c: Char) extends Parser[String, Char] {
   def parse(sb: String) = 
-    if (sb.head == c) Set((c, sb.tail)) else Set()
+    if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
 }
 
 case class StringParser(s: String) extends Parser[String, String] {
@@ -63,7 +55,8 @@
 }
 
 // convenience
-implicit def string2parser(s : String) = StringParser(s)
+implicit def string2parser(s: String) = StringParser(s)
+implicit def char2parser(c: Char) = CharParser(c)
 
 implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
   def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
@@ -86,7 +79,7 @@
   (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
    ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "")
 
-println("Palindrom: " + Pal.parse_all("ababbaba"))
+println("Palindrome: " + Pal.parse_all("ababbaba"))
 
 // well-nested parenthesis parser
 lazy val P : Parser[String, String] = 
@@ -112,6 +105,20 @@
 println(E.parse_all("1+2+3"))  // this is not parsed, because of 
                                // how the grammar is set up
 
+// a repetition parser
+
+def RepParser[I  <% Seq[_], T](p: => Parser[I, T]): Parser[I, List[T]] = {
+  p ==> { case x => x :: Nil } ||
+  p ~ RepParser(p) ==> { case (x, y) => x :: y }   
+}
+
+
+// a repetition parser
+lazy val R : Parser[String, List[Char]] = RepParser('a') 
+println(R.parse_all("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
+
+
+
 // non-ambiguous vs ambiguous grammars
 lazy val S : Parser[String, String] =
   ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || ""
Binary file slides/slides05.pdf has changed
--- a/slides/slides05.tex	Sat Oct 22 18:29:54 2016 +0100
+++ b/slides/slides05.tex	Mon Oct 24 14:37:46 2016 +0100
@@ -39,6 +39,23 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[c]
+\frametitle{Survey: Thanks!}
+\small
+
+\begin{itemize}
+\item {\bf My Voice} ``could be a bit louder''
+\item {\bf Writing} ``sometimes a bit difficult to read''
+\item {\bf Recording} ``video caps of blackboard''
+\item ``It's all great''
+\end{itemize}
+  
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
+
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{frame}[c]
 \frametitle{\begin{tabular}{c}Last Week\\[-1mm] 
             Regexes and Values\end{tabular}}
 
--- a/style.sty	Sat Oct 22 18:29:54 2016 +0100
+++ b/style.sty	Mon Oct 24 14:37:46 2016 +0100
@@ -57,7 +57,7 @@
 
 \newcommand{\HEADER}{{\bf Please submit your solutions via email. Please submit 
 only ASCII text or PDFs. Every solution should be preceeded by the corresponding 
-question, like:
+question text, like:
 
 \begin{center}
 \begin{tabular}{ll}