The primary learning goals of this assignment are to build intuition for the following:
working with abstract syntax trees;
how grammars are used to specify the syntax of programming languages; and
the distinction between concrete and abstract syntax.
Instructions
This assignment asks you to write Scala code. There are restrictions associated with how you can solve these problems. Please pay careful heed to those. If you are unsure, ask the course staff.
Note that ??? indicates that there is a missing function or code fragment that needs to be filled in. Make sure that you remove the ??? and replace it with the answer.
Use the test cases provided to test your implementations. You are also encouraged to write your own test cases to help debug your work. However, please delete any extra cells you may have created lest they break an autograder.
Imports
org.scalatest._
// Run this cell FIRST before testing.import $ivy.`org.scalatest::scalatest:3.2.19`, org.scalatest._, events._, flatspec._defreport(suite: Suite):Unit= suite.execute(stats =true)defassertPassed(suite: Suite):Unit= suite.run(None,Args(new Reporter {defapply(e:Event)= e match{case e @ (_: TestFailed)=>assert(false,s"${e.message} (${e.testName})")case _ =>()}}))defpassed(points:Int):Unit={require(points >=0)if(points ==1)println("*** 🎉 Tests Passed (1 point) ***")elseprintln(s"*** 🎉 Tests Passed ($points points) ***")}deftest(suite: Suite, points:Int):Unit={report(suite)assertPassed(suite)passed(points)}
import $ivy.$ , org.scalatest._, events._, flatspec._
defined functionreport
defined functionassertPassed
defined functionpassed
defined functiontest
scala.util.parsing.combinator._
// Run this cell FIRST before building your parser.import $ivy.`org.scala-lang.modules::scala-parser-combinators:2.4.0`
import $ivy.$
13.1 Abstract Syntax Trees
Let us consider the (abstract) syntax of a language of Boolean expressions:
Exercise 13.1 (5 points) Define an inductive data type BExpr in Scala following the above grammar. Use the the names given above for the constructors, and use the Scala type String to represent variable names.
Edit this cell:
sealedtrait BExpr???
scala.NotImplementedError: an implementation is missing
scala.Predef$.$qmark$qmark$qmark(Predef.scala:345)
ammonite.$sess.cmd2$Helper.<init>(cmd2.sc:2)
ammonite.$sess.cmd2$.<clinit>(cmd2.sc:7)
cmd3.sc:1: not found: type BExpr
val a: BExpr = Var("A")
^cmd3.sc:2: not found: type BExpr
val b: BExpr = Var("B")
^cmd3.sc:3: not found: type BExpr
val c: BExpr = Var("C")
^cmd3.sc:4: not found: type BExpr
val not_b: BExpr = Not(b)
^cmd3.sc:5: not found: type BExpr
val not_a: BExpr = Not(a)
^cmd3.sc:6: not found: type BExpr
val e1: BExpr = And(a,not_b)
^cmd3.sc:7: not found: type BExpr
val e2: BExpr = And(b, not_a)
^cmd3.sc:8: not found: type BExpr
val e3: BExpr = Or(e1,e2)
^cmd3.sc:1: not found: value Var
val a: BExpr = Var("A")
^cmd3.sc:2: not found: value Var
val b: BExpr = Var("B")
^cmd3.sc:3: not found: value Var
val c: BExpr = Var("C")
^cmd3.sc:4: not found: value Not
val not_b: BExpr = Not(b)
^cmd3.sc:5: not found: value Not
val not_a: BExpr = Not(a)
^cmd3.sc:6: not found: value And
val e1: BExpr = And(a,not_b)
^cmd3.sc:7: not found: value And
val e2: BExpr = And(b, not_a)
^cmd3.sc:8: not found: value Or
val e3: BExpr = Or(e1,e2)
^Compilation Failed
:
Compilation Failed
13.1.2 Converting to Negation Normal Form
Exercise 13.2 (10 points) Write a function nnf to convert Boolean formulas from the above grammar to their Negation Normal Form (NNF). A Boolean formula is said to be in NNF iff the negation operator \(\neg\) (i.e., \(\texttt{Not}\)) is only applied to the variables. For example, the following formula is in NNF because negation is only applied to \(A\) or \(B\), or both of which are variables:
\[
(A \wedge \neg B) \vee (B \land \neg A) \;.
\]
On the other hand, the following formula is not in NNF as negation is applied to a complex expression:
\[
\neg(A \vee \neg B) \wedge (\neg B \vee C) \;.
\]
The negation normal form of the above formula is as follows:
\[
(\neg A \wedge \neg B) \wedge (\neg B \vee C) \;.
\]
Edit this cell:
defnnf(e: BExpr): BExpr =// Hint: There are 7 cases.???
cmd3.sc:1: not found: type BExpr
def nnf(e: BExpr): BExpr =
^cmd3.sc:1: not found: type BExpr
def nnf(e: BExpr): BExpr =
^Compilation Failed
:
Compilation Failed
???
Notes
In general, a formula can be converted to NNF by applying three rules:
Rule 1
Double negation is cancelled: \(\neg(\neg e) = e\) for any Boolean formula \(e\).
Rule 2
De Morgan’s Law for conjunction: \(\neg(e_1 \wedge e_2) = \neg e_1 \vee \neg e_2\) for any two Boolean formulas \(e_1\) and \(e_2\).
Rule 3
De Morgan’s Law for disjunction: \(\neg(e_1 \vee e_2) = \neg e_1 \wedge \neg e_2\) for any two Boolean formulas \(e_1\) and \(e_2\).
The nnf function will have a case for each of the above rule. In addition, the function will also need to handle the following cases:
A variable \(x\) or its negation \(\neg x\) is already in NNF.
An expression \(e_1 \wedge e_2\) is in NNF iff \(e_1\) and \(e_2\) are in NNF.
An expression \(e_1 \vee e_2\) is in NNF iff \(e_1\) and \(e_2\) are in NNF.
If you handle all the 7 cases described above, then your nnf function will likely be correct, though of course, you will want to test it.
Tests
test(new AnyFlatSpec {val testcase1 =Var("A")val testcase2 =Not(Not(Var("B")))val testcase3 =And(Var("A"),Or(Var("B"),Var("C")))val testcase4 =Or(Var("A"),And(Var("B"),Var("C")))val testcase5 =Not(And(Var("A"),Var("B")))val testcase6 =Not(Or(Var("A"),Var("B")))val testcase7 =Or(And(testcase5,testcase6),testcase2)"nnf" should "Test Case 1" in {assertResult(Var("A")){nnf(testcase1)}} it should "Test Case 2" in {assertResult(Var("B")){nnf(testcase2)}} it should "Test Case 3" in {assertResult(And(Var("A"),Or(Var("B"),Var("C")))){nnf(testcase3)}} it should "Test Case 4" in {assertResult(Or(Var("A"),And(Var("B"),Var("C")))){nnf(testcase4)}} it should "Test Case 5" in {assertResult(Or(Not(Var("A")),Not(Var("B")))){nnf(testcase5)}} it should "Test Case 6" in {assertResult(And(Not(Var("A")),Not(Var("B")))){nnf(testcase6)}} it should "Test Case 7" in {assertResult(Or(And(Or(Not(Var("A")),Not(Var("B"))),And(Not(Var("A")),Not(Var("B")))),Var("B"))){nnf(testcase7)}}},10)
cmd3.sc:2: not found: value Var
val testcase1 = Var("A")
^cmd3.sc:3: not found: value Not
val testcase2 = Not(Not(Var("B")))
^cmd3.sc:3: not found: value Not
val testcase2 = Not(Not(Var("B")))
^cmd3.sc:3: not found: value Var
val testcase2 = Not(Not(Var("B")))
^cmd3.sc:4: not found: value And
val testcase3 = And(Var("A"), Or(Var("B"), Var("C")))
^cmd3.sc:4: not found: value Var
val testcase3 = And(Var("A"), Or(Var("B"), Var("C")))
^cmd3.sc:4: not found: value Or
val testcase3 = And(Var("A"), Or(Var("B"), Var("C")))
^cmd3.sc:4: not found: value Var
val testcase3 = And(Var("A"), Or(Var("B"), Var("C")))
^cmd3.sc:4: not found: value Var
val testcase3 = And(Var("A"), Or(Var("B"), Var("C")))
^cmd3.sc:5: not found: value Or
val testcase4 = Or(Var("A"), And(Var("B"), Var("C")))
^cmd3.sc:5: not found: value Var
val testcase4 = Or(Var("A"), And(Var("B"), Var("C")))
^cmd3.sc:5: not found: value And
val testcase4 = Or(Var("A"), And(Var("B"), Var("C")))
^cmd3.sc:5: not found: value Var
val testcase4 = Or(Var("A"), And(Var("B"), Var("C")))
^cmd3.sc:5: not found: value Var
val testcase4 = Or(Var("A"), And(Var("B"), Var("C")))
^cmd3.sc:6: not found: value Not
val testcase5 = Not(And(Var("A"), Var("B")))
^cmd3.sc:6: not found: value And
val testcase5 = Not(And(Var("A"), Var("B")))
^cmd3.sc:6: not found: value Var
val testcase5 = Not(And(Var("A"), Var("B")))
^cmd3.sc:6: not found: value Var
val testcase5 = Not(And(Var("A"), Var("B")))
^cmd3.sc:7: not found: value Not
val testcase6 = Not(Or(Var("A"), Var("B")))
^cmd3.sc:7: not found: value Or
val testcase6 = Not(Or(Var("A"), Var("B")))
^cmd3.sc:7: not found: value Var
val testcase6 = Not(Or(Var("A"), Var("B")))
^cmd3.sc:7: not found: value Var
val testcase6 = Not(Or(Var("A"), Var("B")))
^cmd3.sc:8: not found: value Or
val testcase7 = Or(And(testcase5,testcase6),testcase2)
^cmd3.sc:8: not found: value And
val testcase7 = Or(And(testcase5,testcase6),testcase2)
^cmd3.sc:10: not found: value Var
"nnf" should "Test Case 1" in { assertResult( Var("A") ) { nnf(testcase1)} }
^cmd3.sc:10: not found: value nnf
"nnf" should "Test Case 1" in { assertResult( Var("A") ) { nnf(testcase1)} }
^cmd3.sc:11: not found: value Var
it should "Test Case 2" in { assertResult( Var("B") ) { nnf(testcase2) } }
^cmd3.sc:11: not found: value nnf
it should "Test Case 2" in { assertResult( Var("B") ) { nnf(testcase2) } }
^cmd3.sc:12: not found: value And
it should "Test Case 3" in { assertResult( And(Var("A"), Or(Var("B"), Var("C"))) ) { nnf(testcase3) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 3" in { assertResult( And(Var("A"), Or(Var("B"), Var("C"))) ) { nnf(testcase3) } }
^cmd3.sc:12: not found: value Or
it should "Test Case 3" in { assertResult( And(Var("A"), Or(Var("B"), Var("C"))) ) { nnf(testcase3) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 3" in { assertResult( And(Var("A"), Or(Var("B"), Var("C"))) ) { nnf(testcase3) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 3" in { assertResult( And(Var("A"), Or(Var("B"), Var("C"))) ) { nnf(testcase3) } }
^cmd3.sc:12: not found: value nnf
it should "Test Case 3" in { assertResult( And(Var("A"), Or(Var("B"), Var("C"))) ) { nnf(testcase3) } }
^cmd3.sc:13: not found: value Or
it should "Test Case 4" in { assertResult( Or(Var("A"), And(Var("B"), Var("C"))) ) { nnf(testcase4) } }
^cmd3.sc:13: not found: value Var
it should "Test Case 4" in { assertResult( Or(Var("A"), And(Var("B"), Var("C"))) ) { nnf(testcase4) } }
^cmd3.sc:13: not found: value And
it should "Test Case 4" in { assertResult( Or(Var("A"), And(Var("B"), Var("C"))) ) { nnf(testcase4) } }
^cmd3.sc:13: not found: value Var
it should "Test Case 4" in { assertResult( Or(Var("A"), And(Var("B"), Var("C"))) ) { nnf(testcase4) } }
^cmd3.sc:13: not found: value Var
it should "Test Case 4" in { assertResult( Or(Var("A"), And(Var("B"), Var("C"))) ) { nnf(testcase4) } }
^cmd3.sc:13: not found: value nnf
it should "Test Case 4" in { assertResult( Or(Var("A"), And(Var("B"), Var("C"))) ) { nnf(testcase4) } }
^cmd3.sc:14: not found: value Or
it should "Test Case 5" in { assertResult( Or(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase5) } }
^cmd3.sc:14: not found: value Not
it should "Test Case 5" in { assertResult( Or(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase5) } }
^cmd3.sc:14: not found: value Var
it should "Test Case 5" in { assertResult( Or(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase5) } }
^cmd3.sc:14: not found: value Not
it should "Test Case 5" in { assertResult( Or(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase5) } }
^cmd3.sc:14: not found: value Var
it should "Test Case 5" in { assertResult( Or(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase5) } }
^cmd3.sc:14: not found: value nnf
it should "Test Case 5" in { assertResult( Or(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase5) } }
^cmd3.sc:15: not found: value And
it should "Test Case 6" in { assertResult( And(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase6) } }
^cmd3.sc:15: not found: value Not
it should "Test Case 6" in { assertResult( And(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase6) } }
^cmd3.sc:15: not found: value Var
it should "Test Case 6" in { assertResult( And(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase6) } }
^cmd3.sc:15: not found: value Not
it should "Test Case 6" in { assertResult( And(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase6) } }
^cmd3.sc:15: not found: value Var
it should "Test Case 6" in { assertResult( And(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase6) } }
^cmd3.sc:15: not found: value nnf
it should "Test Case 6" in { assertResult( And(Not(Var("A")), Not(Var("B"))) ) { nnf(testcase6) } }
^cmd3.sc:16: not found: value Or
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value And
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Or
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Not
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Var
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Not
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Var
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value And
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Not
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Var
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Not
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Var
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value Var
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^cmd3.sc:16: not found: value nnf
it should "Test Case 7" in { assertResult( Or(And(Or(Not(Var("A")), Not(Var("B"))), And(Not(Var("A")), Not(Var("B")))), Var("B")) ) { nnf(testcase7) } }
^Compilation Failed
:
Compilation Failed
13.1.3 Substitution
Exercise 13.3 (5 points) Write a function subst that substitutes in a given Boolean expression with another expression for a given variable name. For example, substituting in \[
\neg(A \vee B) \wedge (\neg B \vee C)
\] with \(D\) for \(B\) yields \[
\neg(A \vee D) \wedge (\neg D \vee C) \;.
\]
cmd3.sc:1: not found: type BExpr
def subst(with_e: BExpr, for_x: String, in_e: BExpr): BExpr =
^cmd3.sc:1: not found: type BExpr
def subst(with_e: BExpr, for_x: String, in_e: BExpr): BExpr =
^cmd3.sc:1: not found: type BExpr
def subst(with_e: BExpr, for_x: String, in_e: BExpr): BExpr =
^Compilation Failed
:
Compilation Failed
???
Tests
test(new AnyFlatSpec {val testcase1 =Var("A")val testcase2 =Not(Var("B"))val testcase3 =Or(Var("A"),Var("B"))val testcase4 =And(Not(Var("C")),Or(Var("D"),Var("E")))val testcase5 =Or(And(Var("X"),Var("Y")),Or(Var("Z"),Var("X"))) it should "Test Case 1" in {assertResult(Var("X")){subst(Var("X"),"A", testcase1)}} it should "Test Case 2" in {assertResult(Not(Var("Y"))){subst(Var("Y"),"B", testcase2)}} it should "Test Case 3" in {assertResult(Or(Var("C"),Var("B"))){subst(Var("C"),"A", testcase3)}} it should "Test Case 4" in {assertResult(And(Not(Var("C")),Or(Var("D"),Var("F")))){subst(Var("F"),"E", testcase4)}} it should "Test Case 5" in {assertResult(Or(And(Var("W"),Var("Y")),Or(Var("Z"),Var("W")))){subst(Var("W"),"X", testcase5)}}},5)
cmd3.sc:2: not found: value Var
val testcase1 = Var("A")
^cmd3.sc:3: not found: value Not
val testcase2 = Not(Var("B"))
^cmd3.sc:3: not found: value Var
val testcase2 = Not(Var("B"))
^cmd3.sc:4: not found: value Or
val testcase3 = Or(Var("A"), Var("B"))
^cmd3.sc:4: not found: value Var
val testcase3 = Or(Var("A"), Var("B"))
^cmd3.sc:4: not found: value Var
val testcase3 = Or(Var("A"), Var("B"))
^cmd3.sc:5: not found: value And
val testcase4 = And(Not(Var("C")), Or(Var("D"), Var("E")))
^cmd3.sc:5: not found: value Not
val testcase4 = And(Not(Var("C")), Or(Var("D"), Var("E")))
^cmd3.sc:5: not found: value Var
val testcase4 = And(Not(Var("C")), Or(Var("D"), Var("E")))
^cmd3.sc:5: not found: value Or
val testcase4 = And(Not(Var("C")), Or(Var("D"), Var("E")))
^cmd3.sc:5: not found: value Var
val testcase4 = And(Not(Var("C")), Or(Var("D"), Var("E")))
^cmd3.sc:5: not found: value Var
val testcase4 = And(Not(Var("C")), Or(Var("D"), Var("E")))
^cmd3.sc:6: not found: value Or
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:6: not found: value And
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:6: not found: value Var
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:6: not found: value Var
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:6: not found: value Or
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:6: not found: value Var
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:6: not found: value Var
val testcase5 = Or(And(Var("X"), Var("Y")), Or(Var("Z"), Var("X")))
^cmd3.sc:8: not found: value Var
it should "Test Case 1" in { assertResult( Var("X") ) { subst(Var("X"), "A", testcase1) } }
^cmd3.sc:8: not found: value subst
it should "Test Case 1" in { assertResult( Var("X") ) { subst(Var("X"), "A", testcase1) } }
^cmd3.sc:8: not found: value Var
it should "Test Case 1" in { assertResult( Var("X") ) { subst(Var("X"), "A", testcase1) } }
^cmd3.sc:9: not found: value Not
it should "Test Case 2" in { assertResult( Not(Var("Y")) ) { subst(Var("Y"), "B", testcase2) } }
^cmd3.sc:9: not found: value Var
it should "Test Case 2" in { assertResult( Not(Var("Y")) ) { subst(Var("Y"), "B", testcase2) } }
^cmd3.sc:9: not found: value subst
it should "Test Case 2" in { assertResult( Not(Var("Y")) ) { subst(Var("Y"), "B", testcase2) } }
^cmd3.sc:9: not found: value Var
it should "Test Case 2" in { assertResult( Not(Var("Y")) ) { subst(Var("Y"), "B", testcase2) } }
^cmd3.sc:10: not found: value Or
it should "Test Case 3" in { assertResult( Or(Var("C"), Var("B")) ) { subst(Var("C"), "A", testcase3) } }
^cmd3.sc:10: not found: value Var
it should "Test Case 3" in { assertResult( Or(Var("C"), Var("B")) ) { subst(Var("C"), "A", testcase3) } }
^cmd3.sc:10: not found: value Var
it should "Test Case 3" in { assertResult( Or(Var("C"), Var("B")) ) { subst(Var("C"), "A", testcase3) } }
^cmd3.sc:10: not found: value subst
it should "Test Case 3" in { assertResult( Or(Var("C"), Var("B")) ) { subst(Var("C"), "A", testcase3) } }
^cmd3.sc:10: not found: value Var
it should "Test Case 3" in { assertResult( Or(Var("C"), Var("B")) ) { subst(Var("C"), "A", testcase3) } }
^cmd3.sc:11: not found: value And
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value Not
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value Var
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value Or
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value Var
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value Var
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value subst
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:11: not found: value Var
it should "Test Case 4" in { assertResult( And(Not(Var("C")), Or(Var("D"), Var("F"))) ) { subst(Var("F"), "E", testcase4) } }
^cmd3.sc:12: not found: value Or
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value And
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value Or
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value subst
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^cmd3.sc:12: not found: value Var
it should "Test Case 5" in { assertResult( Or(And(Var("W"), Var("Y")), Or(Var("Z"), Var("W"))) ) { subst(Var("W"), "X", testcase5) } }
^Compilation Failed
Exercise 13.4 (5 points) Write Scala expressions to determine if \(\texttt{-}\) has higher precedence than \(\texttt{<<}\) or vice versa. To do, write an expression bound to e_no_parens that uses no parentheses. Then, bind to e_higher_- the expression that adds parentheses to the e_no_parens expression corresponding to the case if \(\texttt{-}\) has higher precedence than \(\texttt{<<}\), and bind to e_higher_<< the expression adds parentheses corresponding to the other case.
Edit this cell:
val e_no_parens =???val e_higher_-=???val e_higher_<<=???
scala.NotImplementedError: an implementation is missing
scala.Predef$.$qmark$qmark$qmark(Predef.scala:345)
ammonite.$sess.cmd3$Helper.<init>(cmd3.sc:2)
ammonite.$sess.cmd3$.<clinit>(cmd3.sc:7)
Make sure that you are checking for precedence and not for left or right associativity.
???
val e_higher_- = ???
val e_higher_<< = ???
- Partial implementation, but there are significant issues in the Scala expressions.
- Major issues:
- Doesn't check for operator precedence between `-` and `<<`.
- Incorrect or missing parentheses in `e_higher_-` or `e_higher_<<`.
- Fail most test cases.
**Approaching (A)**
val e_no_parens = ???
val e_higher_- = ???
val e_higher_<< = ???
- Mostly correct implementation, but might fail some test cases or have minor errors.
- Attempts to check for relative precedence of `-` and `<<` with parentheses but might misplace them.
**Proficient (P) or Exceeding (E)**
val e_no_parens = ???
val e_higher_- = ???
val e_higher_<< = ???
- Fully correct implementation of all expressions.
- Correctly binds `e_no_parens`, `e_higher_-`, and `e_higher_<<` to appropriately test for precedence of `-` and `<<`.
- Passes all test cases, including:
- Ensuring that e_no_parens == e_higher_- or e_no_parens == e_higher_<<.
- Ensuring that e_higher_- != e_higher_<<.
// END SOLUTION
:::
#### Assertions {.unnumbered}
:::: {.content-hidden when-format="pdf"}
::: {#104f38ec .cell nbgrader='{"grade":true,"grade_id":"detective_tests","locked":true,"points":5,"schema_version":3,"solution":false,"task":false}' execution_count=11}
``` {.scala .cell-code}
assert(e_no_parens == e_higher_- || e_no_parens == e_higher_<<)
assert(e_higher_- != e_higher_<<)
passed(5)
cmd4.sc:1: not found: value e_no_parens
val res4_0 = assert(e_no_parens == e_higher_- || e_no_parens == e_higher_<<)
^cmd4.sc:1: not found: value e_higher_-
val res4_0 = assert(e_no_parens == e_higher_- || e_no_parens == e_higher_<<)
^cmd4.sc:1: not found: value e_no_parens
val res4_0 = assert(e_no_parens == e_higher_- || e_no_parens == e_higher_<<)
^cmd4.sc:1: not found: value e_higher_<<
val res4_0 = assert(e_no_parens == e_higher_- || e_no_parens == e_higher_<<)
^cmd4.sc:2: not found: value e_higher_-
val res4_1 = assert(e_higher_- != e_higher_<<)
^cmd4.sc:2: not found: value e_higher_<<
val res4_1 = assert(e_higher_- != e_higher_<<)
^Compilation Failed
:
Compilation Failed
::::
Explanation
Exercise 13.5 (5 points) Explain how you arrived at the relative precedence of \(\texttt{-}\) and \(\texttt{<<}\) based on the output that you saw in the Scala interpreter.
defined traitParseTree
defined classLeaf
defined classNode
Observe that a ParseTree is just an \(n\)-ary tree containing Strings. A Leaf is a terminal containing the lexemes (i.e., letters from the alphabet), while a Node is represents a non-terminal with a String for the name of the non-terminal a list of childrenParseTrees.
For example, the following are ParseTrees for this grammar (Equation 13.1):
val p1 =Node("x",Leaf("a"):: Nil)val p2 =Node("e",Node("x",Leaf("a"):: Nil):: Nil)
p1: Node = Node(nonterm = "x", children = List(Leaf(term = "a")))
p2: Node = Node(
nonterm = "e",
children = List(Node(nonterm = "x", children = List(Leaf(term = "a"))))
)
We provide a function that pretty-prints a ParseTree for this grammar (Equation 13.1).
defpretty(t: ParseTree):Option[String]={val alphabet =Set("a","b","?",":") t match{// e ::= xcaseNode("e",(x @ Node("x", _)):: Nil)=>pretty(x)// e ::= e ? e : ecaseNode("e",(e1 @ Node("e", _))::Leaf("?")::(e2 @ Node("e", _))::Leaf(":")::(e3 @ Node("e", _)):: Nil)=>for{ s1 <-pretty(e1); s2 <-pretty(e2); s3 <-pretty(e3)}yields"$s1 ? $s2 : $s3"// x ::= acaseNode("x",Leaf("a"):: Nil)=>Some("a")// x ::= bcaseNode("x",Leaf("b"):: Nil)=>Some("b")// failurecase _ =>None}}pretty(p1)pretty(p2)
Since it is possible to have ParseTrees that are not recognized by this grammar, the pretty function has return type Option[String]. Calling pretty on ParseTrees that are not in this grammar return None:
Note that the pretty function makes use of Scala’s for-yield expressions, which you do not need to understand for this exercise.
Exercise
Exercise 13.6 (5 points) Give a parse tree for the sentence in the grammar a ? b : a.
Edit this cell:
val parseTree1: ParseTree =???
scala.NotImplementedError: an implementation is missing
scala.Predef$.$qmark$qmark$qmark(Predef.scala:345)
ammonite.$sess.cmd8$Helper.<init>(cmd8.sc:2)
ammonite.$sess.cmd8$.<clinit>(cmd8.sc:7)
???
Assertion
assert(pretty(parseTree1)==Some("a ? b : a"))passed(5)
cmd9.sc:1: not found: value parseTree1
val res9_0 = assert(pretty(parseTree1) == Some("a ? b : a"))
^Compilation Failed
:
Compilation Failed
Exercise
Exercise 13.7 (10 points) Show that this grammar (Equation 13.1) is ambiguous by giving two parse trees for the sentence in the grammar a ? b : a ? b : a.
Edit this cell:
val parseTree2: ParseTree =???val parseTree3: ParseTree =???
scala.NotImplementedError: an implementation is missing
scala.Predef$.$qmark$qmark$qmark(Predef.scala:345)
ammonite.$sess.cmd9$Helper.<init>(cmd9.sc:2)
ammonite.$sess.cmd9$.<clinit>(cmd9.sc:7)
???
pretty(parseTree2)pretty(parseTree3)
cmd10.sc:1: not found: value parseTree2
val res10_0 = pretty(parseTree2)
^cmd10.sc:2: not found: value parseTree3
val res10_1 = pretty(parseTree3)
^Compilation Failed
cmd10.sc:1: not found: value parseTree2
val res10_0 = assert(parseTree2 != parseTree3)
^cmd10.sc:1: not found: value parseTree3
val res10_0 = assert(parseTree2 != parseTree3)
^cmd10.sc:2: not found: value parseTree2
val res10_1 = assert(pretty(parseTree2).isDefined)
^cmd10.sc:3: not found: value parseTree3
val res10_2 = assert(pretty(parseTree3).isDefined)
^Compilation Failed
:
Compilation Failed
assert(pretty(parseTree2)==Some("a ? b : a ? b : a"))assert(pretty(parseTree3)==Some("a ? b : a ? b : a"))passed(5)
cmd10.sc:1: not found: value parseTree2
val res10_0 = assert(pretty(parseTree2) == Some("a ? b : a ? b : a"))
^cmd10.sc:2: not found: value parseTree3
val res10_1 = assert(pretty(parseTree3) == Some("a ? b : a ? b : a"))
^Compilation Failed
:
Compilation Failed
13.4 Defining Grammars
In this question, we define a BNF grammar for floating-point numbers that are made up of a fraction (e.g., 5.6 or 3.123 or -2.5) followed by an optional exponent (e.g., E10 or E-10).
More precisely for this exercise, our floating-point numbers
The exponent, if it exists, is the letter E followed by an integer. For example, the following are floating-point numbers: 0.0, 3.5E3, 3.123E30, -2.5E2, -2.5E-2, 3.50, and 3.01E2.
The following are examples of strings that are not floating-point numbers by our definition: 0, -0.0, 3.E3, E3, 3.0E4.5, and 4E4.
For this exercise, let us assume that the tokens are characters in the following alphabet \(\Sigma\):
Exercise 13.8 (10 points) Translate a grammar into a parser FloatParser.float:Parser[String] using the Scala Parsing Combinator Library that recognizes the language of floating-point numbers described above. Since we do not care about the correctness of the grammar, we let parse result simply be the input string.
We suggest that you first use the scratch cell below to give a grammar in BNF notation. Your grammar should be completely defined using the alphabet above as the terminals (i.e., it should not count on a non-terminal that it does not itself define).
The success[A](a: A):Parser[A] method corresponds to an \(\varepsilon\) production in BNF. It returns a parser that is successful without consuming any input yielding the parse result a. If you use it in your parser, make sure it is the last production for the non-terminal.
We provide some helper functions concat2:String~String=>String, concat3:String~String~String=>String, etc. that simply concatenate their input strings together. These helper functions are the only semantic actions you need.
We have provided some basic parsers sign, anyOneToNine, digit, zeroOrMoreDigits that you may use if you like.
You may edit the parse interface function that we have provided to start from a different non-terminal while you’re developing, but make sure it is float in the end and that your starting non-terminal is float. Alternatively, you may add additional such interface functions with a different name for your testing.
Tests
test(new AnyFlatSpec {val tests_positive =List("0.0","3.5E3","3.123E30","-2.5E2","-2.5E-2","3.50","3.01E2") behavior of "FloatParser"for(t <- tests_positive){ it should s"parse $t" in {assertResult(Some(t)){ FloatParser.parse(t)}}}val tests_negative =List("0","-0.0","3.E3","E3","3.0E4.5","4E4")for(t <- tests_negative){ it should s"fail to parse $t" in {assertResult(None){ FloatParser.parse(t)}}}},10)
Run starting. Expected test count is: 13
cmd11$Helper$$anon$1:
FloatParser
- should parse 0.0 *** FAILED ***
scala.NotImplementedError: an implementation is missing
at scala.Predef$.$qmark$qmark$qmark(Predef.scala:345)
at ammonite.$sess.cmd10$Helper$FloatParser$.<clinit>(cmd10.sc:11)
at ammonite.$sess.cmd11$Helper$$anon$1.$anonfun$new$2(cmd11.sc:17)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.flatspec.AnyFlatSpecLike$$anon$5.apply(AnyFlatSpecLike.scala:1832)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
...
ammonite.$sess.cmd11$Helper$$anon$1 *** ABORTED ***
java.lang.NoClassDefFoundError: Could not initialize class ammonite.$sess.cmd10$Helper$FloatParser$
at ammonite.$sess.cmd11$Helper$$anon$1.$anonfun$new$2(cmd11.sc:17)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.flatspec.AnyFlatSpecLike$$anon$5.apply(AnyFlatSpecLike.scala:1832)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.flatspec.AnyFlatSpec.withFixture(AnyFlatSpec.scala:1686)
...
*** RUN ABORTED ***
A needed class was not found. This could be due to an error in your runpath. Missing class: Could not initialize class ammonite.$sess.cmd10$Helper$FloatParser$
java.lang.NoClassDefFoundError: Could not initialize class ammonite.$sess.cmd10$Helper$FloatParser$
at ammonite.$sess.cmd11$Helper$$anon$1.$anonfun$new$2(cmd11.sc:17)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.flatspec.AnyFlatSpecLike$$anon$5.apply(AnyFlatSpecLike.scala:1832)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.flatspec.AnyFlatSpec.withFixture(AnyFlatSpec.scala:1686)
...