Question: LEX.H FILE GIVEN AND CANNOT CHANGE OR MODIFY THIS FILE. CODE: #ifndef LEX_H_ #define LEX_H_ #include #include #include using namespace std; //Definition of all the

 LEX.H FILE GIVEN AND CANNOT CHANGE OR MODIFY THIS FILE. CODE:

#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,

LEX.H FILE GIVEN AND CANNOT CHANGE OR MODIFY THIS FILE.

CODE:

#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 PrintStmt :- PRINT, Exprlist IfStmt - IF (LogicExpr) THEN (Stmt) END IF AssignStmt Expr ReadStmt = READ, VarList ExprList Expr | Expr) Expr = Term { (+1-) Term) Term = SFactor ((*/) SFactor) SFactor Sign Factor | Factor LogicExpr Expr (==|

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!