Professional Documents
Culture Documents
Vaiva Mam Sol
Vaiva Mam Sol
01-134122-045
BS(CS) 6(B)
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<Ctype.h>
#include<string>
#include<iomanip>
using namespace std;
typedef enum
{IF=1,ELSE,WHILE,DO,FOR,NUM,LPRN,RPRN,OPR,UID,FLOAT,LCRBR,RCRBR,LSQRBR,MINUS,MULTI,MINUSM
INUS,MINUSEQUAL,
RSQRBR,PLUSPLUS,PLUS,PLUSEQ,EQEQ,EQUAL,SEMICLN,CLN,COMMA,COMMENT,BEGIN,DIV,LESS,LESSEQ,EN
D,GREATER,GREATEREQ,
INT,STRING,CHAR,DOUBLE}tokentype;
struct token
{
tokentype tkn;
string entryno;
int num;
string variable;
};
string innt[20];
int search(string st);
//................................................................
bool program();
bool statement_list();
int expr();
int factor();
int term();
bool statement();
bool comparison();
int assignment();
//................................................................
struct keyword_insert
{
string name;
int num;
};
int ki = 1;
int ki2 = 1;
keyword_insert k_i1[20];
keyword_insert k_i2[20];
//...............................................................
string s[200];
token lexical();
token t;
token t1[30];
ifstream infile;
int symbol=0; // use for symbol table variable
int number=0;
// main function
void main()
{
infile.open("compiler.txt");
cout<<"\n"<<setw(45)<<"****************** "<<endl;
cout<<setw(45)<<"TOKENS OF THE FILE "<<endl;
cout<<setw(46)<<"****************** \n"<<endl;
cout<<endl;
if (infile.is_open())
{
if(program())
cout<<"Successful "<<endl;
else
cout<<"UNSuccesful"<<endl;
}
/*token t;
while(!infile.eof())
{
t=lexical();
cout<<setw(30)<<"TOKEN : "<<t.tkn<<setw(8);
cout<<" ENTRY NO: "<<t.entryno<<endl;
cout<<endl;
}
}
else
{
cout<<"file not found"<<endl;
}
cout<<endl;
cout<<setw(43)<<"****************"<<endl;
cout<<setw(43)<<"* SYMBOL TABLE *"<<endl;
cout<<setw(43)<<"****************"<<endl;
cout<<endl;
cout<<setw(20);
cout<<" ***********************************************"<<endl;
cout<<" * ENTRY NO."<<setw(10) <<"TOKEN "<<setw(25)<<"USER DEFINED VARIABLE
*"<<endl;
cout<<" ***********************************************"<<endl;
for(int i=0;i<symbol;i++)
{
cout<<setw(19)<< t1[i].num<<setw(11)<< t1[i].tkn<<setw(15)<<
t1[i].variable<<endl;
}
cout<<" ***********************************************"<<endl;
cout<<endl;
cout<<setw(43)<<"****************"<<endl;
cout<<setw(43)<<" * INTEGER TABLE *"<<endl;
cout<<setw(43)<<"****************"<<endl;
cout<<endl;
cout<<setw(20);
cout<<" ***********************************************"<<endl;
cout<<" * ENTRY NO."<<setw(10) <<"TOKEN "<<setw(25)<<" *"<<endl;
cout<<" ***********************************************"<<endl;
for(int i=1;i<ki;i++)
{
cout<<setw(19)<< k_i1[i].num<<setw(11)<< k_i1[i].name<<setw(15)<<endl;
}
cout<<" ***********************************************"<<endl;
cout<<endl;
cout<<setw(43)<<"****************"<<endl;
cout<<setw(43)<<"* STRING TABLE *"<<endl;
cout<<setw(43)<<"****************"<<endl;
cout<<endl;
cout<<setw(20);
cout<<" ***********************************************"<<endl;
cout<<" * ENTRY NO."<<setw(10) <<"TOKEN "<<setw(25)<<"USER DEFINED VARIABLE
*"<<endl;
cout<<" ***********************************************"<<endl;
for(int i=1;i<ki2;i++)
{
cout<<setw(19)<< k_i2[i].num<<setw(11)<< k_i2[i].name<<setw(15)<<endl;
}
cout<<" ***********************************************"<<endl;
cout<<endl;*/
_getch();
}
// end main
int search(string st);
void insert_in_table(int number,tokentype tkn,string s)
{
t1[symbol].entryno="variable";
t1[symbol].num=number;
t1[symbol].tkn=tkn;
t1[symbol].variable=s;
symbol++;
}
// insert keyword in table
void keyword_insert_in_table(string s, int n)
{
k_i1[ki].name=s;
k_i1[ki].num=n;
ki++;
}
//insert string
void keyword1_insert_in_table(string s, int n)
{
k_i2[ki2].name=s;
k_i2[ki2].num=n;
ki2++;
}
}while(ch!=(int)13);
cout<<"Comment Found "<<"["<<st<<"]\n";
st="";
}
}*/
else if( ch=='/')
{
/*string x="";
x+=ch;
infile.get(ch);
if(ch=='/')
{
do
{
x=+ch;
infile.get(ch);
}while(ch!='\n');
}*/
//cout<<"\n"<<x<<endl;
if(ch=='/')
{
tokn.tkn=COMMENT; tokn.entryno="COMMENT"; return tokn;
}
else if(ch=='*')
{
tokn.tkn=COMMENT; tokn.entryno="COMMENT"; return tokn;
}
else
tokn.tkn=DIV; tokn.entryno="DIV"; return tokn;
}
else if(ch=='*')
{
if( ch=='/')
{
tokn.tkn=COMMENT; tokn.entryno="COMMENT"; return tokn;
}
}
else if(ch=='+')
{
infile.get(ch);
if(ch=='=')
{
tokn.tkn=PLUSEQ; tokn.entryno="PLUSEQ"; return
tokn;
}
else if(ch=='+')
{
tokn.tkn=PLUSPLUS; tokn.entryno="PLUSPLUS"; return
tokn;
}
else
{
infile.unget();
tokn.tkn=PLUS; tokn.entryno="PLUS"; return
tokn;
}
}
else if(ch=='-')
{
infile.get(ch);
if(ch=='=')
{
tokn.tkn=MINUSEQUAL; tokn.entryno="MINUSEQUAL";
return tokn;
}
else if(ch=='-')
{
tokn.tkn=MINUSMINUS; tokn.entryno="MINUSMINUS";
return tokn;
}
else
{
infile.unget();
tokn.tkn=MINUS; tokn.entryno="MINUS";
return tokn;
}
}
else if(ch=='*')
{
tokn.tkn=MULTI; tokn.entryno="MULTI"; return tokn;
}
else if(ch=='=')
{
infile.get(ch);
if(ch=='=')
{
tokn.tkn=EQEQ; tokn.entryno="EQEQ"; return
tokn;
}
else
{
infile.unget();
tokn.tkn=EQUAL; tokn.entryno="EQAUL"; return
tokn;
}
}
else if(ch=='<')
{
infile.get(ch);
if(ch=='=')
{
tokn.tkn=LESSEQ; tokn.entryno="LESSEQ"; return
tokn;
}
else
{
infile.unget();
tokn.tkn=EQUAL; tokn.entryno="EQUAL"; return
tokn;
}
}
else if(ch=='>')
{
infile.get(ch);
if(ch=='=')
{
tokn.tkn=GREATEREQ; tokn.entryno="GREATEREQ"; return
tokn;
}
else
{
infile.unget();
tokn.tkn=GREATER; tokn.entryno="GREATER"; return
tokn;
}
}
else if((int) ch >=48 && (int) ch <=57)
{
string b="";
////infile.get(ch);
do
{
b+=ch;
infile.get(ch);
}while((int) ch >=48 && (int) ch <=57);
infile.get(ch);
while (!infile.eof())
{
if((int (ch) >=65 && (int) ch<= 90) || ( (int) ch>=97 &&
(int) ch<=122))
//if(isalpha(ch))
{
st+=ch;
}
else { infile.unget(); break;}
infile.get(ch);
}
if(st =="int")
{
string b="";
do
{
infile.get(ch);
}while(infile.get(ch)=="=");
infile.get(ch);
infile.get(ch);
do
{
b+=ch;
infile.get(ch);
}while(!isdigit);
keyword_insert_in_table(b,ki);
}while(b=="'");
//infile.unget();
keyword1_insert_in_table(b,ki2);
}
}
}
return tokn;
}
token tooken;
token current_token;
//..........................................
bool flag=0;
void ungettoken()
{
flag=1;
}
token gettoken()
{
if(flag==0)
{
current_token=lexical();
return current_token;
}
else
{
flag=0;
return current_token;
}
}
//........PROGRAM.............................
bool result;
bool program()
{
tooken=gettoken();
if(tooken.tkn==BEGIN)
{
if(statement_list())
{
tooken=gettoken();
if(tooken.tkn==END)
return true;
}
}
return false;
tooken=gettoken();
if(tooken.tkn==IF||tooken.tkn==DO||tooken.tkn==UID)
{
ungettoken();
if(!statement())
return false;
}
else
{
ungettoken();
break;
}while(true);
return true;
}
return false;
}
//.................statement function............
bool statement()
{
tooken=gettoken();
if(tooken.tkn==UID)
{
tooken=gettoken();
if(tooken.tkn==EQUAL)
{
if(expr())
{
tooken=gettoken();
if(tooken.tkn==CLN)
return true;
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else if(tooken.tkn==IF)
{
tooken=gettoken();
if(tooken.tkn==LPRN)
{
if(comparison())
{
tooken=gettoken();
if(tooken.tkn==RPRN)
{
tooken=gettoken();
if(tooken.tkn==LCRBR)
{
if(statement_list())
{
tooken=gettoken();
if(tooken.tkn==RCRBR)
{
tooken=gettoken();
if(tooken.tkn==ELSE)
{
tooken=gettoken();
if(tooken.tkn==LCRBR)
{
if(statement_list())
{
tooken=gettoken();
if(tooken.tkn==RCRBR)
return
true;
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
}
}
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else if(tooken.tkn==DO)
{
tooken=lexical();
if(tooken.tkn==LCRBR)
{
if(statement_list())
{
tooken=lexical();
if(tooken.tkn==RCRBR)
{
tooken=lexical();
if(tooken.tkn==WHILE)
{
tooken=lexical();
if(tooken.tkn==LPRN)
{
if(expr())
{
tooken=lexical();
if(tooken.tkn==RPRN)
return true;
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
else
ungettoken();
}
ungettoken();
}
ungettoken();
return false;
}
//..............exp().................
int expr()
{
do
{
if(!term())
return false;
tooken=gettoken();
}while(tooken.tkn==PLUS||tooken.tkn==MINUS);
ungettoken();
term();
return true;
}
//.........term...............
int term()
{
do
{
if(!factor())
return false;
tooken=gettoken();
}while(tooken.tkn==MULTI||tooken.tkn==DIV);
ungettoken();
term();
return true;
}
char inbuf[100];
//..............factor.........
int factor()
{
tooken=gettoken();
if(tooken.tkn==UID)
return true;
else if(tooken.tkn==NUM)
return true;
else if(tooken.tkn==LPRN)
if(expr())
{
tooken=gettoken();
if(tooken.tkn==RPRN)
return true;
else
ungettoken();
}
else
ungettoken();
ungettoken();
return false;
}
//.................. COMPARISION,,,,,,,,,,,,,,,
bool comparison()
{
if(factor())
{
tooken=gettoken();
if(tooken.tkn==GREATER||tooken.tkn==LESS||tooken.tkn==GREATEREQ||
tooken.tkn==LESSEQ||tooken.tkn==EQEQ)
if(factor())
return true;
else
return false;
return true;
}
return false;
Output: