Professional Documents
Culture Documents
Compiler Construction LAB7
Compiler Construction LAB7
01-134201-083
LAB-07
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;
ifstream file("LAB5.txt");
int counter = 0;
char c;
typedef enum
{
S_ORB, S_CRB, S_CCB, S_OCB, S_PLUS, S_MINUS, S_DIV, S_MUL, S_COLON, S_COMMA,
S_SEMICOLON, S_INC, S_DEC, S_Equal, S_DoubleEqual, S_AND, S_OR, S_LessEqual, S_GreaterEqual,
S_PLUSEqual, S_LessThen, S_GreaterThen, S_identifier, S_Number,
S_IFidentifier, S_WHILEidentifier,A_T, S_Factor, S_DOidentifier, S_ELSEidentifier, S_ENDidentifier,
S_SWITCHidentifier, S_MIN, S_BEGINidentifier, S_BREAKidentifierntifier, S_INTidentifier, S_MAINidentifier
}tokenrecord;
struct S_Record
{
tokenrecord t;
string a;
};
struct symboltable
{
int id;
string identifier;
};
symboltable arr[50];
int insert(string str)
{
for (int i = 0; i <= counter; i++)
{
if (arr[i].identifier == str)
{
return arr[i].id;
}
}
counter++;
arr[counter].identifier = str;
arr[counter].id = counter;
return arr[counter].id;
}
S_Record lexical()
{
S_Record obj;
if (file.is_open())
{
while (file.get(c))
{
if (isspace(c))
{
}
else
{
if (c == '(')
{
obj.t = S_ORB;
obj.a = "Opening-Round-Bracket";
return obj;
}
else if (c == ')')
{
obj.t = S_CRB;
obj.a = "Closing-Round-Bracket";
return obj;
}
else if (c == '{')
{
obj.t = S_OCB;
obj.a = "Opening-Curly-Bracket";
return obj;
}
else if (c == '}')
{
obj.t = S_OCB;
obj.a = "Closing-Curly-Bracket";
return obj;
}
else if (c == ',')
{
obj.t = S_COMMA;
obj.a = "Comma";
return obj;
}
else if (c == '=')
{
file.get(c);
if (c == '=')
{
obj.t = S_DoubleEqual;
obj.a = "Double-Equal";
return obj;
}
else
{
file.putback(c);
obj.t = S_Equal;
obj.a = "Equal";
return obj;
}
}
else if (c == '&')
{
obj.t = S_AND;
obj.a = "And";
return obj;
}
else if (c == '|')
{
obj.t = S_OR;
obj.a = "OR";
return obj;
}
else if (c == ';')
{
obj.t = S_SEMICOLON;
obj.a = "Semi-Colon";
return obj;
}
else if (c == ':')
{
obj.t = S_COLON;
obj.a = "Colon";
return obj;
}
else if (c == '+')
{
file.get(c);
if (c == '+')
{
obj.t = S_INC;
obj.a = "Increment";
return obj;
}
else if (c == '=')
{
obj.t = S_PLUSEqual;
obj.a = "Plus-Equal";
return obj;
}
else
{
file.putback(c);
obj.t = S_PLUS;
obj.a = "Plus";
return obj;
}
}
else if (c == '-')
{
file.get(c);
if (c == '-')
{
obj.t = S_DEC;
obj.a = "Decrement";
return obj;
}
else
{
file.putback(c);
obj.t = S_MINUS;
obj.a = "Minus";
return obj;
}
}
else if (c == '*')
{
obj.t = S_MUL;
obj.a = "Multiply";
return obj;
}
else if (c == '/')
{
file.get(c);
if (c == '/')
{
while (c != '\n')
{
file.get(c);
}
}
else
{
obj.t = S_DIV;
obj.a = "Divide";
return obj;
}
}
else if (c == '<')
{
file.get(c);
if (c == '=')
{
obj.t = S_LessEqual;
obj.a = "Less-Equal";
return obj;
}
else
{
file.putback(c);
obj.t = S_LessThen;
obj.a = "Less-Then";
return obj;
}
}
else if (c == '>')
{
file.get(c);
if (c == '=')
{
obj.t = S_GreaterEqual;
obj.a = "Greater-Equal";
return obj;
}
else
{
file.putback(c);
obj.t = S_GreaterThen;
obj.a = "Greater-Then";
return obj;
}
}
else if (c == '/')
{
}
else if (isdigit(c))
{
string str;
while (isdigit(c))
{
str += c;
file.get(c);
}
file.putback(c);
obj.t = S_Number;
obj.a = "DIGIT";
return obj;
}