# HG changeset patch # User Christian Urban # Date 1605183531 0 # Node ID 7aab258bf72a1d27020ea9eb02fe7721ae65e037 # Parent 9eea6a801e10a5323362a7b7fc05d2bcd3b1fc6b updated diff -r 9eea6a801e10 -r 7aab258bf72a cws/cw03.pdf Binary file cws/cw03.pdf has changed diff -r 9eea6a801e10 -r 7aab258bf72a 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 9eea6a801e10 -r 7aab258bf72a 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 9eea6a801e10 -r 7aab258bf72a 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