Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

THIS IS THE THIRD TIME I AM POSTING THIS QUESTION, AND SO FAR NO ONE CAN SOLVE IT. PLEASE HELP ME I REALLY NEED IT!!!!!!!!!!!!

THIS IS THE THIRD TIME I AM POSTING THIS QUESTION, AND SO FAR NO ONE CAN SOLVE IT. PLEASE HELP ME I REALLY NEED IT!!!!!!!!!!!!

In this programming assignment, you will be building a lexical analyzer for small programming language, called Simple Perl-Like (SPL), and a program to test it. This assignment will be followed by two other assignments to build a parser and an interpreter to the SPL 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 determine the language terminals: reserved words, constants, identifier(s), and operators. The syntax definitions of the SPL language are given below using EBNF notations. However, the details of the meanings (i.e. semantics) of the language constructs will be given later on.

Prog ::= StmtList

StmtList ::= Stmt ;{ Stmt; }

Stmt ::= AssignStme | WriteLnStmt | IfStmt

WriteLnStmt ::= WRITELN (ExprList)

IfStmt ::= IF (Expr) { StmtList } [ ELSE { StmtList } ]

AssignStmt ::= Var = Expr

Var ::= NIDENT | SIDENT

ExprList ::= Expr { , Expr }

Expr ::= RelExpr [(-eq|==) RelExpr ]

RelExpr ::= AddExpr [ ( -lt | -gt | < | > ) AddExpr ]

AddExpr :: MultExpr { ( + | - | .) MultExpr }

MultExpr ::= ExponExpr { ( * | / | **) ExponExpr }

ExponExpr ::= UnaryExpr { ^ UnaryExpr }

UnaryExpr ::= [( - | + )] PrimaryExpr

PrimaryExpr ::= IDENT | SIDENT | NIDENT | ICONST | RCONST | SCONST | (Expr).

here is the 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,

// an identifiers IDENT, NIDENT, SIDENT,

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

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

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

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students explore these related Databases questions

Question

Write the structure for a. dCMP b. dGMP

Answered: 3 weeks ago