Question: In this programming assignment, you will be building a lexical analyzer for small programming language, called Mini C - Like ( MCL ) , and

In this programming assignment, you will be building a lexical analyzer for small programming language, called Mini C-Like (MCL), and a program to test it. This assignment will be followed by two other assignments to build a parser and an interpreter to the 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, and operators. The syntax definitions of the MCL 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 ::= PROGRAM IDENT CompStmt
StmtList ::= Stmt { Stmt }
Stmt ::= DeclStmt ; | ControlStmt ; | CompStmt
DeclStmt ::=( INT | FLOAT | BOOL | CHAR | STRING ) VarList
VarList ::= Var [= Expr]{,Var [= Expr]}
ControlStmt ::= AssgnStmt | IfStmt | PrintStmt
PrintStmt ::= PRINT (ExprList)
CompStmt ::={ StmtList }
IfStmt ::= IF (Expr) Stmt [ ELSE Stmt ]
AssgnStmt ::= Var (=|+=|-=|*=|/=|%=) Expr
Var ::= IDENT
ExprList ::= Expr {, Expr }
Expr ::= LogANDExpr {|| LogANDRxpr }
LogANDExpr ::= EqualExpr { && EqualExpr }
EqualExpr ::= RelExpr [(==|!=) RelExpr ]
RelExpr ::= AddExpr [(|>) AddExpr ]
AddExpr :: MultExpr {(+|-) MultExpr }
MultExpr ::= UnaryExpr {(*|/|%) UnaryExpr }
UnaryExpr ::=[(-|+|!)] PrimaryExpr
PrimaryExpr ::= IDENT | ICONST | RCONST | SCONST | BCONST | CCONST |( Expr )
lex.h file:
#ifndef LEXH
#define LEXH
#include
#include
#include
using namespace std;
// Definition of all the possible token types
enum Token {
// keywords OR RESERVED WORDS
IF, ELSE, PRINT, INT, FLOAT,
CHAR, STRING, BOOL, PROGRAM, TRUE, FALSE,
// identifiers
IDENT,
// an integer, real, logical and string constants
ICONST, RCONST, SCONST, BCONST, CCONST,
// the arithmetic operators, logic operators, relational operators
PLUS, MINUS, MULT, DIV, ASSOP, EQ, NEQ, ADDASSOP, SUBASSOP, MULASSOP, DIVASSOP,
REMASSOP, GTHAN, LTHAN, AND, OR, NOT, REM,
// Delimiters
COMMA, SEMICOL, LPAREN, RPAREN, DOT, LBRACE, RBRACE,
// any error returns this token
ERR,
// when completed (EOF), return this token
DONE,
};
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; }
};
// Function to convert Token to string representation
inline string tokenPrint(Token tok){
static const map tokenMap ={
{IF,"IF"},{ELSE, "ELSE"},{PRINT, "PRINT"},{INT, "INT"},{FLOAT, "FLOAT"},
{CHAR, "CHAR"},{STRING, "STRING"},{BOOL, "BOOL"},{PROGRAM, "PROGRAM"},
{TRUE, "TRUE"},{FALSE, "FALSE"},{IDENT, "IDENT"},{ICONST, "ICONST"},
{RCONST, "RCONST"},{SCONST, "SCONST"},{BCONST, "BCONST"},{CCONST, "CCONST"},
{PLUS, "PLUS"},{MINUS, "MINUS"},{MULT, "MULT"},{DIV, "DIV"},{ASSOP, "ASSOP"},
{EQ,"EQ"},{NEQ, "NEQ"},{ADDASSOP, "ADDASSOP"},{SUBASSOP, "SUBASSOP"},
{MULASSOP, "MULASSOP"},{DIVASSOP, "DIVASSOP"},{REMASSOP, "REMASSOP"},
{GTHAN, "GTHAN"},{LTHAN, "LTHAN"},{AND, "AND"},{OR,"OR"},{NOT, "NOT"},
{REM, "REM"},{COMMA, "COMMA"},{SEMICOL, "SEMICOL"},{LPAREN, "LPAREN"},
{RPAREN, "RPAREN"},{DOT, "DOT"},{LBRACE, "LBRACE"},{RBRACE, "RBRACE"},
{ERR, "ERR"},{DONE, "DONE"}
};
auto it = tokenMap.find(tok);
if (it != tokenMap.end()){
return it->second;
}
return "UNKNOWN";
}
// Overloaded stream insertion operator for LexItem
inline ostream& operator(ostream& out, const LexItem& tok){
out tokenPrint(tok.GetToken());
if (tok.GetToken()== IDENT || tok.GetToken()== ICONST || tok.GetToken()== RCONST ||
tok.GetToken()== SCONST || tok.GetToken()== BCONST || tok.GetToken()== CCONST){
out "(" tok.GetLexeme()")";
}
return out;
}
// Function to determine if an identifier is a keyword
extern LexItem id_or_kw(const string& lexeme, int linenum);
// Function to get the next token from the input stream
extern LexItem getNextToken(istream& in, int& linenum);
#endif /* LEXH */
In this programming assignment, you will be

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 Programming Questions!