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"😉;
node* n2 = new node("num",2);
node* n3 = new node("op","+"😉;
node* n4 = new node("ide","x"😉;
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).
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"😉;
node* n2 = new node("num",2);
node* n3 = new node("op","+"😉;
node* n4 = new node("ide","x"😉;
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).