Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE DO NOT GIVE ME THAT SAME INCORRECT ANSWER WITH A SCREENSHOT WHICH IS UNFINISHED AND INCORRECT. THIS IS 5TH TIME I AM ASKING SAME

PLEASE DO NOT GIVE ME THAT SAME INCORRECT ANSWER WITH A SCREENSHOT WHICH IS UNFINISHED AND INCORRECT. THIS IS 5TH TIME I AM ASKING SAME QUESTION.

You are given a copy of "lex.h" from Programming Assignment 1, and a file called "ListTokens.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":

  • The function

LexItem id_or_kw(const string& lexeme, int linenum);

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 either the keyword Token if it is found, or the identifier token IDENT if not.

  • The overloaded operator function operator<< for LexItem.

ostream& operator<<(ostream& out, const LexItem& tok);

The operator<< function accepts a reference to an ostream object and a reference to a LexItem object, and returns a reference to the ostream object. The operator<< function should print out the string value of the Token in 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.

Note that the implementation of operator<< function in RA5 differ from its implementation in PA1. 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. See the details of the outputs for the examples in the slides.

lex.h is below:

#ifndef LEX_H_ #define LEX_H_

#include #include #include using namespace std;

//Definition of all the possible token types enum Token { // keywords PROGRAM, WRITE, INT, END, IF, FLOAT, STRING,

// an identifier REPEAT, BEGIN, IDENT,

// an integer, real, and string constant ICONST, RCONST, SCONST,

// the operators, parens, semicolon PLUS, MINUS, MULT, DIV, REM, ASSOP, LPAREN, RPAREN, COMMA, EQUAL, GTHAN, SEMICOL, // 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<<(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_ */

listTokens is below:

#include #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; ifstream file ; LexItem idtoks[] = { LexItem(IDENT,"WRITE",3), LexItem(IDENT,"circle",3), LexItem(IDENT,"square",11), LexItem(IDENT,"rectangle",12), LexItem(IDENT,"ellipse",14), LexItem(IDENT, "PROGRAM", 1), LexItem(IDENT, "REPEAT", 8), LexItem(IDENT, "INT",2), LexItem(IDENT, "FLOAT",3), LexItem(IDENT, "STRING",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(SEMICOL,";",3), LexItem(COMMA,",",3), LexItem(REM,"%",3), LexItem(EQUAL, "==", 4), LexItem( GTHAN, ">", 4 ), 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions