# HG changeset patch # User Christian Urban # Date 1605183531 0 # Node ID b17a43f80ed02f4168d664ba3bdbd36c9bc498bb # Parent bdd731e4edbf9b1a15213183122d98103b99aa3a updated diff -r bdd731e4edbf -r b17a43f80ed0 cws/cw03.pdf Binary file cws/cw03.pdf has changed diff -r bdd731e4edbf -r b17a43f80ed0 cws/cw03.tex --- a/cws/cw03.tex Sat Nov 07 00:07:28 2020 +0000 +++ b/cws/cw03.tex Thu Nov 12 12:18:51 2020 +0000 @@ -55,7 +55,7 @@ list, of \emph{tokens} generated by the tokenizer from the previous coursework. For this you might want to filter out whitespaces and comments. Your parser should be able to handle the WHILE programs in -Figures~\ref{fib}, \ref{loop} and \ref{primes}. In addition give the +Figures~\ref{fib} -- \ref{collatz}. In addition give the parse tree for the statement: \begin{lstlisting}[language=While,numbers=none] diff -r bdd731e4edbf -r b17a43f80ed0 progs/parser-combinators/comb1.sc --- a/progs/parser-combinators/comb1.sc Sat Nov 07 00:07:28 2020 +0000 +++ b/progs/parser-combinators/comb1.sc Thu Nov 12 12:18:51 2020 +0000 @@ -79,9 +79,13 @@ // p"<_some_string_>" implicit def parser_interpolation(sc: StringContext) = new { - def p(args: Any*) = StrParser(sc.s(args:_*)) + def p(args: Any*) = TokParser(sc.s(args:_*)) } +p"while" ==> StrParser[String,....] + TokParser[List[Token],....] + +for x := 3 to 10 // more convenient syntax for parser combinators implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new { @@ -102,7 +106,7 @@ // A parser for palindromes (just returns them as string) -lazy val Pal : Parser[String, String] = { +lazy val Pal : Parser[List[Token], String] = { (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } || p"a" || p"b" || p"" diff -r bdd731e4edbf -r b17a43f80ed0 progs/pow.scala --- a/progs/pow.scala Sat Nov 07 00:07:28 2020 +0000 +++ b/progs/pow.scala Thu Nov 12 12:18:51 2020 +0000 @@ -6,6 +6,12 @@ case n => concat(A, pow(A, n- 1)) } +val A = Set("a", "b", "c", "d", "e") +val B = Set("a", "b", "c", "d", "") +pow(A, 4).size +pow(B, 4).size + + val A = Set("aa", "a") val B = Set("aaa", "aaaa") concat(A, B).size // -> 3