• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to attack the programming problem?

SU27

Junior Member
Hi, Guys,

I encountered a following exercise when I am study the concepts of programming languages.

What is the destructor methods mentioned in the program.
And from which engle we should write this program.

Please give me some hints about it. Or if you can help with part of the pseudocode, it will be better.

thanks.

It is as the following:


The purpose of this exercise is to provide experience with interpretation of expressions.
Consider the language of expressions, and the eval function for such language.
The problem consists in completing the definitions of the various classes (tree, node, and environment) with the necessary methods (and possibly additional fields, if needed), so to get a real C++ interpreter for the language of expressions. Please define also the destructor methods for all the dynamic user-defined structures.

the program will be tested by adding to it a main function like the following:

void main(){
node* n1 = new node("dec","x&quot😉;
node* n2 = new node("num",2);
node* n3 = new node("op","+&quot😉;
node* n4 = new node("ide","x&quot😉;
node* n5 = new node("num",5);
tree* t2 = new tree(n2);
tree* t4 = new tree(n4);
tree* t5 = new tree(n5);
tree* t3 = new tree(n3,t4,t5);
tree* t1 = new tree(n1,t2,t3);
environment *r = empty_environment();
cout << eval(t1,r);
delete t1;
delete r;
}

The call empty_environment() should return an empty environment, namely an environment containing no associations.
The tree t1 constructed in this main function corresponds to the expression

let x = 2 in x + 5 end

and the result given in output by the program should be, of course, 7.
Note: the program should work in general, not just in this case. The code will be tested also on other expressions, and in particular it will be tested on expressions containing nested blocks, like

let x = 2
in let x = 3
in x + 4
end
+ x
end

(result: 9)
let x = 2
in let y = 3
in x * y
end
+
let y = 1
in x - y
end
end

(result: 7).
 
Back
Top