Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Current C++ code: parse.h // This file contains the function prototype of the parseName function whose body is defined in parse.cpp. string parseName(stringstream& in); parse.cpp

Current C++ code:

parse.h // This file contains the function prototype of the parseName function whose body is defined in parse.cpp.

string parseName(stringstream& in);

parse.cpp // characters until the next whitespace and returns the name that those characters form.

#include #include #include using namespace std; #include "parse.h"

string parseName(stringstream& in) {

main.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 << "File did not open" << endl; system("pause"); return 1; } while (true) { fin.getline(line, SIZE); if (!fin) break; stringstream in(line, ios_base::in); in >> paren; cout << line << " "; try { expression = SubExpression::parse(in); in >> comma; parseAssignments(in); double result = expression->evaluate(); cout << "Value = " << result << endl; } catch (string message) { cout << message << endl; } } 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 == ','); }

char alnum;

string name = "";

in >> ws;

while (isalnum(in.peek()))

{ in >> alnum; name += alnum;

}

return name; }

operand.h

class Operand: public Expression {

public: static Expression* parse(stringstream& in);

};

operand.cpp

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;

}

subexpression.h

class SubExpression: public Expression {

public: SubExpression(Expression* left, Expression* right);

static Expression* parse(stringstream& in);

protected: Expression* left; Expression* right;

};

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; }

plus.h

class Plus: public SubExpression {

public: Plus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() + right->evaluate(); }

};

variable.h

class Variable: public Operand {

public: Variable(string name) { this->name = name; } double evaluate(); private: string name;

};

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);

}

minus.h

class Minus: public SubExpression {

public: Minus(Expression* left, Expression* right): SubExpression(left, right) { } double evaluate() { return left->evaluate() - right->evaluate(); }

};

literal.h

class Literal: public Operand {

public: Literal(double value) { this->value = value; } double evaluate() { return value; } private: double value; }; expression.h class Expression { public: virtual double evaluate() = 0;

};

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;

};

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 < elements.size(); i++) if (elements[i].variable == variable) return elements[i].value; return -1;

}

I need the following:

1. 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 in Bold:

statement expression ',' assignments ';'

expression '(' expressions ')'

expressions unary_expression | binary_expression | ternary_expression | quaternary_expression

unary_expression expression '~'

binary_expression expression binary_operator expression

binary_operator '+' | '-' | '*' | '/' | '%' | '^' | '<' | '>' | '_'

ternary_expression expression '?' expression expression

quaternary_expression expression '#' expression expression expression

operand literal | variable | expression

assignments assignments ',' assignment | assignment

assignment variable '=' literal

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.

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: < Minimum (Evaluates to the minimum of the left and right operand) > 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 #.

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

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions