Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a project that evaluates statements of an expression language that accepts input from a file named input.txt, which contains one statement per line.

I have a project that evaluates statements of an expression language that accepts input from a file named input.txt, which contains one statement per line. Here is the grammar of a single input file:

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

I need to make adjustments to the following specifications:

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.

Here is the Literal class: class Literal: public Operand { public: Literal(double value) { this->value = value; } double evaluate() { return value; } private: double value; };

Here is the Variable class:

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

Here is the main project module, of which parseAssignments I believe to be of interest:

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

Please provide suggestions for changes, in code if possible.

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

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions