Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LEX.H /* * lex.h * * CS280 * Spring 2021 */ #ifndef LEX_H_ #define LEX_H_ #include #include #include using std::string; using std::istream; using std::ostream; using

image text in transcribed

image text in transcribed

LEX.H

/* * lex.h * * CS280 * Spring 2021 */

#ifndef LEX_H_ #define LEX_H_

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

//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

#endif /* LEX_H_ */

TokensListing.cpp

#include #include #include "lex.h"

/* * CS280 - Spring 2021 */

#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

Please read all the given instruction

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 RA5.cpp": The function LexItem id_or_kw (const string& lexeme, int linenum); The overloaded operator function operator

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

How is iteration used across phases?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago

Question

HOW MANY TOTAL WORLD WAR?

Answered: 1 week ago

Question

Discuss the scope of financial management.

Answered: 1 week ago

Question

Discuss the goals of financial management.

Answered: 1 week ago