# HG changeset patch # User Christian Urban # Date 1544132983 0 # Node ID e461b5325b5eae42f4fe6ffedbd46b4a951020f4 # Parent c989f50da5de85cf1aa7a242cc90da4386802d73 updated diff -r c989f50da5de -r e461b5325b5e solutions5/bfc.scala --- a/solutions5/bfc.scala Thu Dec 06 18:56:26 2018 +0000 +++ b/solutions5/bfc.scala Thu Dec 06 21:49:43 2018 +0000 @@ -1,7 +1,7 @@ // Part 2 about a "Compiler" for the Brainf*** language //====================================================== -object CW10b { +object CW10b { // only for producing the jar-file // !!! Copy any function you need from file bf.scala !!! // diff -r c989f50da5de -r e461b5325b5e testing4/re.scala --- a/testing4/re.scala Thu Dec 06 18:56:26 2018 +0000 +++ b/testing4/re.scala Thu Dec 06 21:49:43 2018 +0000 @@ -1,23 +1,21 @@ // Part 1 about Regular Expression Matching //========================================== -//object CW9a { - // Regular Expressions abstract class Rexp case object ZERO extends Rexp case object ONE extends Rexp case class CHAR(c: Char) extends Rexp -case class ALT(r1: Rexp, r2: Rexp) extends Rexp -case class SEQ(r1: Rexp, r2: Rexp) extends Rexp -case class STAR(r: Rexp) extends Rexp +case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative +case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence +case class STAR(r: Rexp) extends Rexp // star -// some convenience for typing in regular expressions + +// some convenience for typing regular expressions import scala.language.implicitConversions import scala.language.reflectiveCalls - def charlist2rexp(s: List[Char]): Rexp = s match { case Nil => ONE case c::Nil => CHAR(c) @@ -46,30 +44,38 @@ // accordingly. def nullable (r: Rexp) : Boolean = r match { - case ZERO => false - case ONE => true - case CHAR(_) => false - case ALT(r1, r2) => nullable(r1) || nullable(r2) - case SEQ(r1, r2) => nullable(r1) && nullable(r2) - case STAR(_) => true + case ZERO => false + case ONE => true + case CHAR(_) => false + case ALT(r1, r2) => nullable(r1) | nullable(r2) + case SEQ(r1, r2) => nullable(r1) & nullable(r2) + case STAR(_) => true } + + // (2) Complete the function der according to // the definition given in the coursework; this // function calculates the derivative of a // regular expression w.r.t. a character. +//TODO: debug +//TODO: understand this more. +// first test runs +// test 2 fails +// test 3 runs +// test 4 runs def der (c: Char, r: Rexp) : Rexp = r match { - case ZERO => ZERO - case ONE => ZERO - case CHAR(d) => if (c == d) ONE else ZERO - case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) - case SEQ(r1, r2) => - if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) - else SEQ(der(c, r1), r2) - case STAR(r1) => SEQ(der(c, r1), STAR(r1)) + //TODO: debug + case ZERO => ZERO + case ONE => ZERO + case CHAR(r1) => if (c == r1) ONE else ZERO + case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) + case SEQ(r1, r2) => if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) else SEQ(der(c, r1), r2) + case STAR(r1) => SEQ(der(c, r1), STAR(r1)) } + // (3) Complete the simp function according to // the specification given in the coursework; this // function simplifies a regular expression from @@ -78,19 +84,21 @@ // STAR-regular expressions. def simp(r: Rexp) : Rexp = r match { - case ALT(r1, r2) => (simp(r1), simp(r2)) match { - case (ZERO, r2s) => r2s - case (r1s, ZERO) => r1s - case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) - } - case SEQ(r1, r2) => (simp(r1), simp(r2)) match { - case (ZERO, _) => ZERO - case (_, ZERO) => ZERO - case (ONE, r2s) => r2s - case (r1s, ONE) => r1s - case (r1s, r2s) => SEQ(r1s, r2s) - } - case r => r + case STAR(_) => r + case SEQ(r1, r2) => (simp(r1), simp(r2)) match { // potential failure + case (_, ZERO) => ZERO + case (ZERO, _) => ZERO + case (r1, ONE) => simp(r1) + case (ONE, r2) => simp(r2) + case (r1, r2) => SEQ(r1, r2) + } + case ALT(r1, r2) => (simp(r1), simp(r2)) match { + case (r1, ZERO) => simp(r1) + case (ZERO, r1) => simp(r1) + case (r1, r2) if r1 == r2 => simp(r1) + case (r1, r2) => ALT(r1, r2) + } + case r => r } @@ -98,58 +106,60 @@ // calculates the derivative w.r.t. a string; the second // is the regular expression matcher taking a regular // expression and a string and checks whether the -// string matches the regular expression. +// string matches the regular expression def ders (s: List[Char], r: Rexp) : Rexp = s match { - case Nil => r - case c::s => ders(s, simp(der(c, r))) + case Nil => r + case c::cs => ders(cs, simp(der(c, r))) } -// main matcher function -def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r)) +def matcher(r: Rexp, s: String): Boolean = { + nullable(ders(s.toList, r)) +} + // (5) Complete the size function for regular // expressions according to the specification // given in the coursework. - def size(r: Rexp): Int = r match { - case ZERO => 1 - case ONE => 1 - case CHAR(_) => 1 - case ALT(r1, r2) => 1 + size(r1) + size (r2) - case SEQ(r1, r2) => 1 + size(r1) + size (r2) - case STAR(r1) => 1 + size(r1) + case ZERO => 1 + case ONE => 1 + case CHAR(_) => 1 + case SEQ(r1, r2) => 1 + size(r1) + size(r2) + case ALT(r1, r2) => 1 + size(r1) + size(r2) + case STAR(r1) => 1 + size(r1) } +// some testing data -// some testing data -/* -matcher(("a" ~ "b") ~ "c", "abc") // => true -matcher(("a" ~ "b") ~ "c", "ab") // => false +//matcher(("a" ~ "b") ~ "c", "abc") // => true +//matcher(("a" ~ "b") ~ "c", "ab") // => false // the supposedly 'evil' regular expression (a*)* b val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) -matcher(EVIL, "a" * 1000 ++ "b") // => true -matcher(EVIL, "a" * 1000) // => false +//matcher(EVIL, "a" * 1000 ++ "b") // => true +//matcher(EVIL, "a" * 1000) // => false // size without simplifications -size(der('a', der('a', EVIL))) // => 28 -size(der('a', der('a', der('a', EVIL)))) // => 58 +//size(der('a', der('a', EVIL))) // => 28 +//size(der('a', der('a', der('a', EVIL)))) // => 58 + + // size with simplification -size(simp(der('a', der('a', EVIL)))) // => 8 -size(simp(der('a', der('a', der('a', EVIL))))) // => 8 +//size(simp(der('a', der('a', EVIL)))) // => 8 +//size(simp(der('a', der('a', der('a', EVIL))))) // => 8 // Python needs around 30 seconds for matching 28 a's with EVIL. // Java 9 and later increase this to an "astonishing" 40000 a's in -// around 30 seconds. +// 30 seconds. // -// Lets see how long it takes to match strings with -// 5 Million a's...it should be in the range of a -// couple of seconds. +// Lets see how long it really takes to match strings with +// 5 Million a's...it should be in the range of a couple +// of seconds. def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() @@ -158,19 +168,17 @@ (end - start)/(i * 1.0e9) } -for (i <- 0 to 5000000 by 500000) { - println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i)))) -} +//for (i <- 0 to 5000000 by 500000) { +// println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i)))) +//} // another "power" test case -simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next) == ONE +println(simp(Iterator.iterate(ONE:Rexp)(r => ALT(r, r)).drop(40).next)) // the Iterator produces the rexp // // SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE) // -// where SEQ is nested 100 times. - -*/ +// where SEQ is nested 50 times. -//} + diff -r c989f50da5de -r e461b5325b5e testing4/re_test.sh --- a/testing4/re_test.sh Thu Dec 06 18:56:26 2018 +0000 +++ b/testing4/re_test.sh Thu Dec 06 21:49:43 2018 +0000 @@ -19,8 +19,7 @@ # functional tests function scala_assert { - (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" "$2" -e "") #2> /dev/null 1> /dev/null) - + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null) } # purity test diff -r c989f50da5de -r e461b5325b5e testing5/bf.scala --- a/testing5/bf.scala Thu Dec 06 18:56:26 2018 +0000 +++ b/testing5/bf.scala Thu Dec 06 21:49:43 2018 +0000 @@ -1,7 +1,7 @@ // Part 1 about an Interpreter for the Brainf*** language //======================================================== -object CW10a { // only for generating the Jar file +//object CW10a { // only for generating the Jar file type Mem = Map[Int, Int] @@ -11,9 +11,9 @@ import scala.util._ // (1) Write a function that takes a file name as argument and -// and requests the corresponding file from disk. It returns the +// and requests the corresponding file from disk. It Returns the // content of the file as a String. If the file does not exists, -// the function should return the empty string. +// the function should Return the empty string. def load_bff(name: String) : String = Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("") @@ -222,4 +222,4 @@ time_needed(1, run(b1)) */ -} +//} diff -r c989f50da5de -r e461b5325b5e testing5/bf1a_test.scala --- a/testing5/bf1a_test.scala Thu Dec 06 18:56:26 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ - -//import scala.concurrent._ -//import scala.concurrent.duration._ -//import ExecutionContext.Implicits.global -//import scala.language.postfixOps -//import scala.language.reflectiveCalls - -//lazy val f = Future { - import CW8b._ - - assert(sread(Map(), 2) == 0) - assert(sread(Map(2 -> 1), 2) == 1) - assert(write(Map(), 1, 2) == Map(1 -> 2)) - assert(write(Map(1 -> 0), 1, 2) == Map(1 -> 2)) -//} - -//Await.result(f, 120 second) diff -r c989f50da5de -r e461b5325b5e testing5/bf1b_test.scala --- a/testing5/bf1b_test.scala Thu Dec 06 18:56:26 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ - -//import scala.concurrent._ -//import scala.concurrent.duration._ -//import ExecutionContext.Implicits.global -//import scala.language.postfixOps -//import scala.language.reflectiveCalls - -//lazy val f = Future { - import CW8b._ - - assert(jumpRight("[******]***", 1, 0) == 8) - assert(jumpRight("[**[*]*]***", 1, 0) == 8) - assert(jumpRight("[**[*]*]***", 1, 0) == 8) - assert(jumpRight("[**[***]***", 1, 0) == 11) - assert(jumpRight("[*[][]*]***", 1, 0) == 8) - assert(jumpLeft("[******]***", 6, 0) == 1) - assert(jumpLeft("[******]***", 7, 0) == -1) - assert(jumpLeft("[*[][]*]***", 6, 0) == 1) -//} - -//Await.result(f, 120 second) diff -r c989f50da5de -r e461b5325b5e testing5/bf1c_test.scala --- a/testing5/bf1c_test.scala Thu Dec 06 18:56:26 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ - -//import scala.concurrent._ -//import scala.concurrent.duration._ -//import ExecutionContext.Implicits.global -//import scala.language.postfixOps -//import scala.language.reflectiveCalls - -//lazy val f = Future { - import CW8b._ - - assert(start("[-]", Map(0 -> 100)) == Map(0 -> 0)) - assert(start("[->+<]", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10)) - assert(start("[>>+>>+<<<<-]", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42)) - assert(start("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<] - <-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""", Map()) == Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87)) - -//} - -//Await.result(f, 120 second) diff -r c989f50da5de -r e461b5325b5e testing5/bf_test.sh --- a/testing5/bf_test.sh Thu Dec 06 18:56:26 2018 +0000 +++ b/testing5/bf_test.sh Thu Dec 06 21:49:43 2018 +0000 @@ -3,22 +3,22 @@ out=${1:-output} -echo "" > $out +echo -e "" > $out -echo "Below is the feedback for your submission of CW 8, Part 2." >> $out -echo "" >> $out +echo -e "Below is the feedback for your submission of CW 10, Part 1." >> $out +echo -e "" >> $out # compilation tests function scala_compile { - (ulimit -t 30 -m 1024000 ; scala "$1" 2>> $out 1>> $out) + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala "$1" 2>> $out 1>> $out) } # functional tests function scala_assert { - (ulimit -t 30 -m 1024000 ; scala -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null) + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null) } # purity test @@ -30,14 +30,14 @@ # var, return, ListBuffer test # -echo "bf.scala does not contain vars, returns etc?" >> $out +echo -e "bf.scala does not contain vars, returns etc?" >> $out if (scala_vars bf.scala) then - echo " --> fail" >> $out - tsts0=$(( 1 )) + echo -e " --> fail (make triple-sure your program conforms to the required format)" >> $out + tsts0=$(( 0 )) else - echo " --> success" >> $out + echo -e " --> success" >> $out tsts0=$(( 0 )) fi @@ -45,14 +45,14 @@ # compilation test if [ $tsts0 -eq 0 ] then - echo "bf.scala runs?" >> $out + echo -e "bf.scala runs?" >> $out if (scala_compile bf.scala) then - echo " --> success" >> $out + echo -e " --> success" >> $out tsts1=$(( 0 )) else - echo " --> scala bf.scala did not run successfully" >> $out + echo -e " --> --> SCALA DID NOT RUN BF.SCALA\nx" >> $out tsts1=$(( 1 )) fi else @@ -60,19 +60,33 @@ fi +### bf tests if [ $tsts1 -eq 0 ] then - echo " sread(Map(), 2) == 0" >> $out - echo " sread(Map(2 -> 1), 2) == 1" >> $out - echo " write(Map(), 1, 2) == Map(1 -> 2)" >> $out - echo " write(Map(1 -> 0), 1, 2) == Map(1 -> 2)" >> $out + echo -e " load_bff(\"benchmark.bf\").length == 188" >> $out + echo -e " load_bff(\"foobar.bf\") == \"\"" >> $out - if (scala_assert "bf.scala" "bf1a_test.scala") + if (scala_assert "bf.scala" "bf_test1.scala") then - echo " --> success" >> $out + echo -e " --> success" >> $out else - echo " --> test failed" >> $out + echo -e " --> \n ONE TEST FAILED\n" >> $out + fi +fi + +if [ $tsts1 -eq 0 ] +then + echo -e " sread(Map(), 2) == 0" >> $out + echo -e " sread(Map(2 -> 1), 2) == 1" >> $out + echo -e " write(Map(), 1, 2) == Map(1 -> 2)" >> $out + echo -e " write(Map(1 -> 0), 1, 2) == Map(1 -> 2)" >> $out + + if (scala_assert "bf.scala" "bf_test2.scala") + then + echo -e " --> success" >> $out + else + echo -e " --> \n ONE TEST FAILED\n" >> $out fi fi @@ -80,20 +94,20 @@ if [ $tsts1 -eq 0 ] then - echo " jumpRight(\"[******]***\", 1, 0) == 8" >> $out - echo " jumpRight(\"[**[*]*]***\", 1, 0) == 8" >> $out - echo " jumpRight(\"[**[*]*]***\", 1, 0) == 8" >> $out - echo " jumpRight(\"[**[***]***\", 1, 0) == 11" >> $out - echo " jumpRight(\"[*[][]*]***\", 1, 0) == 8" >> $out - echo " jumpLeft(\"[******]***\", 6, 0) == 1" >> $out - echo " jumpLeft(\"[******]***\", 7, 0) == -1" >> $out - echo " jumpLeft(\"[*[][]*]***\", 6, 0) == 1" >> $out + echo -e " jumpRight(\"[******]***\", 1, 0) == 8" >> $out + echo -e " jumpRight(\"[**[*]*]***\", 1, 0) == 8" >> $out + echo -e " jumpRight(\"[**[*]*]***\", 1, 0) == 8" >> $out + echo -e " jumpRight(\"[**[***]***\", 1, 0) == 11" >> $out + echo -e " jumpRight(\"[*[][]*]***\", 1, 0) == 8" >> $out + echo -e " jumpLeft(\"[******]***\", 6, 0) == 1" >> $out + echo -e " jumpLeft(\"[******]***\", 7, 0) == -1" >> $out + echo -e " jumpLeft(\"[*[][]*]***\", 6, 0) == 1" >> $out - if (scala_assert "bf.scala" "bf1b_test.scala") + if (scala_assert "bf.scala" "bf_test3.scala") then - echo " --> success" >> $out + echo -e " --> success" >> $out else - echo " --> test failed" >> $out + echo -e " --> \n ONE TEST FAILED\n" >> $out fi fi @@ -101,17 +115,17 @@ if [ $tsts1 -eq 0 ] then - echo " start(\"[-]\", Map(0 -> 100)) == Map(0 -> 0)" >> $out - echo " start(\"[->+<]\", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10)" >> $out - echo " start(\"[>>+>>+<<<<-]\", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42)" >> $out - echo " start({{hello world prg 1}}, Map()) == " >> $out - echo " Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87)" >> $out + echo -e " run(\"[-]\", Map(0 -> 100)) == Map(0 -> 0)" >> $out + echo -e " run(\"[->+<]\", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10)" >> $out + echo -e " run(\"[>>+>>+<<<<-]\", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42)" >> $out + echo -e " run(\"\"\"+++++[->++++++++++<]>--<+++[->>++++++++++" >> $out + echo -e " <<]>>++<<----------[+>.>.<+<]\"\"\") == Map(0 -> 0, 1 -> 58, 2 -> 32)" >> $out - if (scala_assert "bf.scala" "bf1c_test.scala") + if (scala_assert "bf.scala" "bf_test4.scala") then - echo " --> success" >> $out + echo -e " --> success" >> $out else - echo " --> test failed" >> $out + echo -e " --> \n ONE TEST FAILED\n" >> $out fi fi diff -r c989f50da5de -r e461b5325b5e testing5/bf_test1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing5/bf_test1.scala Thu Dec 06 21:49:43 2018 +0000 @@ -0,0 +1,3 @@ + +assert(load_bff("benchmark.bf").length == 188) +assert(load_bff("foobar.bf") == "") diff -r c989f50da5de -r e461b5325b5e testing5/bf_test2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing5/bf_test2.scala Thu Dec 06 21:49:43 2018 +0000 @@ -0,0 +1,5 @@ + +assert(sread(Map(), 2) == 0) +assert(sread(Map(2 -> 1), 2) == 1) +assert(write(Map(), 1, 2) == Map(1 -> 2)) +assert(write(Map(1 -> 0), 1, 2) == Map(1 -> 2)) diff -r c989f50da5de -r e461b5325b5e testing5/bf_test3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing5/bf_test3.scala Thu Dec 06 21:49:43 2018 +0000 @@ -0,0 +1,9 @@ + +assert(jumpRight("[******]***", 1, 0) == 8) +assert(jumpRight("[**[*]*]***", 1, 0) == 8) +assert(jumpRight("[**[*]*]***", 1, 0) == 8) +assert(jumpRight("[**[***]***", 1, 0) == 11) +assert(jumpRight("[*[][]*]***", 1, 0) == 8) +assert(jumpLeft("[******]***", 6, 0) == 1) +assert(jumpLeft("[******]***", 7, 0) == -1) +assert(jumpLeft("[*[][]*]***", 6, 0) == 1) diff -r c989f50da5de -r e461b5325b5e testing5/bf_test4.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing5/bf_test4.scala Thu Dec 06 21:49:43 2018 +0000 @@ -0,0 +1,8 @@ + +assert(run("[-]", Map(0 -> 100)) == Map(0 -> 0)) +assert(run("[->+<]", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10)) +assert(run("[>>+>>+<<<<-]", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42)) +val hw_urban = """+++++[->++++++++++<]>--<+++[->>++++++++++ + <<]>>++<<----------[+>.>.<+<]""" +assert(run(hw_urban) == Map(0 -> 0, 1 -> 58, 2 -> 32)) + diff -r c989f50da5de -r e461b5325b5e testing5/bfc.scala --- a/testing5/bfc.scala Thu Dec 06 18:56:26 2018 +0000 +++ b/testing5/bfc.scala Thu Dec 06 21:49:43 2018 +0000 @@ -1,7 +1,7 @@ // Part 2 about a "Compiler" for the Brainf*** language //====================================================== -object CW10b { +//object CW10b { // !!! Copy any function you need from file bf.scala !!! // @@ -306,4 +306,4 @@ //time_needed(1, run4(load_bff("mandelbrot.bf"))) -} +//} diff -r c989f50da5de -r e461b5325b5e testing5/mark --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing5/mark Thu Dec 06 21:49:43 2018 +0000 @@ -0,0 +1,16 @@ +#!/bin/bash +###set -e + +trap "exit" INT + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +cp $DIR/* . + +./bf_test.sh output1 + +echo -e "Here is an automated test report for your work so far on assignment 10. Please note that this is not the mark for your work; it is provided only in the hope that it is useful in developing your solution. Passing these tests does not guarantee your code is free from bugs: after the deadline, your code will be marked against a different, more thorough set of test cases.\n\n" > $1 + + +cat output1 >> $1 +