Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(I need help with problem 2. It's in Scalar/Scala, and I can't pass all the test cases. It would be greatly appreciated if you could
(I need help with problem 2. It's in Scalar/Scala, and I can't pass all the test cases. It would be greatly appreciated if you could show me how to do this problem and explain it.)
[ ]:
sealed trait Expr
type Tag = String
type Identifier = String
case class Const(d: Double) extends Expr
case class Ident(id:Identifier, tag: Tag) extends Expr
case class Plus(e1: Expr, e2: Expr) extends Expr
case class Mult(e1: Expr, e2: Expr) extends Expr
case class Let(id: Identifier, tag: Tag, e1: Expr, e2: Expr) extends Expr
// Some helpful functions to write test cases -- you can ignore these.
def x(tag:String):Expr = Ident("x", tag)
def y(tag:String):Expr = Ident("y", tag)
def z(tag:String):Expr = Ident("z", tag)
def w(tag:String):Expr = Ident("w", tag)
[Fill in the ??? ]:
def getAllUseDefLinks(e: Expr, env: Map[Identifier, Tag]): Map[Tag, Tag] =
e match {
case Const(_) => { Map.empty }
case Ident(id, usetag) => {
// YOUR CODE HERE
???
}
case Plus(e1, e2) => {
// YOUR CODE HERE
???
}
case Mult(e1, e2) => {
// YOUR CODE HERE
???
}
case Let(x, tag, e1, e2) => {
// YOUR CODE HERE
???
}
}
[ test cases ]:
/*
let y@tag2 = x @use0 in
let x @tag1 = 10 in
x@use1 + x@use2
*/
val e = Let("y", "tag2", x("use0"), Let ("x", "tag1", Const(10), Plus(x("use1"), x("use2"))))
val map = getAllUseDefLinks(e, Map.empty)
assert (map("use0") == "UNDEFINED", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use0")}")
assert (map("use1") == "tag1", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use1")}")
assert (map("use2") == "tag1", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use2")}")
passed(5)
[ test cases ]:
/*
let x @tag3 = x @use0 + 10 in
let y@tag1 = 10 in
x@use1 * y @use2 */
val e = Let("x", "tag3", Plus(x("use0"), Const(10.0)),
Let ("y", "tag1", Const(10), Mult(x("use1"), y("use2"))))
val map = getAllUseDefLinks(e, Map.empty)
assert (map("use0") == "UNDEFINED", s"Test1 failed: use0 must be associated with UNDEFINED. Your code returns ${map("use0")}")
assert (map("use1") == "tag3", s"Test1 failed: use1 must be associated with UNDEFINED. Your code returns ${map("use1")}")
assert (map("use2") == "tag1", s"Test1 failed: use2 must be associated with tag1. Your code returns ${map("use2")}")
passed(5)Problem 2: Tag Usages of Identifiers with Definitions In this problem, we will extend Lettuce by adding user-define generated (https//en.wikipedia.org/wiki/Use-define_chain). Example 1 In this example, we add "comment tagg" to each let binding (tagi, tag2, and tag3) and to each usage ( use1, use2, use3). The togs play no role in the program execution other than giving names to refer to the appropriate definitions and usages of identifiers. The purpose of this analysis is to produce a Map that maps usage tags to the let binding tags. This denotes that the usage tagged "use1" corresponds to the let-binding "tag3", "use2" usage corresponds to let binding "tog1" and "use3" usage corresponds to let binding "tag2". Abstract Syntax - First compute the use-def mappings for the RHS expression e1. - Nirst compute update the enwironment to note that identifier 18 is associated with tag. - Now, compute the mappings for in" exp - Conjoin the two maps for e1 and e2. Here are two nules for a identifiers. getAllseDefLinks(Ident(1d,usetag),env)={usetag"UNDENINED"}1d/domsin(env)(ident-undefined) Note that we reserve a special tag name UADEFINED (i.e, the string UNDEFINED") to denote identifiers that are not defined in the current scope - If the identifier Id does not belong to the domain of the environment, then retum a singleton mapping from the use-tag usetag associated with this use of id to the special tag unoEFINED: Part (A) : Complete the rule for Plus ( 5 points). Write down what should go in for ?1 and ?2 below. - If there are no antecedents just write "blank". - The empty map is represented as \{\} . - Concatenation of two maps is nap1 and map 2 is denoted map1 + map2: the scala operator for this is ++ though. YOUR ANSWER HERE Complete the rule for Const Write down what should go in for ?1 and 2 below. - If there are no antecedents just write "blank". - The empty map is represented as \{\} . - Concatenation of two maps is map1 and map2 is denoted nap1 + map2 . YOUR ANSWER HERE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started