Question
Lex.h /* * lex.h * * CS280 * Spring 2023 */ #ifndef LEX_H_ #define LEX_H_ #include #include #include using namespace std; //Definition of all the
Lex.h
/* * lex.h * * CS280 * Spring 2023 */
#ifndef LEX_H_ #define LEX_H_
#include
//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<<(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 "lex.h"
extern ostream operator <<(ostream & out, const tok & tok) { out<< "Token: "<< tok.GetToken( )<< "lex:" << tok.Getlexeme( )<< "Line No: " < #ifndef LEX_H_ #define LEX_H_ #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 << (ostream &out, const Tok &tok); extern Tok getNextToken (istream&in, int & line num); #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<< toks[i] < 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
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