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

I need to set up a header file that has the following Class, Constructors .. .et

ID: 3848991 • Letter: I

Question

I need to set up a header file that has the following Class, Constructors .. .etc. Below it is the Test Code must work with.

The class TokenParser should be declared in TokenParser.h and contain the following methods:

// Default constructor
TokenParser()
Default constructor for this class; creates a parser with no initial single-character tokens.

TokenParser(const vector<char>& tokens)
Constructor for this class; creates a parser with the provided single-character tokens defined.

void setCharacterTokens(const vector<char>& tokens) Resets this parser’s single-character tokens to the provided list of tokens.

void addCharacterTokens(const vector<char>& newTokens)
Adds the provided single-character tokens to the set of tokens this parser recognizes.

void addCharacterToken(char newToken)
Adds the provided single-character token to the set of tokens this parser recognizes.

vector<string> parse(string input) const Parses the string and returns a vector of tokens in string form.

You may, at our option, add any private class methods that help you accomplish this goal. DO NOT add any additional public methods or variables, as this violates encapsulation safety.

It is suggested that students consider using a following private helper method similar to the following:

vector<string> tokenize(string snippet)
dBreaks a whitespace-free snippet into tokens by finding and splitting at single-character tokens.

#include <iostream>
#include <vector>
#include "TokenParser.h"

void printTokens(const vector<string>& tokens)
{
    cout << "[ ";
    for (auto entry : tokens)
    {
        cout << entry << " ";
    }
    cout << "] ";
}
int main()
{
    const char blocks[] = ""{}()";
    const char punctuation[] = ".!?'";
    const char symbols[] = "$#-";

    vector<char> blockTokens(begin(blocks), end(blocks));
    vector<char> punctuationTokens(begin(punctuation), end(punctuation));

    string input1 = "{Oh what a loon() I am! Don't you agree?}";
    string input2 = "I'll sing "Amazing Grace" for $5. Please call1 the #867-5309.";

    TokenParser* parser = new TokenParser();

    cout << "Input strings "
         << "------------- "
         << "[" << input1 << "] "
         << "[" << input2 << "] "
         << " ";

    cout << " "
         << "Whitespace only: "
         << "--------------- ";

    printTokens(parser->parse(input1));
    printTokens(parser->parse(input2));

    parser->setCharacterTokens(blockTokens);

    cout << " "
         << "Block markers: "
         << "-------------- ";

    printTokens(parser->parse(input1));
    printTokens(parser->parse(input2));

    parser->addCharacterTokens(punctuationTokens);

    cout << " "
         << "Punctuation added: "
         << "------------------ ";

    printTokens(parser->parse(input1));
    printTokens(parser->parse(input2));

    for (char symbol : symbols)
    {
        parser->addCharacterToken(symbol);
    }

    cout << " "
         << "Symbols added: "
         << "-------------- ";
    printTokens(parser->parse(input1));
    printTokens(parser->parse(input2));

    delete parser;
    return 0;
}

Explanation / Answer

Complete Program:

File: TokenParser.h

// Header files section
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

// TokenParser class specification
class TokenParser
{
public:
   TokenParser();
   TokenParser(const vector<char>& tokens);
   void setCharacterTokens(const vector<char>& tokens);
   void addCharacterTokens(const vector<char>& newTokens);
   void addCharacterToken(char newToken);
   vector<string> parse(string input);

private:
   vector<string> tokenize(string snippet);
   vector<char> delimiters;
};

// TokenParser class implementation

// default constructor creates a parser with no initial single-character tokens
TokenParser::TokenParser()
{}

// args-constructor creates a parser with the provided single-character tokens defined
TokenParser::TokenParser(const vector<char>& tokens)
{
   delimiters = tokens;
}

// setCharacterTokens function resets this parser’s single-character tokens to the provided list of tokens
void TokenParser::setCharacterTokens(const vector<char>& tokens)
{
   delimiters = tokens;
}

// addCharacterTokens function adds the provided single-character tokens to the set of tokens this parser recognizes
void TokenParser::addCharacterTokens(const vector<char>& newTokens)
{
   delimiters.insert(delimiters.end(), newTokens.begin(), newTokens.end());
}

// addCharacterToken function adds the provided single-character token to the set of tokens this parser recognizes
void TokenParser::addCharacterToken(char newToken)
{
   delimiters.push_back(newToken);
}

// parse function parses the string and returns a vector of tokens in string form
vector<string> TokenParser::parse(string input)
{
   string result = "";
   int doubleQuotes = 0;  

   for (unsigned int i = 0; i < input.length(); i++)
   {
       char ch = input[i];

       if (ch == ' ' || ch == ' ' || ch == ' ')
       {
           result += ' ';
       }      
       else if (std::find(delimiters.begin(), delimiters.end(), ch) != delimiters.end())
       {
           if (ch == '"')
           {
               doubleQuotes++;

               if (doubleQuotes % 2 == 1)
               {
                   result += ch;
                   result += ' ';
               }
               else
               {
                   result += ' ';
                   result += ch;
               }
           }
           else
           {
               result += ' ';
               result += ch;
               result += ' ';
           }
       }
       else
       {
           result += ch;
       }
   }
  
   vector<string> perser = tokenize(result);

   return perser;
}

// tokenize function breaks a whitespace-free snippet into tokens by finding and splitting at single-character tokens
vector<string> TokenParser::tokenize(string snippet)
{
   vector<string> perser;

   int pos = snippet.find(' ');
   while(pos != -1)
   {      
       string str = snippet.substr(0, pos);
       snippet = snippet.substr(pos+1);

       perser.push_back(str);

       pos = snippet.find(' ');
   }

   perser.push_back(snippet);

   return perser;
}

File: main.cpp

#include <iostream>
#include <vector>
#include "TokenParser.h"

void printTokens(const vector<string>& tokens)
{
   cout << "[ ";
   for (auto entry : tokens)
   {
       cout << entry << " ";
   }
   cout << "] ";
}

int main()
{
   const char blocks[] = ""{}()";
   const char punctuation[] = ".!?'";
   const char symbols[] = "$#-";

   vector<char> blockTokens(begin(blocks), end(blocks));
   vector<char> punctuationTokens(begin(punctuation), end(punctuation));

   string input1 = "{Oh what a loon() I am! Don't you agree?}";
   string input2 = "I'll sing "Amazing Grace" for $5. Please call1 the #867-5309.";

   TokenParser* parser = new TokenParser();

   cout << "Input strings "
       << "------------- "
       << "[" << input1 << "] "
       << "[" << input2 << "] "
       << " ";

   cout << " "
       << "Whitespace only: "
       << "--------------- ";

   printTokens(parser->parse(input1));
   printTokens(parser->parse(input2));
  
   parser->setCharacterTokens(blockTokens);

   cout << " "
       << "Block markers: "
       << "-------------- ";

   printTokens(parser->parse(input1));
   printTokens(parser->parse(input2));

   parser->addCharacterTokens(punctuationTokens);

   cout << " "
       << "Punctuation added: "
       << "------------------ ";

   printTokens(parser->parse(input1));
   printTokens(parser->parse(input2));

   for (char symbol : symbols)
   {
       parser->addCharacterToken(symbol);
   }

   cout << " "
       << "Symbols added: "
       << "-------------- ";
   printTokens(parser->parse(input1));
   printTokens(parser->parse(input2));
  
   delete parser;

    return 0;
}

Sample Output:

Input strings
-------------
[{Oh what a loon() I am!        Don't you agree?}]
[I'll sing "Amazing Grace" for $5.
Please call1 the #867-5309.]


Whitespace only:
---------------
[ {Oh what a loon() I am! Don't you agree?} ]
[ I'll sing "Amazing Grace" for $5. Please call1 the #867-5309. ]

Block markers:
--------------
[ { Oh what a loon ( ) I am! Don't you agree? } ]
[ I'll sing " Amazing Grace " for $5. Please call1 the #867-5309. ]

Punctuation added:
------------------
[ { Oh what a loon ( ) I am ! Don ' t you agree ? } ]
[ I ' ll sing " Amazing Grace " for $5 . Please call1 the #867-5309 . ]

Symbols added:
--------------
[ { Oh what a loon ( ) I am ! Don ' t you agree ? } ]
[ I ' ll sing " Amazing Grace " for $ 5 . Please call1 the # 867 - 5309 . ]