Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lex.h /* * lex.h * * CS280 * Spring 2023 */ #ifndef LEX_H_ #define LEX_H_ #include #include #include using namespace std; //Definition of all the

image text in transcribed

Lex.h

/* * lex.h * * CS280 * Spring 2023 */

#ifndef LEX_H_ #define LEX_H_

#include #include #include using namespace std;

//Definition of all the possible token types enum Token { // keywords WRITELN, IF, ELSE,

// identifiers IDENT, NIDENT, SIDENT,

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

// the numeric operators, assignment, numeric and string comparison operators PLUS, MINUS, MULT, DIV, EXPONENT, ASSOP, NEQ, NGTHAN, NLTHAN, CAT, SREPEAT, SEQ, SLTHAN, SGTHAN, //Delimiters COMMA, SEMICOL, LPAREN, RPAREN, LBRACES, RBRACES, // 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

#endif /* LEX_H_ */

tokensListing.cpp

#include "lex.h"

extern ostream operator

#ifndef LEX_H_ #define LEX_H_ #include #include

using std:: string; using std:: istream; using std:: ostream;

Enum Token {

//Keywords PRINT,PRINTLN, REPEAT, BEGIN, END

// an identifier

INDENT,

// an Integer and String constant

ICONST, SCONST,

//Operators, Parens, semicolon

PLUS, MINUS, STAR, SLASH, EQ, LPAREN, RPAREN, SC,

// any error returns this token

ERR,

//When completed (EDF), return this token

DONE }; Class Tok { Token token; String lexeme; int inum;

public Tok( ){ token = ERR; lnum = -1; }

Token( 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_*/

#include 'lex.h' int main( ) { Tok toks[ ] = { TOK(PRINT, "PRINT", 3); TOK(PRINTLN, "PRINTLN",3); TOK(REPEAT, "REPEAT", 3); TOK(BEGIN, "BEGIN",3); TOK(END, "END", 3); TOK(IDENT, "foo", 3); TOK(CONST,"347",3); TOK(SCONST, "Hello world", 3); TOK(PLUS, " +" , 3); TOK(MINUS, "-", 3); TOK(STAR, "*",3); TOK(SLASH, "/",3); TOK(EQ, "=",3); TOK(LPAREN, "(",3); TOK(RPAREN, ")", 3); TOK(SC, ":",3); TOK(DONE,"DONE",3); }; for (int i = 0; toks[i]! = DONE; i++) cout

return 0;

}

Case 1 input

IDENT PROGRAM 0 IDENT $PROG1 3 IDENT INT 2 IDENT _X1 2 COMMA , 2 IDENT @Y_1 2 COMMA , 2 IDENT Z 2 MINUS - 2 COMMA , 2 IDENT W234 2 SEMICOL ; 2 IDENT FLOAT 3 IDENT Z 3 ASSOP = 3 RCONST 0.75 3 SEMICOL ; 3 IDENT $BOOL 4 IDENT @FLAG 4 ASSOP = 4 SEMICOL ; 4 IDENT R 6 ERR $ 6 IDENT FLAG 7 ASSOP = 7 SEMICOL ; 7 IDENT IF 8 LPAREN ( 8 IDENT FLAG 8 RPAREN ) 8 IDENT THEN 8 IDENT Y_1 9 ASSOP = 9 ICONST 5 9 SEMICOL ; 9 IDENT ELSE 10 IDENT Y_1 11 ASSOP = 11 RCONST 7.5 11 SEMICOL ; 11 IDENT END 12 IDENT IF 12 IDENT PRINT 13 SCONST 8 "Value = " 13 COMMA , 13 IDENT R 13 COMMA , 13 SCONST 4 "z = " 13 COMMA , 13 IDENT Z 13 SEMICOL ; 13 IDENT END 14 IDENT PROGRAM 14

Case 1 output

IDENTIFIER: PROGRAM at Line 0 NIDENT: "$PROG1" at Line 3 IDENTIFIER: INT at Line 2 IDENTIFIER: _X1 at Line 2 COMMA: "," at Line 2 SIDENT: "@Y_1" at Line 2 COMMA: "," at Line 2 IDENTIFIER: Z at Line 2 MINUS: "-" at Line 2 COMMA: "," at Line 2 IDENTIFIER: W234 at Line 2 SEMICOL: ";" at Line 2 IDENTIFIER: FLOAT at Line 3 IDENTIFIER: Z at Line 3 ASSOP: "=" at Line 3 RCONST: (0.75) at Line 3 SEMICOL: ";" at Line 3 NIDENT: "$BOOL" at Line 4 SIDENT: "@FLAG" at Line 4 ASSOP: "=" at Line 4 SEMICOL: ";" at Line 4 IDENTIFIER: R at Line 6 Error: : "$" at Line 6 IDENTIFIER: FLAG at Line 7 ASSOP: "=" at Line 7 SEMICOL: ";" at Line 7 IF: "IF" at Line 8 LPAREN: "(" at Line 8 IDENTIFIER: FLAG at Line 8 RPAREN: ")" at Line 8 IDENTIFIER: THEN at Line 8 IDENTIFIER: Y_1 at Line 9 ASSOP: "=" at Line 9 ICONST: (5) at Line 9 SEMICOL: ";" at Line 9 ELSE: "ELSE" at Line 10 IDENTIFIER: Y_1 at Line 11 ASSOP: "=" at Line 11 RCONST: (7.5) at Line 11 SEMICOL: ";" at Line 11 IDENTIFIER: END at Line 12 IF: "IF" at Line 12 IDENTIFIER: PRINT at Line 13 SCONST: 'Value = ' at Line 13 COMMA: "," at Line 13 IDENTIFIER: R at Line 13 COMMA: "," at Line 13 SCONST: 'z = ' at Line 13 COMMA: "," at Line 13 IDENTIFIER: Z at Line 13 SEMICOL: ";" at Line 13 IDENTIFIER: END at Line 14 IDENTIFIER: PROGRAM at Line 14

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 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 Lexltem 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 Lexltem object containing the keyword Token if it is found. Otherwise, it returns a Lexitem object containing a token for one of the possible types of identifiers (i.e., IDENT, SIDENT, or NIDENT). - 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

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions

Question

a. When did your ancestors come to the United States?

Answered: 1 week ago