Answered step by step
Verified Expert Solution
Question
1 Approved Answer
DO NOT CHANGE lex.h (use the one here not a new one) lex.h: #ifndef LEX_H_ #define LEX_H_ #include #include #include using namespace std ; //Definition
DO NOT CHANGE lex.h (use the one here not a new one)
lex.h:#ifndef LEX_H_#define LEX_H_#include#include #include using namespace std;//Definition of all the possible token typesenum Token {// keywordsPROGRAM, PRINT, READ, INTEGER, END, IF, THEN, REAL, CHAR,// an identifierIDENT,// an integer and string constantICONST, RCONST, SCONST,// the operators, parens, semicolonPLUS, MINUS, MULT, DIV, ASSOP, LPAREN, RPAREN, COMA,EQUAL, LTHAN, CONCAT, COLON,// any error returns this tokenERR,// when completed (EOF), return this tokenDONE};static map string> 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 mapstring,Token> kwmap = {{"PROGRAM", PROGRAM},{"READ", READ},{ "INTEGER", INTEGER},{ "REAL", REAL},{ "CHAR", CHAR},{ "PRINT", PRINT },{ "IF", IF },{"THEN", THEN},{ "END", END },};//Class definition of LexItemclass LexItem {Tokentoken;stringlexeme;intlnum;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; }TokenGetToken() const { return token; }stringGetLexeme() const { return lexeme; }intGetLinenum() const { return lnum; }};extern ostream& operatorconst LexItem& tok);extern LexItem id_or_kw(const string& lexeme, int linenum);extern LexItem getNextToken(istream& in, int& linenum);#endif /* LEX_H_ */
ALSO YOU CAN USE THIS TO HELP YOU:
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