Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LEX.H file code: Do not change or modify this code /* * lex.h * * CS280 * Fall 2020 */ #ifndef LEX_H_ #define LEX_H_ #include

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

LEX.H file code: Do not change or modify this code

/* * lex.h * * CS280 * Fall 2020 */

#ifndef LEX_H_ #define LEX_H_

#include #include #include

using namespace std;

//Definition of all the possible token types enum Token { // keywords PROGRAM, PRINT, READ, INTEGER, END, IF, THEN, REAL, CHAR,

// an identifier IDENT,

// an integer 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_ */

In this programming assignment, you will be building a lexical analyzer for small programming language and a program to test it. This assignment will be followed by two other assignments to build a parser and interpreter to the same language. Although, we are not concerned about the syntax definitions of the language in this assignment, we intend to introduce it ahead of Programming Assignment 2 in order to show the language reserved words, constants, and operators. The syntax definitions of a Fortran-Like small programming language are given below using EBNF notations. The details of the meanings (i.e. semantics) of the language constructs will be given later on. Prog = PROGRAM IDENT (Decl) (Stmt) END PROGRAM IDENT Decl = Type : Varlist. Type = INTEGER | REAL CHAR VarList - Var (Var) Stmt = Assigstmt | IfStmt | PrintStmt ReadStmt Print Stmt :- PRINT, ExprList IfStmt. = IF (LogicExpr) THEN {Stmt) END IF AssignStmt - Var = Expr ReadStmt - READ, VarList ExprList = Expr , Expr) Expr = Term { (+/-) Term) Term = SFactor {(*1/) SFactor) SFactor = Sign Factor | Factor LogicExpr = Expr (== | ,<. these operators are for add subract multiply divide concatenate assignment left parenthesis right equality and less than operations. they have the following tokens respectively: plus minus mult div concat assop lparen rparen equal lthan. colon comma characters terminals with tokens: coma. a comment is defined by all exclamation mark l to end of line. does not overlap one recognized ignored token. white spaces skipped. however between used improve readability can be as way delimit tokens. an error will denoted err file done lexical analyzer requirements: you write function called getnexttoken driver program testing it. must signature: lexitem in int linenumber first argument reference istream object that should read from. second integer contains current line number. update this every time it reads newline from input stream. returns object. class token string lexeme number data members. header lex.h provided you. definition enumerated type symbols use provided. may change note getnext performs following: any detected result returned value when was detected. also both unrecoverable. once either shouldn call again. separated but most cases required be. example sequence iconst iconst. similarly sconst sconst. . recommended implement source main test another file. takes several command flags. notations each flag follows: if present printed seen followed its parentheses. prints out unique constants numeric order. real alphabetical order identifiers filename passed function. your open filename. apply rules: arguments begin dash appear multiple times. only last considered. there at name specified on more print new message allowed stop running. no found other flags permitted. unrecognized where whatever given cannot n corresponding then invalid constant message: repeatedly until or err. summary information handles flags-sconsts iconsts rconsts lines: lines counting zero further printed. option per output format capital letters lparen. case ident rconst space identifier literal center circle through points them would be: the-sconsts cause label strings: itself without double quotes sconsts nothing integers: reals: identifiers: comma-separated list idents note: set files associated programming vocareum automatic grading based files. check implementation. available compressed archive pai cases.zip canvas assignment. table below. submission guidelines submit implementation lex.cpp vocareum. propagated work directory. building small language two assignments build parser interpreter same language. although we concerned about syntax definitions intend introduce ahead show reserved words operators. fortran-like below using ebnf notations. details meanings semantics constructs later on. prog="PROGRAM" decl="Type" : varlist. char varlist var stmt="Assigstmt" ifstmt printstmt readstmt :- exprlist ifstmt.="IF" assignstmt expr term sfactor factor logicexpr="Expr" sign="+" rules assigned has referred terminal which letter digit. as: digit ident. digits. digits decimal point rconst. such .2 accepted not. literals delimited single cs literals. escape characters. character quote string. smith home acceptable strings. are: char.>,<. these operators are for add subract multiply divide concatenate assignment left parenthesis right equality and less than operations. they have the following tokens respectively: plus minus mult div concat assop lparen rparen equal lthan. colon comma characters terminals with tokens: coma. a comment is defined by all exclamation mark l to end of line. does not overlap one recognized ignored token. white spaces skipped. however between used improve readability can be as way delimit tokens. an error will denoted err file done lexical analyzer requirements: you write function called getnexttoken driver program testing it. must signature: lexitem in int linenumber first argument reference istream object that should read from. second integer contains current line number. update this every time it reads newline from input stream. returns object. class token string lexeme number data members. header lex.h provided you. definition enumerated type symbols use provided. may change note getnext performs following: any detected result returned value when was detected. also both unrecoverable. once either shouldn call again. separated but most cases required be. example sequence iconst iconst. similarly sconst sconst. . recommended implement source main test another file. takes several command flags. notations each flag follows: if present printed seen followed its parentheses. prints out unique constants numeric order. real alphabetical order identifiers filename passed function. your open filename. apply rules: arguments begin dash appear multiple times. only last considered. there at name specified on more print new message allowed stop running. no found other flags permitted. unrecognized where whatever given cannot n corresponding then invalid constant message: repeatedly until or err. summary information handles flags-sconsts iconsts rconsts lines: lines counting zero further printed. option per output format capital letters lparen. case ident rconst space identifier literal center circle through points them would be: the-sconsts cause label strings: itself without double quotes sconsts nothing integers: reals: identifiers: comma-separated list idents note: set files associated programming vocareum automatic grading based files. check implementation. available compressed archive pai cases.zip canvas assignment. table below. submission guidelines submit implementation lex.cpp vocareum. propagated work directory>

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions