Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C++ Tokenizer class. This Tokenizer class will consist of both a header

ID: 3740849 • Letter: C

Question

Create a C++ Tokenizer class. This Tokenizer class will consist of both a header and a class file. You will create the prog2_1.hpp and prog2_1.cpp

This Tokenizer class will have four public methods. First, the constructor, which will setup/create any internal data structures you need. Second, the destructor, which will delete any internal data structures you created during the lifetime of the object.

Third will be a void function that takes a single string argument name Tokenize. This Tokenize function will tokenize the input string on the space character. Only the following tokens are valid:

push, pop, add, sub, mul, div, mod, skip, save, get and any valid integer.

If an input string contains a potential token that is not one of those values the function should throw a std::exception with the message “Unexpected token: ” where is replaced by the unexpected token. (If the input was “foobar 3.14” only the first bad token ‘foobar’ should cause the exception, which should read “Unexpected token: foobar”.

Any valid input should then have all of those tokens saved inside the tokenizer in some sort of queue structure that will return them when requested by the GetTokens function.

Fourth will be a vector function that takes no arguments named GetTokens. This function will retrieve a single set of input tokens that had previously been passed to the function Tokenize in a queue fashion (the first input to Tokenize is the first output from GetTokens). If there are no remaining outputs the function should throw a std::exception with the message “No tokens”.

This Tokenizer class will be used by prog2_1

Explanation / Answer

//Header File for tokenizer #pragma once #include #include class Tokenizer { public: Tokenizer(); ~Tokenizer(); void Tokenize(std::string); std::vector GetTokens(); }; //Class file for tokenizer #include #include #include using namespace std; // Need to correct what method of custom excetion class class CustomException : public std::exception { private: string message; public: CustomException(const string& message_1) { message = message_1; } virtual const char* what() const throw() { return message.c_str(); } }; class Tokenizer { private: vector tokens; size_t index; public: Tokenizer() { } ~Tokenizer() { } void Tokenize(std::string parameter) { vector validTokens = {"push", "pop", "add", "sub", "mul", "div", "mod", "skip", "save", "get"}; stringstream check1(parameter); string splitTokens; while (getline(check1, splitTokens, ' ')) { bool exist = std::find(validTokens.begin(), validTokens.end(), splitTokens) != validTokens.end(); if (exist || isdigit(splitTokens[0])) { tokens.push_back(splitTokens); } else { throw CustomException(splitTokens); } } index = tokens.size(); } vector GetTokens() { if (index == 0) { throw std::exception("No Tokens"); } vector value; while (index > 0) { value.push_back(tokens[index - 1]); --index; } return value; } }; //Include this main method in prog2_1.cpp class int main() { Tokenizer t; try { t.Tokenize("Swati Jain"); } catch (std::exception e) { cout