You are given a copy of lex.h from Programming Assignment 1, and a file called tokensListing.cpp as a driver program.
DO NOT CHANGE neither lex.h nor tokensListing.cpp.
Your implementation should include the following in another file, called RA5.cpp:
id_or_kw () function accepts a reference to a string of a lexeme and a line number and returns a LexItem object. It searches for the lexeme in a directory that maps a string value of a keyword to its corresponding Token value, and it returns a LexItem object containing the keyword Token if it is found. Otherwise, it returns a LexItem object containing a token for one of the possible types of identifiers (i.e., IDENT, SIDENT, or NIDENT).
/*
* lex.h
*
* CS280
* Spring 2023
*/
#ifndef LEX_H_
#define LEX_H_
#include
#include
#include
using namespace std;
//Definition of all the possible token types
enum Token {
// keywords
WRITELN, IF, ELSE,
// identifiers
IDENT, NIDENT, SIDENT,
// an integer, real, and string constant
ICONST, RCONST, SCONST,
// the numeric operators, assignment, numeric and string comparison operators
PLUS, MINUS, MULT, DIV, EXPONENT, ASSOP, NEQ,
NGTHAN, NLTHAN, CAT, SREPEAT, SEQ, SLTHAN, SGTHAN,
//Delimiters
COMMA, SEMICOL, LPAREN, RPAREN, LBRACES, RBRACES,
// any error returns this token
ERR,
// when completed (EOF), return this token
DONE,
};
//Class definition of LexItem
class LexItem {
Token token;
string lexeme;
int lnum;
public:
LexItem() {
token = ERR;
lnum = -1;
}
LexItem(Token token, string lexeme, int line) {
this->token = token;
this->lexeme = lexeme;
this->lnum = line;
}
bool operator==(const Token token) const { return this->token == token; }
bool operator!=(const Token token) const { return this->token != token; }
Token GetToken() const { return token; }
string GetLexeme() const { return lexeme; }
int GetLinenum() const { return lnum; }
};
extern ostream& operator
extern LexItem id_or_kw(const string& lexeme, int linenum);
extern LexItem getNextToken(istream& in, int& linenum);
#endif /* LEX_H_ */
\
tokensListing.cpp as a driver program.
#include
#include
#include
#include
#include
#include "lex.h"
//#include "ra5.cpp"
/*
* CS280 - Spring 2023
*/
using std::map;
using namespace std;
istream& operator>>(istream& in, LexItem& tok) {
map StrToTok = {
{"WRITELN", WRITELN},
{ "IF", IF },
{ "ELSE", ELSE },
{ "IDENT", IDENT },
{ "NIDENT", NIDENT },
{ "SIDENT", SIDENT },
{ "ICONST", ICONST },
{ "RCONST", RCONST },
{ "SCONST", SCONST },
{ "PLUS", PLUS },
{ "MINUS" , MINUS },
{ "MULT" , MULT },
{ "DIV" , DIV },
{ "EXPONENT" , EXPONENT },
{ "ASSOP", ASSOP },
{ "NEQ", NEQ },
{ "NGTHAN" , NGTHAN },
{ "NLTHAN", NLTHAN },
{ "CAT", CAT },
{ "SREPEAT" , SREPEAT },
{ "SEQ", SEQ },
{ "SGTHAN", SGTHAN },
{ "SLTHAN", SLTHAN },
{ "COMMA", COMMA },
{ "LPAREN", LPAREN },
{ "RPAREN", RPAREN },
{ "LBRACES", LBRACES },
{ "RBRACES", RBRACES },
{ "SEMICOL", SEMICOL },
{ "ERR",ERR },
{ "DONE", DONE },
};
Token tt;
string tokstr, lexeme, str2;
char ch;
int i, line, strlen;
in >> tokstr ;
if(!in)
{
return in;
}
auto ttitr = StrToTok.find(tokstr);
//cout
if(ttitr != StrToTok.end())
{
tt = ttitr->second;
if(tt == IDENT)
{
in >> lexeme >> line;
tok = id_or_kw(lexeme, line);
}
else if (tt == ICONST || tt == RCONST )
{
in >> lexeme >> line;
LexItem item(tt, lexeme, line);
tok = item;
}
else if (tt == SCONST)
{
in >> strlen;
str2 = ""; i = 0;
ch = in.get();
while (ch != '\"')
{
ch = in.get();
}
ch = in.get();
while ( (ch != '\"') && (i
{
str2 += ch;
ch = in.get();
i++;
}
in >> line;
LexItem item(tt, str2, line);
tok = item;
}
else
{
in >> lexeme >> line;
LexItem item(tt, lexeme, line);
tok = item;
}
}
return in;
}
int main(int argc, char *argv[])
{
LexItem item(ERR, "", 0);
LexItem& tokItem = item;
LexItem kwtok;
ifstream file ;
string str1;
if (argc == 1)
{
cerr
return 0;
}
else
{
if(argv[1][0] == '-')
{
cerr
return 0;
}
file.open(argv[1]);
if( file.is_open() == false ) {
cerr
return 0;
}
}
while (file >> tokItem)
{