Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions