Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The single ternary operator ? is the conditional expression operator. Unlike the conditional expression operator in C++ and Java, no colon separates the second and

image text in transcribed

image text in transcribed

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.

C++ Code:

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

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

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

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 > paren; cout > comma; parseAssignments(in); double result = expression->evaluate(); cout

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

symboltable.h

// The body of its functions are defined in symboltable.cpp. 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

// a subclass of Expression, it must implement the function evaluate, whose body is defined in variable.cpp.

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

Please modify the code below to meet the requirements. This is different from the many similar questions that have been asked. ***If you copy/paste from the other answers without reading the requirements, I will downvote the answer. 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][aZAZ09] 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 in bold: statement expression ', assignments ';' expression(expressions) expressions unary_expression | binary_expression | ternary_expression | quaternary_ex| ression 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 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

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_2

Step: 3

blur-text-image_3

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions