Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the Skeleton: expression.h class Expression { public: virtual double evaluate() = 0; }; literal.h class Literal: public Operand { public: Literal(double value) {

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Here is the Skeleton:

expression.h

class Expression {

public:

virtual double evaluate() = 0;

};

literal.h

class Literal: public Operand {

public:

Literal(double value) {

this->value = value;

}

double evaluate() {

return value;

}

private:

double value;

};

minus.h

class Minus: public SubExpression {

public:

Minus(Expression* left, Expression* right): SubExpression(left, right) {

}

double evaluate() {

return left->evaluate() - right->evaluate();

}

};

operand.cpp

#include

#include

#include

#include

#include

using namespace std;

#include "expression.h"

#include "subexpression.h"

#include "operand.h"

#include "variable.h"

#include "literal.h"

#include "parse.h"

Expression* Operand::parse(stringstream& in) {

char paren;

int value;

in >> ws;

if (isdigit(in.peek())) {

in >> value;

Expression* literal = new Literal(value);

return literal;

}

if (in.peek() == '(') {

in >> paren;

return SubExpression::parse(in);

}

else

return new Variable(parseName(in));

return 0;

}

operand.h

class Operand: public Expression {

public:

static Expression* parse(stringstream& in);

};

parse.cpp

#include

#include

#include

using namespace std;

#include "parse.h"

string parseName(stringstream& in) {

char alnum;

string name = "";

in >> ws;

while (isalnum(in.peek())) {

in >> alnum;

name += alnum;

}

return name;

}

parse.h

string parseName(stringstream& in);

plus.h

class Plus: public SubExpression {

public:

Plus(Expression* left, Expression* right): SubExpression(left, right) {

}

double evaluate() {

return left->evaluate() + right->evaluate();

}

};

project2.cpp

#include

#include

#include

#include

#include

using namespace std;

#include "expression.h"

#include "subexpression.h"

#include "symboltable.h"

#include "parse.h"

SymbolTable symbolTable;

void parseAssignments(stringstream& in);

int main() {

const int SIZE = 256;

Expression* expression;

char paren, comma, line[SIZE];

ifstream fin;

fin = ifstream("input.txt");

if (!(fin.is_open())) {

cout

system("pause");

return 1;

}

while (true) {

fin.getline(line, SIZE);

if (!fin)

break;

stringstream in(line, ios_base::in);

in >> paren;

cout

try {

expression = SubExpression::parse(in);

in >> comma;

parseAssignments(in);

double result = expression->evaluate();

cout

}

catch (string message) {

cout

}

}

system("pause");

return 0;

}

void parseAssignments(stringstream& in) {

char assignop, delimiter;

string variable;

int value;

do {

variable = parseName(in);

in >> ws >> assignop >> value >> delimiter;

symbolTable.insert(variable, value);

}

while (delimiter == ',');

}

subexpression.cpp

#include

#include

using namespace std;

#include "expression.h"

#include "subexpression.h"

#include "operand.h"

#include "plus.h"

#include "minus.h"

SubExpression::SubExpression(Expression* left, Expression* right) {

this->left = left;

this->right = right;

}

Expression* SubExpression::parse(stringstream& in) {

Expression* left;

Expression* right;

char operation, paren;

left = Operand::parse(in);

in >> operation;

right = Operand::parse(in);

in >> paren;

switch (operation) {

case '+':

return new Plus(left, right);

case '-':

return new Minus(left, right);

}

return 0;

}

subexpression.h

class SubExpression: public Expression {

public:

SubExpression(Expression* left, Expression* right);

static Expression* parse(stringstream& in);

protected:

Expression* left;

Expression* right;

};

symboltable.cpp

#include

#include

using namespace std;

#include "symboltable.h"

void SymbolTable::insert(string variable, double value) {

const Symbol& symbol = Symbol(variable, value);

elements.push_back(symbol);

}

double SymbolTable::lookUp(string variable) const {

for (int i = 0; i

if (elements[i].variable == variable)

return elements[i].value;

return -1;

}

symboltable.h

class SymbolTable {

public:

SymbolTable() {}

void insert(string variable, double value);

double lookUp(string variable) const;

private:

struct Symbol {

Symbol(string variable, double value) {

this->variable = variable;

this->value = value;

}

string variable;

double value;

};

vector elements;

};

variable.cpp

#include

#include

using namespace std;

#include "expression.h"

#include "operand.h"

#include "variable.h"

#include "symboltable.h"

extern SymbolTable symbolTable;

double Variable::evaluate() {

return symbolTable.lookUp(name);

}

variable.h

class Variable: public Operand {

public:

Variable(string name) {

this->name = name;

}

double evaluate();

private:

string name;

};

Help create the unary_expression, binary_expression, ternary_expression, and quaternary_expression, based on the skeleton code. Thanks.

The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study in the week 5 module reading. The skeleton code for this project is attached. It differs slightly from the what is provided in the case study. The code in the case study allows only one expression to be input from the keyboard whereas the code in the attached skeleton accepts input from a file named input. txt. That file contains one statement per line. The statements of that expression language consist of an arithmetic expression followed by a list of assignments. Assignments are separated from the expression and each other by commas. A semicolon terminates the expression. The arithmetic expressions are fully parenthesized infix expressions containing integer literals and variables. The syntax of a single input file line is described grammar below: statement expression ',' assignments ';' expression '(' operand operator operand ')' operator '+' ' ' operand literal | variable | expression assignments assignments ',' assignment | assignment assignment variable = literal In the above grammar, terminal symbols are upper case names or character literals shown in blue and nonterminal symbols are lower case names shown in red. EBNF metacharacters are shown in black. Tokens can be separated by any number of spaces. Tokens can be separated by any number of spaces. Variable names begin with an alphabetic character, followed by any number of alphanumeric characters. Variable names are case sensitive. The regular expressions defining the variables and literal tokens are the following: variable [azAZ][azAZO9] * literal [09]+ The program reads in the arithmetic expression and encodes the expression as a binary tree. After the expression has been read in, the variable assignments are read in and the variables and their values of the variables are placed into the symbol table. Finally the expression is evaluated recursively. Your first task is to modify the program so that it will parse additional types of expressions defined by the expanded grammar shown below with the additions to the grammar highlighted in yellow: The semantics of the additional binary arithmetic operators are as follows: * Multiplication / Division Remainder ^ Exponentiation Although two of the three additional binary operators are customarily relational operators in most languages, that is not true in this language. The semantics of all three of those operators are as follows: Maximum (Evaluates to the maximum of the left and right operand) \& Average (Evaluates to the average of the left and right operand) The single unary operator is the negation operator. Unlike the unary minus in most languages, it is a postfix operator rather than a prefix one. The single ternary operator ? is the conditional expression operator. Unlike the conditional expression operator in C++ and Java, no colon separates the second and third operands. This expression is evaluated as follows. If the expression to the left of the operator ? is not 0 , the value of the expression is the value of the first expression after the operator ?. If it is 0 , the value of the expression is the value of the second expression after the operator ?. The single quaternary operator \# is a variation of the typical conditional expression operator. Like the ternary conditional expression operator, the remaining three operands are delimited only by whitespace. This expression is evaluated as follows. If the expression to the left of the operator \# is less than 0 , the value of the expression is the value of the first expression after the operator \#. If it is equal to 0 , the value of the expression is the value of the second expression after the operator \#. If it is greater than 0 , the value of the expression is the value of the third expression after the operator \#. The second task is to modifier the variable token so that underscores are permitted in all but the first character and modify the literal token so that it accepts unsigned floating point literals. Assignments also should be modified to allow assignment to values that are should also floating point rather than just integers. The final task is to make the following modifications: - The symbol table should be initialized before each statement is evaluated, so that variables that are reused do not contain the value from a previous statement - Statements containing uninitialized variables should be reported as an error - A variable initialized more than once in a statement should be reported as an error (Creating an exception class to accommodate this error and the previous one is the recommended approach) You may assume that all input is syntactically correct. No checks for syntax errors is required. Each new class must be in a separate . h and .cpp pair of files. If all the functions in a class are one line functions, they can be implemented inline in .h file and the .cpp file can be omitted

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

10. Why do questions matter?

Answered: 1 week ago