I need to construct a c++ 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.
How can I do this by making use of the algoritms module, std::find, std::vector, and iterator types.
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.
How can I do this?
//Code Image
//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 . ]
//Code to copy
//TokenParser.h
#pragma once
//TokenParser.h
#include<iostream>
#include<string>
using namespace std;
class TokenParser {
private:
vector<char>delimiters;
vector<string> tokenize(string snippet) ;
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) const;
};
//Default constructor of TokenParser
TokenParser::TokenParser() {
}
//parameterized constructor of TokenParser
TokenParser::TokenParser(const vector<char>&tokens) {
delimiters = tokens;
}
//Implementation of setCharacterTokens method
void TokenParser::setCharacterTokens(const vector<char>& tokens) {
delimiters = tokens;
}
//Implementation of addCharacterTokens method
void TokenParser::addCharacterTokens(const vector<char>& newTokens) {
delimiters.insert(delimiters.end(), newTokens.begin(), newTokens.end());
}
//Implementation of addCharacterToken method
void TokenParser::addCharacterToken(char newToken) {
delimiters.push_back(newToken);
}
//Implementation of tokenize method
vector<string> TokenParser::tokenize(string snippet) {
vector<string> parserVector;
int position = snippet.find("");
//iterate the loop
while (position != -1) {
string resultstring = snippet.substr(0, position);
snippet = snippet.substr(position + 1);
parserVector.push_back(resultstring);
position = snippet.find("");
}
parserVector.push_back(snippet);
return parserVector;
}
//Implementation of parse method
vector<string> TokenParser::parse(string input) const {
string result = " ";
int doubleQuotes = 0;
unsigned int j;
for (j = 0; j < input.length(); j++) {
char character = input[j];
if (character == ' ' || character == '\n' || character == '\t') {
result = result + ' ';
}
else if (std::find(delimiters.begin(), delimiters.end(), character) != delimiters.end()) {
if (character == '\"') {
doubleQuotes = doubleQuotes + 1;
if (doubleQuotes % 2 == 1) {
result = result + character;
result = result + ' ';
}
else {
result = result + ' ';
result = result + character;
}
}else{
result = result + ' ';
result = result + character;
result = result + ' ';
}
}
else {
result = result + character;
}
}
vector <string>parser = tokenize(result);
return parser;
}
//TokenParser.cpp
//TokenParser.cpp
#include <iostream>
#include <vector>
#include "TokenParser.h"
#include<string>
using namespace std;
//Implementation of printTokens method
void printTokens(const vector<string>& tokens)
{
cout << "[ ";
for (auto entry : tokens)
{
cout << entry << " ";
}
cout << "]\n";
}
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!\tDon't you agree?}";
string input2 = "I'll sing \"Amazing Grace\" for $5.\nPlease call1 the #867-5309.";
TokenParser* parser = new TokenParser();
//Display statements
cout << "Input strings\n"
<< "-------------\n"
<< "[" << input1 << "]\n"
<< "[" << input2 << "]\n"
<< "\n";
cout << "\n"
<< "Whitespace only:\n"
<< "---------------\n";
printTokens(parser->parse(input1));
printTokens(parser->parse(input2));
parser->setCharacterTokens(blockTokens);
cout << "\n"
<< "Block markers:\n"
<< "--------------\n";
printTokens(parser->parse(input1));
printTokens(parser->parse(input2));
parser->addCharacterTokens(punctuationTokens);
cout << "\n"
<< "Punctuation added:\n"
<< "------------------\n";
printTokens(parser->parse(input1));
printTokens(parser->parse(input2));
//Iterate the loop
for (char symbol : symbols)
{
parser->addCharacterToken(symbol);
}
//Display statement
cout << "\n"
<< "Symbols added:\n"
<< "--------------\n";
printTokens(parser->parse(input1));
printTokens(parser->parse(input2));
delete parser;
return 0;
}
I need to construct a c++ class that defines a parser. This parser will be able...
You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process the tokens and determine if there are errors in the program. This program have 3 files Lexer.java, Token.java, Parser.java Part 1 Lexer: The class Lexer should have the following methods: public constructor which takes a file name as a parameter○private getInput which reads the data from the...
This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...
2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...
Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...
Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...
I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...
If i could get any guidance on how to get this started it will be great. My prof. literally just gave us the information below (which are literally just instructions) and I am unsure on how to get started. For this assignment we are going to create a function that reads an input stream and classifies it into “tokens” from a language that we define here. In this language we make the following rules: ● An identifier is a letter...
c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...
This is for my c++ class and I would really appreciate the help, Thank you! Complete the definitions of the functions for the ConcessionStand class in the ConcessionStand.cpp file. The class definition and function prototypes are in the provided ConcessionStand.h header file. A testing program is in the provided main.cpp file. You don’t need to change anything in ConcessionStand.h or main.cpp, unless you want to play with different options in the main.cpp program. ___________________ Main.cpp ____________________ #include "ConcessionStand.h" #include <iostream>...
Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and will add various functionality. We will store the following private variables specific to Warehouse: . a std::vector of float values which indicate the monetary values of each sale made by this salesman . a std::string which stores that salesman's position title . a float which stores their commission percentage, i.e., the percentage of sales they receive as a paycheck . a float which stores...