Question: 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

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.

Files:

lex.h

/* * lex.h */

#ifndef LEX_H_ #define LEX_H_

#include #include using std::string; using std::istream; using std::ostream;

//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 tokenPrint = { {PROGRAM, "PROGRAM"}, {READ, "READ"}, {INTEGER, "INTEGER"}, {REAL, "REAL"}, {CHAR, "CHAR"}, { PRINT, "PRINT" }, { IF, "IF" }, { END, "END" }, {THEN, "THEN"},

{ 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 kwmap = { {"PROGRAM", PROGRAM}, {"READ", READ}, { "INTEGER", INTEGER}, { "REAL", REAL}, { "CHAR", CHAR}, { "PRINT", PRINT }, { "IF", IF }, {"THEN", THEN}, { "END", END }, };

//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 #include #include "lex.h" #include "ra5.cpp"

/* * CS280 - Fall 2020 */

#include #include using std::map;

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 string arg( argv[i] ); if( arg == "-idsonly" ) idsflag = true; else if( arg == "-othertok" ) tokflag = true; else if( arg[0] == '-' ) { cerr << "UNRECOGNIZED FLAG " << arg << endl; return 0; } } if(idsflag) { cout << "Identifiers and Keywords:" << endl; for( i = 0; idtoks[i].GetLexeme() != "END"; i++ ) { kwtok = id_or_kw(idtoks[i].GetLexeme(), toks[i].GetLinenum()); cout << kwtok; } kwtok = id_or_kw(idtoks[i].GetLexeme(), toks[i].GetLinenum()); cout << kwtok; } if(tokflag) { cout << "Other tokens:" << endl; for( i = 0; toks[i] != DONE; i++ ) { cout << toks[i]; } cout << toks[i]; } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!