All code must be written using C++ programming language.
You will construct a class that defines a parser. This parser will be able to break up a string into tokens. A token is a distinct string of text – it can be a word, a symbol, or a combination of these. Your parser will eliminate whitespace and break string snippets into individual tokens (where single-character tokens are defined at runtime).
Your parser should behave as follows:
1) Whitespace (spaces, tabs, and new lines) is used only to separate tokens – otherwise, it is ignored.
2) Most tokens are made up of strings of text separated punctuation and other symbols.
3) In addition, single-character tokens (punctuation and/or other symbols) may be defined.
The parser should treat symbols like any other text character. The default behavior will that all characters not separated by whitespace will be part of the same token. However, the parser will provide several functions that will allow the user to define additional, single-character symbols as token as well.
Target 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.]
Output Delimiting/Parsing by Whitespace only:
—————
[ {Oh what a loon() I am! Don’t you agree?} ]
[ I’ll sing “Amazing Grace” for $5. Please call1 the #867-5309. ]
Output Delimiting/Parsing by Block markers:
————–
[ { Oh what a loon ( ) I am! Don’t you agree? } ]
[ I’ll sing ” Amazing Grace ” for $5. Please call1 the #867-5309. ]
Output Delimiting/Parsing by Punctuation added:
——————
[ { Oh what a loon ( ) I am ! Don ‘ t you agree ? } ]
[ I ‘ ll sing ” Amazing Grace ” for $5 . Please call1 the #867-5309 . ]
Output Delimiting/Parsing by Symbols added:
————–
[ { Oh what a loon ( ) I am ! Don ‘ t you agree ? } ]
[ I ‘ ll sing ” Amazing Grace ” for $ 5 . Please call1 the # 867 – 5309 . ]
Expert Answer
//TokenParser.h
//Include the needed header files
#ifndef TokenParserClass_h
#define TokenParserClass_h
#include <iostream>
#include <vector>
using namespace std;
//Class
class TokenParser
{
//Access specifier
private:
//Declare
vector<char> listOfTokens;
//Method declaration tokenize()
vector<string> tokenize(string snippet) const;
//Access specifier
public:
//Default constructor
TokenParser();
//Constructor
TokenParser(const vector<char>& tokens);
//Method declaration setCharacterTokens()
void setCharacterTokens(const vector<char>& tokens);
//Method declaration addCharacterTokens()
void addCharacterTokens(const vector<char>& newTokens);
//Method declaration addCharacterToken()
void addCharacterToken(char newToken);
//Method declaration parse()
vector<string> parse(string input) const;
//Method declaration show()
void show();
};
#endif
//TokenParser.cpp
//Include the needed header files
#include “TokenParser.h”
using namespace std;
//Method definition tokenize()
vector<string> TokenParser::tokenize(string snippet) const
{
//Declare
vector<string> tokens;
//Loop
for(int index = 0; index < snippet.size(); index++)
{
//Push
tokens.push_back(string(1,snippet[index]));
}
//Return
return tokens;
}
//Constructor definition
TokenParser::TokenParser(){}
//Constructor definition
TokenParser::TokenParser(const vector<char>& tokens)
{
//Initialize
listOfTokens = tokens;
}
//Method definition setCharacterTokens()
void TokenParser::setCharacterTokens(const vector<char>& tokens)
{
//Set
listOfTokens = tokens;
}
//Method definition addCharacterTokens()
void TokenParser::addCharacterTokens(const vector<char>& newTokens)
{
//Insert
listOfTokens.insert(listOfTokens.end(), newTokens.cbegin(), newTokens.cend());
}
//Method definition addCharacterToken()
void TokenParser::addCharacterToken(char newToken)
{
//Insert
listOfTokens.push_back(newToken);
}
//Method definition parse()
vector<string> TokenParser::parse(string input) const
{
//Return
return tokenize(input);
}
//Method definition show()
void TokenParser::show()
{
//Loop
for(vector<char>::iterator iter = listOfTokens.begin(); iter != listOfTokens.end(); ++iter)
{
//Display
cout << *iter << ” “;
}
}
//Driver.cpp
//Include the needed header files
#include “TokenParser.h”
#include <iostream>
#include <string>
using namespace std;
//Driver
int main()
{
//Vector 1
vector<char> vector1 = {‘h’, ‘a’, ‘p’, ‘p’,’y’};
//Vector 2
vector<char> vector2 = {‘d’, ‘a’, ‘y’};
//Vector 3
vector<char> vector3 = {‘c’,’o’,’m’, ‘e’};
//Function call
TokenParser parser(vector1);
//Display
cout << “The contents of the parser loaded with the vector ‘vector1’ is ” << endl;
//Function call
parser.show();
//Display
cout << “nAdding the vector ‘vector2’ into the parser” << endl;
//Function call
parser.addCharacterTokens(vector2);
//Function call
parser.show();
//Display
cout << “nSetting the parser tokens to the vector ‘vector3′” << endl;
//Function call
parser.setCharacterTokens(vector3);
//Function call
parser.show();
//Function call
vector<string> vector4 = parser.parse(“again”);
//Display
cout << “nShowing the vector of strings ‘vector4’ created employing parse(“again”)” <<endl;
//Loop
for(vector<string>::iterator iter = vector4.begin(); iter != vector4.end(); ++iter)
//Display
cout << *iter << ” “;
//Display
cout << “nThe End!” <<endl;
}