Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <regex.h>
using std::ostream;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::ifstream;
using std::istringstream;
using std::string;
using std::vector;
class Token
{
string Type, Name;
int LineNo, LineCol;
public:
Token(string type, string name, int lineno, int linecol)
: Type(type), Name(name), LineNo(lineno), LineCol(linecol) {}
Token() {}
void SetType (string type) { Type = type; }
void SetName (string name) { Name = name; }
void SetLineNo (int lineno) { LineNo = lineno; }
void SetLineCol(int linecol) { LineCol = linecol; }
string GetType () { return Type; }
string GetName () { return Name; }
int GetLineNo () { return LineNo; }
int GetLineCol() { return LineCol; }
};
ostream& operator<<(ostream &out, Token token)
{
out<<"("<< token.GetType() <<", '"<< token.GetName() <<"', "<< token.GetLineNo() <<", "<< token.GetLineCol() <<")";
return out;
}
void ReadInFile (ifstream&, vector<string>&);
void Lex (vector<string>&, vector<Token>&);
Token Lex (string, int&, int&);
int main(int argc, char *argv[])
{
ifstream File(argv[1]);
vector<string> FileContents;
vector<Token> TokenList;
ReadInFile(File, FileContents);
Lex(FileContents, TokenList);
for(auto &Counter : TokenList)
cout<< Counter << endl;
}
void ReadInFile(ifstream &File, vector<string> &FileContents)
{
int Counter = 0;
string Line;
while(getline(File, Line))
FileContents.push_back(Line);
}
void Lex(vector<string> &FileContents, vector<Token> &TokenList)
{
int LineNo = 1, MBegin, MEnd;
for(auto &Counter : FileContents)
{
(MBegin = 0) && (MEnd = Counter.size() - 1);
while(true)
{
TokenList.push_back(Lex(Counter, MBegin, MEnd));
if(Counter.at(MEnd) == '\n')
{
if(TokenList[TokenList.size() - 1].GetType() == "UNINITIALIZED")
TokenList.pop_back();
break;
}
Counter = Counter.substr(MEnd, Counter.size() - MEnd - 1);
TokenList[TokenList.size() - 1].SetLineNo(LineNo);
}
LineNo++;
}
}
Token Lex(string Line, int &MBegin, int &MEnd)
{
regex_t Regex;
regmatch_t Match;
regcomp(&Regex, "\"[^\"]+\"", REG_EXTENDED);
if(regexec(&Regex, Line.c_str(), 1, &Match, 0) == 0)
{
(MBegin = Match.rm_so) && (MEnd = Match.rm_eo);
return Token("STRING", Line.substr(MBegin + 1, MEnd - MBegin - 2), -1, MBegin + 1);
}
regfree(&Regex);
(MBegin = 0) && (MEnd = Line.size() - 1);
return Token("UNINITIALIZED", "", -1, -1);
}
terminate called after throwing an instance of 'std: