#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,...

#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, real, 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
extern LexItem id_or_kw(const string& lexeme, int linenum);
extern LexItem getNextToken(istream& in, int& linenum);


#endif /* LEX_H_ */

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

#include
#include
#include "lex.h"

using std::map;
using namespace std;

LexItem id_or_kw(const string& lexeme, int linenum)
{
Token tt = IDENT;

auto kIt = kwmap.find(lexeme);
if( kIt != kwmap.end() )
tt = kIt->second;

return LexItem(tt, lexeme, linenum);
}

LexItem
getNextToken(istream& in, int& linenum)
{
enum TokState { START, INID, INSTRING, ININT, INREAL, INCOMMENT } lexstate = START;
string lexeme;
char ch, nextch, nextchar, nextch1;
int strtype = 0;//0 for double quotes and 1 for single quotes

while(in.get(ch)) {
switch( lexstate ) {
case START:
if( ch == '\n' )
linenum++;
if( isspace(ch) )
continue;

lexeme = ch;

if( isalpha(ch) ) {
lexeme = toupper(ch);
lexstate = INID;
}
else if( ch == '"' ) {
lexstate = INSTRING;
}
else if( ch == '\'' ) {
strtype = 1;
lexstate = INSTRING;
}
else if( isdigit(ch) ) {
lexstate = ININT;
}
else if( ch == '!' ) {
lexstate = INCOMMENT;
}
else {
Token tt = ERR;
switch( ch ) {
case '+':
tt = PLUS;
break;
case '-':
tt = MINUS;
break;
case '*':
tt = MULT;
break;
case '/':
nextch1 = in.peek();
if(nextch1 == '/'){
in.get(ch);
tt = CONCAT;
break;
}
tt = DIV;
break;
case '=':
nextchar = in.peek();
if(nextchar == '='){
in.get(ch);
tt = EQUAL;
break;
}
tt = ASSOP;
break;
case '(':
tt = LPAREN;
break;
case ')':
tt = RPAREN;
break;
case ':':
tt = COLON;
break;
case ',':
tt = COMA;
break;
case '
tt = LTHAN;
break;
case '.':
nextch = in.peek();
if(isdigit(nextch)){
lexstate = INREAL;
continue;
}
else {
lexeme += nextch;
return LexItem(ERR, lexeme, linenum);
cout
}
}
return LexItem(tt, lexeme, linenum);
}
break;

case INID:
if( isalpha(ch) || isdigit(ch) ) {
ch = toupper(ch);
lexeme += ch;
}
else {
in.putback(ch);
return id_or_kw(lexeme, linenum);
}
break;

case INSTRING:
if( ch == '\n' ) {
return LexItem(ERR, lexeme, linenum);
}
lexeme += ch;
if( ch == '"' && strtype == 0) {
lexeme = lexeme.substr(1, lexeme.length()-2);
return LexItem(SCONST, lexeme, linenum);
}
else if (ch == '\'' && strtype == 1){
lexeme = lexeme.substr(1, lexeme.length()-2);
return LexItem(SCONST, lexeme, linenum);
}
break;

case ININT:
if( isdigit(ch) ) {
lexeme += ch;
}
else if(ch == '.') {
lexstate = INREAL;
in.putback(ch);
}
else {
in.putback(ch);
return LexItem(ICONST, lexeme, linenum);
}
break;
case INREAL:
if( ch == '.' && isdigit(in.peek()) ) {
lexeme += ch;
}
else if(isdigit(ch)){
lexeme += ch;
}
else if(ch == '.' && !isdigit(in.peek())){
return LexItem(ERR, lexeme, linenum);
}
else {
in.putback(ch);
return LexItem(RCONST, lexeme, linenum);
}
break;
case INCOMMENT:
if( ch == '\n' ) {
++linenum;
lexstate = START;
}
break;
}

}//end of while loop

if( in.eof() )
return LexItem(DONE, "", linenum);
return LexItem(ERR, "some strange I/O error", linenum);
}

ostream& operator
Token tt = tok.GetToken();
out
if( tt == IDENT || tt == ICONST || tt == SCONST || tt == RCONST || tt == ERR ) {
out
}
return out;
}


---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

#ifndef PARSE_H_
#define PARSE_H_

#include

using namespace std;

#include "lex.h"
#include "val.h"


extern bool Prog(istream& in, int& line);
extern bool Stmt(istream& in, int& line);
extern bool Decl(istream& in, int& line);
extern bool PrintStmt(istream& in, int& line);
extern bool IfStmt(istream& in, int& line);
extern bool ReadStmt(istream& in, int& line);
extern bool IdList(istream& in, int& line, LexItem & tok);
extern bool VarList(istream& in, int& line);
extern bool Var(istream& in, int& line, LexItem & tok);
extern bool AssignStmt(istream& in, int& line);
extern bool ExprList(istream& in, int& line);
extern bool LogicExpr(istream& in, int& line, Value & retVal);
extern bool Expr(istream& in, int& line, Value & retVal);
extern bool Term(istream& in, int& line, Value & retVal);
extern bool SFactor(istream& in, int& line, Value & retVal);
extern bool Factor(istream& in, int& line, int sign, Value & retVal);
extern int ErrCount();

#endif /* PARSE_H_ */

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------


#include
#include

#include "lex.h"
#include "val.h"
#include "parserInt.h"


using namespace std;
//extern int error_count;

int main(int argc, char *argv[])
{
int lineNumber = 1;

istream *in = NULL;
ifstream file;
for( int i=1; i{
string arg = argv[i];
if( in != NULL )
{
cerr
return 0;
}
else
{
file.open(arg.c_str());
if( file.is_open() == false )
{
cerr
return 0;
}

in = &file;
}
}
bool status = Prog(*in, lineNumber);
if( !status ){
cout
}
else{
cout
}
}


---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

#include "val.h"


// add this with op
Value Value::operator+(const Value& op) const {
if( GetType() == op.GetType() )
{
if( IsInt() ) return Value( Itemp + op.GetInt() );
if( IsReal() ) return Value( Rtemp + op.GetReal() );
}
else if(IsInt() && op.IsReal())
{
return Value( (float) GetInt() + op.GetReal());
}
else if(IsReal() && op.IsInt())
{
return Value(GetReal() + (float) op.GetInt());
}
return Value();
}
// subtract op from this
Value Value::operator-(const Value& op) const {
if( GetType() == op.GetType() )
{
if( IsInt() ) return Value( Itemp - op.GetInt() );
if( IsReal() ) return Value( Rtemp - op.GetReal() );
}
else if(IsInt() && op.IsReal())
{
return Value( (float) GetInt() - op.GetReal());
}
else if(IsReal() && op.IsInt())
{
return Value(GetReal() - (float) op.GetInt());
}
return Value();
}
// multiply this with op
Value Value::operator*(const Value& op) const {
if( GetType() == op.GetType() )
{
if( IsInt() ) return Value( Itemp * op.GetInt() );
if( IsReal() ) return Value( Rtemp * op.GetReal() );
}
else if(IsInt() && op.IsReal())
{
return Value( (float) GetInt() * op.GetReal());
}
else if(IsReal() && op.IsInt())
{
return Value(GetReal() * (float) op.GetInt());
}
return Value();
}
// divide this by op
Value Value::operator/(const Value& op) const {
if( GetType() == op.GetType() )
{
if( IsInt() ) return Value( Itemp / op.GetInt() );
if( IsReal() ) return Value( Rtemp / op.GetReal() );
}
else if(IsInt() && op.IsReal())
{
return Value( (float) GetInt() / op.GetReal());
}
else if(IsReal() && op.IsInt())
{
return Value(GetReal() / ((float) op.GetInt()));
}
return Value();
}
Value Value::operator==(const Value& op) const {
if( GetType() == op.GetType() )
{
if( IsInt() ) return Value( (bool)(Itemp == op.GetInt() ));
if( IsReal() ) return Value( (bool) (Rtemp == op.GetReal() ));
if( IsChar()) return Value( (bool) (Stemp
}
else if(IsInt() && op.IsReal())
{
return Value( (bool) (((float) GetInt())== op.GetReal()));
}
else if(IsReal() && op.IsInt())
{
return Value( (bool) (GetReal() == ((float) op.GetInt())));
}
return Value();
}
Value Value::operator
if( GetType() == op.GetType() )
{
if( IsInt() ) return Value( Itemp
if( IsReal() ) return Value( Rtemp
if( IsChar()) return Value( Stemp
}
else if(IsInt() && op.IsReal())
{
return Value( ((float) GetInt())
}
else if(IsReal() && op.IsInt())
{
return Value(GetReal()
}
return Value();
}

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------



#ifndef VALUE_H
#define VALUE_H

#include
#include
#include
#include

using namespace std;

enum ValType { VINT, VREAL, VCHAR, VBOOL, VERR };

class Value {
ValType T;
bool Btemp;
int Itemp;
float Rtemp;
string Stemp;
public:
Value() : T(VERR), Itemp(0), Rtemp(0.0){}
Value(bool vb) : T(VBOOL), Btemp(vb), Itemp(0), Rtemp(0.0) {}
Value(int vi) : T(VINT), Itemp(vi) {}
Value(float vr) : T(VREAL), Itemp(0), Rtemp(vr) {}
Value(string vs) : T(VCHAR), Itemp(0), Rtemp(0.0), Stemp(vs) {}
ValType GetType() const { return T; }
bool IsErr() const { return T == VERR; }
bool IsInt() const { return T == VINT; }
bool IsChar() const { return T == VCHAR; }
bool IsReal() const {return T == VREAL;}
bool IsBool() const {return T == VBOOL;}
int GetInt() const { if( IsInt() ) return Itemp; throw "RUNTIME ERROR: Value not an integer"; }
string GetChar() const { if( IsChar() ) return Stemp; throw "RUNTIME ERROR: Value not a string"; }
float GetReal() const { if( IsReal() ) return Rtemp; throw "RUNTIME ERROR: Value not a real"; }
bool GetBool() const {if(IsBool()) return Btemp; throw "RUNTIME ERROR: Value not a boolean";}
void SetType(ValType type)
{
T = type;
}
void SetInt(int val){
if( IsInt() ) {
Itemp = val;
//cout }
else
throw "RUNTIME ERROR: Type not an integer";
}
void SetReal(float val){
if( IsReal() )
Rtemp = val;
else
throw "RUNTIME ERROR: Type not a real";
}
void SetChar(string val){
if( IsChar() )
Stemp = val;
else
throw "RUNTIME ERROR: Type not a string";
}
void SetBool(bool val){
if(IsBool())
Btemp = val;
else
throw "RUNTIME ERROR: Type not a boolean";
}
// add op to this
Value operator+(const Value& op) const;
// subtract op from this
Value operator-(const Value& op) const;
// multiply this by op
Value operator*(const Value& op) const;
// divide this by op
Value operator/(const Value& op) const;
Value operator==(const Value& op) const;

Value operator
friend ostream& operator
if( op.IsInt() ) out
else if( op.IsChar() ) out
else if( op.IsReal()) out
else out
return out;
}
};


#endif

May 02, 2021

Submit New Assignment

Copy and Paste Your Assignment Here