Question
Computer Science //This is a cpp header file and a .cpp file to be implemented by me. Can you help correct my mistakes and help
Computer Science
//This is a cpp header file and a .cpp file to be implemented by me. Can you help correct my mistakes and help explain what I am doing wrong in those areas.
_______________________________________
This is the header file completed and provided by the instructor
_______________________________________
#pragma once
#include
using std::string; using std::vector; using std::unordered_map;
typedef unordered_map
//abstract base class class Stm { public: //interprets the statement using the given symboltable virtual void interp( SymbolTable& ) = 0;
};
//abstract base class class Exp { public: //interprets the expression using the given symboltable virtual int interp( SymbolTable& ) = 0;
};
//abstract base class class ExpList { public: //interprets the expression using the given symboltable virtual void interp( SymbolTable& ) = 0;
};
class CompoundStm: public Stm { private: Stm *stm1, *stm2; public: CompoundStm(Stm *s1, Stm *s2) : stm1(s1), stm2(s2) {/*empty body*/}
~CompoundStm() {delete stm1; delete stm2;}
/* To be implemented by stduents */ void interp( SymbolTable& ); };
class AssignStm: public Stm { private: string id; Exp *exp; public: AssignStm(string i, Exp *e) : id(i), exp(e) {/*empty body*/}
~AssignStm() {delete exp;}
/* To be implemented by stduents */ void interp( SymbolTable& ); };
class PrintStm: public Stm { private: ExpList *exps; public: PrintStm(ExpList *e) : exps(e) {/*empty body*/}
~PrintStm() { delete exps; }
/* To be implemented by stduents */ void interp( SymbolTable& ); };
class IdExp: public Exp { private: string id; public: IdExp(string i) :id(i) { /*empty body*/ } ~IdExp() {}
/* To be implemented by stduents */ int interp( SymbolTable& ); };
class NumExp : public Exp { private: int num; public: NumExp(int n) : num(n) { /*empty body*/ }
~NumExp() {}
/* To be implemented by stduents */ int interp( SymbolTable& ); };
class OpExp : public Exp { public: enum OperType {PLUS, MINUS, TIMES, DIV}; private: Exp *left, *right; OperType oper; public: OpExp(Exp *l, OperType o, Exp *r) : left(l), oper(o), right(r) { /*empty body*/ }
~OpExp() { delete left; delete right;}
/* To be implemented by stduents */ int interp( SymbolTable& ); };
class EseqExp : public Exp { private: Stm *stm; Exp *exp; public: EseqExp(Stm *s, Exp *e) : stm(s), exp(e) { /*empty body*/ }
~EseqExp() { delete stm; delete exp; }
/* To be implemented by stduents */ int interp( SymbolTable& ); };
class PairExpList : public ExpList { private: Exp *head; ExpList *tail; public: PairExpList(Exp *h, ExpList *t) : head(h), tail(t) { /* empty body */ }
~PairExpList() {delete head; delete tail;}; const Exp* getHead( void ) const {return head;} const ExpList* getTail( void ) const {return tail;}
/* To be implemented by stduents */ void interp( SymbolTable& ); //int size() const; };
class LastExpList : public ExpList { private: Exp *head; public: LastExpList(Exp *h) : head(h) { /*empty body */ }
~LastExpList() {delete head; }; const Exp* getHead( void ) const {return head;}
/* To be implemented by stduents */ void interp( SymbolTable& ); //int size() const; }; _____________________________________________
//This is the .cpp file provided below. Can you please help correct my errors and provide some explanation to what I am doing wrong.
_____________________________________________
#include
using namespace std;
/* To be implemented by stduents */ void CompoundStm::interp( SymbolTable& symbols ) { stm1->interp(symbols); stm2->interp(symbols); }
/* To be implemented by stduents */ void AssignStm::interp( SymbolTable& symbols ) { //eval the expr and use result to update the value of the var // 1. the var does not exist in the symbol table // 2. the var is already stored in the symbol table SymbolTable[id]=exp->interp(symbols); }
void PrintStm::interp( SymbolTable& symbols ) //PairExpList & LastExp only used here {
//RETURN to this it is incomplete exps->interp(symbols); }
int IdExp::interp( SymbolTable& symbols ) { //get value of id and return it return SymbolTable[id]; //to avoid compile errors }
int NumExp::interp( SymbolTable& symbols ) { //get value of int const and return it return num; //to avoid compile errors }
int OpExp::interp( SymbolTable& symbols ) { //eval left expr and save to lr //eval right expr and save to rr //depending on type of oper, calc lr and rr //need a switch statement left->interp(symbols); right->interp(symbols); SymbolTable[oper] = "0"; switch (oper) { case 0: return left + right; case 1: return left - right; case 2: return left * right; case 3: return left / right; default: return "No operands stored"; }
return 0; //to avoid compile errors }
int EseqExp::interp( SymbolTable& symbols ) { //evaluate these two stm->interp(symbols); exp->interp(symbols); //return the result return 0; //to avoid compile errors }
void PairExpList::interp( SymbolTable& symbols) { //evaluate the expressions in the list head->interp(symbols); tail->interp(symbols); }
void LastExpList::interp( SymbolTable& symbols) { head->interp(symbols);
}
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