Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

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

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_2

Step: 3

blur-text-image_3

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

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

please dont use chat gpt AI 2 5 0 .

Answered: 1 week ago