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 CLike 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 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 ie 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 ExprVar 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 ;
LexItemToken token, string lexeme, int line
thistoken token;
thislexeme lexeme;
thislnum line;
bool operatorconst Token token const return thistoken token;
bool operator!const Token token const return thistoken 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 tokenPrintToken tok
static const map tokenMap
IFIFELSE "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"
EQEQNEQ "NEQ"ADDASSOP "ADDASSOP"SUBASSOP "SUBASSOP"
MULASSOP "MULASSOP"DIVASSOP "DIVASSOP"REMASSOP "REMASSOP"
GTHAN "GTHAN"LTHAN "LTHAN"AND "AND"ORORNOT "NOT"
REM "REM"COMMA "COMMA"SEMICOL "SEMICOL"LPAREN "LPAREN"
RPAREN "RPAREN"DOT "DOT"LBRACE "LBRACE"RBRACE "RBRACE"
ERR "ERR"DONE "DONE"
;
auto it tokenMap.findtok;
if it tokenMap.end
return itsecond;
return "UNKNOWN";
Overloaded stream insertion operator for LexItem
inline ostream& operatorostream& out, const LexItem& tok
out tokenPrinttokGetToken;
if tokGetToken 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 idorkwconst string& lexeme, int linenum;
Function to get the next token from the input stream
extern LexItem getNextTokenistream& in int& linenum;
#endif LEXH
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
