QuestionQuestion

n this assignment we’re going to extend the arithmetic expression tree presented in class to handle function calls, with a single parameter.

The grammar presented in class was simply:

exp -> NUM
exp -> VAR
exp -> '(' exp ')'
exp -> '-' exp
exp -> exp OP exp

To extend this to functions, we add an additional rule:

exp -> FNAME '(' exp ')'
The rules for function names are similar to those for variables, except that underscore _ characters are allowed in function names but not in variable names.

You should implement the functions

vector<string> tokenize(string s);

exp* parse(vector<string>::iterator start, vector<string>::iterator finish, int& prec);

Note that unlike in class, the finish parameter here represents the position “one-past-the-end” of the vector. That is, finish-1 is the last token in the input. Use the prec parameter to ensure that infix operators are parsed with the correct precedence.

Use an expression tree type with the following classes:

struct exp {
    // You will have to implement the print method on all the subclasses.
    virtual void print() = 0;
};

// Precedence 100
struct exp_num : public exp {
    int value;

    exp_num(int v) : value(v) { }

    void print();
};

// Precedence 100
struct exp_var : public exp {
    std::string name;

    exp_var(std::string n) : name(n) { }

    void print();
};

// Precdence 100
struct exp_paren : public exp {
    exp* inner;

    exp_paren(exp* i) : inner(i) { }

    void print();
};

// Precedence 100
struct exp_minus : public exp {
    exp* inner;

    exp_minus(exp* i) : inner(i) { }

    void print();
};

// Precedence 100
struct exp_func : public exp {
    std::string name;
    exp* arg;

    exp_func(std::string n, exp* a) : name(n), arg(a) { }

    void print();
};

// Precedence:
// + 50
// - 50
// * 75
// / 75
struct exp_op : public exp {
    char op;
    exp* left;
    exp* right;

    exp_op(char o, exp* l, exp* r) : op(o), left(l), right(r) { }

    void print();
};

You will have to implement the print method on all subclasses.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <locale>
#include "Solution.hpp"

void exp_num::print() {
    std::cout << value;
}

void exp_var::print() {
    std::cout << name;
}

void exp_paren::print() {
    if (inner != nullptr) {
       inner->print();
    }
    else {
       std::cout << "NULLPTR" << std::endl;
    }
}

void exp_minus::print() {
    inner->print();
}

void exp_func::print() {
    std::cout << name << "(";
    arg->print();
    std::cout << ")";
}

void exp_op::print() {
    left->print();
    std::cout << op;
    right->print();
}

bool isDigit(char c) {
    return ((c >= '0') && (c <= '9'));
}

bool isAlpha(char c) {
    return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));...
$45.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats