Question
Pls Help !! You are given a copy of lex.h from Programming Assignment 1, and a file called tokensListing.cpp as a driver program. DO NOT
Pls Help !!
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 tokensListing.cpp
Your implementation should include the following in another file, called lex.cpp:
The function
LexItem id_or_kw(const string& lexeme, int linenum);
The overloaded operator function
operator<< for LexItem. ostream& operator<<(ostream& out, const LexItem& tok);
The operator<< function should print out the token of the tok object, followed by its line number. If the token is either an IDENT, ICONST, RCONST, or SCONST, it will print out its token followed by its lexeme between parentheses, and its line number. See the examples below for the output format.
Use the given driver program in tokensListing.cpp for testing your implementations. The driver program accepts two command line arguments, -othertok and -idsonly. The -othertok flag is used to test your implementation of the overloaded operator<< function only, and the -idsonly is used to test your implementation of both functions.
For example, the output of the testing program, when the -idsonly argument is found, is as the follows: Identifiers and Keywords: PRINT Line #: 15 IDENT (circle) Line #: 5 IDENT (square) Line #: 6 IDENT (rectangle) Line #: 7 IDENT (ellipse) Line #: 3 PROGRAM Line #: 3 READ Line #: 3 INTEGER Line #: 3 REAL Line #: 3 CHAR Line #: 3 IF Line #: 3 THEN Line #: 3 END Line #: 3
While, the output of the testing program, when the -othertok argument is found, is as the follows: Other tokens: END Line #: 15 ICONST (579) Line #: 5 SCONST (hello NJIT) Line #: 6 RCONST (5.79) Line #: 7 PLUS Line #: 3 MINUS Line #: 3 MULT Line #: 3 DIV Line #: 3 ASSOP Line #: 3 LPAREN Line #: 3 RPAREN Line #: 3 COLON Line #: 3 COMA Line #: 3 COMA Line #: 3 EQUAL Line #: 4 LTHAN Line #: 4 CONCAT Line #: 6 ERR (ERR) Line #: 3 DONE Line #: 3
lex.h
/* * lex.h */
#ifndef LEX_H_ #define LEX_H_
#include
//Definition of all the possible token types enum Token { // keywords PROGRAM, PRINT, READ, INTEGER, END, IF, THEN, REAL, CHAR,
// an identifier IDENT,
// an integer, real, and string constant ICONST, RCONST, SCONST,
// the operators, parens, semicolon PLUS, MINUS, MULT, DIV, ASSOP, LPAREN, RPAREN, COMA, EQUAL, LTHAN, CONCAT, COLON, // any error returns this token ERR,
// when completed (EOF), return this token DONE };
static map
{ IDENT, "IDENT" },
{ ICONST, "ICONST" }, { RCONST, "RCONST" }, { SCONST, "SCONST" }, { PLUS, "PLUS" }, { MINUS, "MINUS" }, { MULT, "MULT" }, { DIV, "DIV" }, { ASSOP, "ASSOP" }, { LPAREN, "LPAREN" }, { RPAREN, "RPAREN" }, { COLON, "COLON" }, {COMA, "COMA" }, { EQUAL, "EQUAL" }, { LTHAN, "LTHAN" }, { CONCAT, "CONCAT" }, { ERR, "ERR" },
{ DONE, "DONE" }, };
static map
//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<<(ostream& out, const LexItem& tok); extern LexItem id_or_kw(const string& lexeme, int linenum); extern LexItem getNextToken(istream& in, int& linenum);
#endif /* LEX_H_ */
tokensListing.cpp
#include
/* * main function to test lex.cpp */
#include
using namespace std;
int main(int argc, char *argv[]) { LexItem tok; LexItem kwtok; LexItem idtoks[] = { LexItem(IDENT,"PRINT",3), LexItem(IDENT,"circle",3), LexItem(IDENT,"square",11), LexItem(IDENT,"rectangle",12), LexItem(IDENT,"ellipse",14), LexItem(IDENT, "PROGRAM", 1), LexItem(IDENT, "READ", 8), LexItem(IDENT, "INTEGER",2), LexItem(IDENT, "REAL",3), LexItem(IDENT, "CHAR",4), LexItem(IDENT, "IF", 5), LexItem(IDENT, "THEN", 5), LexItem(IDENT, "END", 0) }; LexItem toks[] = { LexItem(END,"END",15),
LexItem(ICONST,"579",5), LexItem(SCONST,"hello NJIT",6), LexItem(RCONST,"5.79",7), LexItem(PLUS,"+",3), LexItem(MINUS,"-",3), LexItem(MULT,"*",3), LexItem(DIV,"/",3), LexItem(ASSOP,"=",3), LexItem(LPAREN,"(",3), LexItem(RPAREN,")",3), LexItem(COLON,":",3), LexItem(COMA,",",3), LexItem(COMA,",",3), LexItem( EQUAL, "EQUAL", 4), LexItem( LTHAN, "LTHAN", 4 ), LexItem( CONCAT, "CONCAT", 6 ), LexItem(ERR, "ERR", 3), LexItem(DONE,"DONE",3), };
bool idsflag = false; bool tokflag = false; int i;
if (argc == 1) { cerr << "No Specified arguments." << endl; return 0; } for( int i=1; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started