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) {
main.cpp #include
#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;
}
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