Question: I need help in parse additional types of expressions defined by the expanded grammar shown below with the additions to the grammar highlighted in BOLD

I need help in parse additional types of expressions defined by the expanded grammar shown below with the additions to the grammar highlighted 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

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

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 == ','); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!