0% found this document useful (0 votes)
10 views15 pages

LAB9CC 06062022 042722pm

solved journal of compiler construction

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

LAB9CC 06062022 042722pm

solved journal of compiler construction

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MARYAM BIBI

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,LPRN,RPRN,OPR,UID,FLOAT,NUM,MINUS,LCRBR,RCRBR,MINUSEQUAL,
DIV,BEGIN,END,LSQRBR,
RSQRBR,PLUSPLUS,PLUS,PLUSEQ,EQEQ,EQUAL,SEMICLN,CLN,COMMA,COMMENT,MINUSMINUS,LESS,
MULTI,LESSEQ,GREATER,GREATEREQ,
INT,STRING,CHAR,DOUBLE}tokentype;
typedef enum {opk,constk,idk} nodekind;

struct token
{
tokentype tkn;
string entryno;
int num;
string variable;
};
string innt[20];
//................................................................
struct keyword_insert
{
string name;
int num;
};
keyword_insert tok;
//..........................tree structure..................................
struct treenode
{
nodekind kind;
int value;
char *name;
tokentype opr;
treenode* leftchild;
treenode* rightchild;
};

treenode* expression();
treenode* term();
treenode* factor();
int evaluate(treenode* temp);
//.............................................................
bool program();
bool statement_list();
bool statement();
bool expr();
//bool expression();
//bool term();
//bool factor();
//.............................................................
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) <<"VALUE "<<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) <<" VALUE
*"<<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++;
}

// start lexical function


token tokn;
token lexical()
{
char ch;
string st="";

while (!infile.eof())
{
infile.get(ch);
if(ch=='(')
{
tokn.tkn=LPRN; tokn.entryno="LPRN"; return tokn;
}
else if (ch==')')
{
tokn.tkn=RPRN; tokn.entryno="RPRN"; return tokn;
}
else if(ch=='[')
{
tokn.tkn=LSQRBR; tokn.entryno="LSQRBR"; return tokn;
}
else if(ch==']')
{
tokn.tkn=RSQRBR; tokn.entryno="RSQRBR"; return tokn;
}
else if(ch=='{')
{
tokn.tkn=LCRBR; tokn.entryno="LCRBR"; return tokn;
}
else if(ch=='}')
{
tokn.tkn=RCRBR; tokn.entryno="RCRBR"; return tokn;
}
else if(ch==';')
{
tokn.tkn=SEMICLN; tokn.entryno="SEMICLN"; return tokn;
}
else if(ch==':')
{
tokn.tkn=CLN; tokn.entryno="CLN"; return tokn;
}
else if(ch==',')
{
tokn.tkn=COMMA; tokn.entryno="COMMA"; return tokn;
}
else if( ch=='/')
{
infile.get(ch);
if (ch == '/')
{
cout << "Single line Comment Found" << endl;
do{
infile.get(ch);
} while (ch != '\n');
}
if (ch == '*')
{
cout << "MultiLine Comment Found" << endl;
while (1)
{
infile.get(ch);
if (ch == '*')
{
infile.get(ch);
if (ch == '/')
{
break;
}
}
}
}
else
tokn.tkn=DIV; tokn.entryno="DIV"; return tokn;
}
else if(ch=='*')
{
if( ch=='/')
{
tokn.tkn=COMMENT; tokn.entryno="COMMENT"; return tokn;
}
else
{
tokn.tkn=MULTI; tokn.entryno="MULTI"; 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=='=')
{
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=='*')
{
tokn.tkn=MULTI; tokn.entryno="MULTI"; 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="";
do
{
b+=ch;
infile.get(ch);
}while(isdigit(ch));
infile.unget();
keyword_insert_in_table(b,ki);
tok.name=b; tok.num;
tokn.tkn=NUM; tokn.entryno="NULL"; return tokn;
}
else if ( (int (ch) >=65 && (int) ch<= 90) || ( (int) ch>=97 && (int)
ch<=122) )
//else if (isalpha(ch))
{
//st +=ch;
//
//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(ch==' '||((int) ch< 97 || (int) ch> 122))
// {
// infile.unget();
// break;
// }
//
// }
st="";
do
{
st+=ch;
infile.get(ch);
}while (isalnum(ch) && st!="end");
if (st != "end")
{
infile.unget();
}
char v;
if(st =="int")
{
string b="";
do
{
infile.get(ch);
v=ch;
}while(v!='=');
infile.get(ch);
do
{
b+=ch;
infile.get(ch);
}while(!isdigit);

keyword_insert_in_table(b,ki);

tokn.tkn=INT; tokn.entryno="NULL"; return tokn;


}
else if(st=="string")
{
string b="";
string v1;
do
{
infile.get(ch);
v1=ch;
}while(v1!="=");
infile.get(ch);
string x;
infile.get(ch);
do
{
b+=ch;
infile.get(ch);
x=ch;

}while(x!="'");
keyword1_insert_in_table(b,ki2);

tokn.tkn=STRING; tokn.entryno="NULL"; return tokn;


}
else if(st=="char")
{
tokn.tkn=CHAR; tokn.entryno="NULL"; return tokn;
}
else if(st=="for")
{
tokn.tkn=FOR; tokn.entryno="NULL"; return tokn;
}
else if(st=="while")
{
tokn.tkn=WHILE; tokn.entryno="NULL"; return tokn;
}
else if(st=="begin")
{
tokn.tkn=BEGIN; tokn.entryno="NULL"; return
tokn;
}
else if(st=="end")
{
tokn.tkn=END; tokn.entryno="NULL"; return tokn;
}
else if(st=="do")
{
tokn.tkn=DO; tokn.entryno="NULL"; return tokn;
}
else if(st=="if")
{
tokn.tkn=IF; tokn.entryno="NULL"; return
tokn;
}
else if(st=="else")
{
tokn.tkn=ELSE; tokn.entryno="NULL"; return
tokn;
}
else if(st=="float")
{
tokn.tkn=FLOAT; tokn.entryno="NULL"; return
tokn;
}
else if(st=="double")
{
tokn.tkn=DOUBLE; tokn.entryno="NULL"; return
tokn;
}
else
{
cout<<" New Entry of UID: "<<
search(st)<<endl;
tokn.tkn=UID; tokn.entryno="NULL"; return
tokn;

}
}
}
return tokn;
}

int search(string st)


{
bool flag=true;
int i=0;
for(i=0;i<=symbol;i++)
{
if(st==t1[i].variable)
{
cout<<" Variable
already at UID: "<<i<<endl;
return t1[i].num;
break;
}
}
if(flag)
{
tokn.tkn=UID;
tokn.entryno="variable";
tokn.variable=st;
s[number]=st;
insert_in_table(number,tokn.tkn,st);
}
return number++;
}

//........ get or unget.......................


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;
}
}
//................................
bool program()
{

tokn=gettoken();
if(tokn.tkn==BEGIN)
{
if(statement_list())

{
tokn=gettoken();
if(tokn.tkn==END)
return true;
}
}
return false;

bool statement_list() // statement list


{

if(statement())
{
do
{
tokn=gettoken();
if(tokn.tkn==IF||tokn.tkn==DO||tokn.tkn==UID)
{
ungettoken();
//if(!statement())
return false;
}
else
{
ungettoken();
break;
}

}while(1);
return true; // if , not, uid not equal then may be else
}
return false; //otherwise false

bool statement()
{
treenode *t1;
tokn=gettoken();
if(tokn.tkn==UID)
{
tokn=gettoken();
if(tokn.tkn==EQUAL)
{
if(t1=expression())
{
cout<<evaluate(t1)<<endl;
tokn=gettoken();
if(tokn.tkn==SEMICLN)
return true;

}
}
}
//else if(tokn.tkn==IF)
//{
// tokn=gettoken();
// if(tokn.tkn==LPRN)
// {
// if(expr())
// {
// tokn=gettoken();
// if(tokn.tkn==RPRN)
// {
// tokn=gettoken();
// if(tokn.tkn==LCRBR)
// {
// if(statement_list())
// {
// tokn=gettoken();
// if(tokn.tkn==RCRBR)
// {
// tokn=gettoken();
// if(tokn.tkn==ELSE)
// {
// tokn=gettoken();
// if(tokn.tkn==LCRBR)
// {
//
if(statement_list())
// {
//
tokn=gettoken();
//
if(tokn.tkn==RCRBR)
// return
true;
// }

// }
// }
// }
// }
// }
// }
// }
// }
//}
//else if(tokn.tkn==DO)
//{
// tokn=gettoken();
// if(tokn.tkn==LCRBR)
// {
// if(statement_list())
// {
// tokn=gettoken();
// if(tokn.tkn==RCRBR)
// {
// tokn=gettoken();
// if(tokn.tkn==WHILE)
// {
// tokn=gettoken();
// if(tokn.tkn==LPRN)

// {
// if(expr())
// {
// tokn=gettoken();
// if(tokn.tkn==RPRN)
// {
// tokn=gettoken();
// if(tokn.tkn==SEMICLN)
// return true;
// }
// }
// }
// }
// }
// }
// }
//}
return false;
}

treenode* expression()
{
treenode *t,*p;
int flage=0;
do
{
t=term();
if(flage)
{
p->rightchild=t;
flage=0;
t=p;
}
tokn=gettoken();
if(tokn.tkn==PLUS || tokn.tkn==MINUS)
{
p=new treenode;
p->kind=opk;
p->opr=tokn.tkn;
p->leftchild=t;
flage=1;
}
}
while(tokn.tkn==PLUS ||tokn.tkn==MINUS );
ungettoken();
return (t);
}

treenode* term()
{
treenode *t,*p;
int flage=0;
do
{
t=factor();
if(flage)
{
p->rightchild=t;
flage=0;
t=p;
}
tokn=gettoken();
if(tokn.tkn==MULTI || tokn.tkn==DIV)
{
p=new treenode;
p->kind=opk;
p->opr=tokn.tkn;
p->leftchild=t;
flage=1;
}
}while(tokn.tkn==MULTI || tokn.tkn==DIV);
ungettoken();
return (t);

treenode* factor()
{
treenode *t;
tokn=gettoken();
switch(tokn.tkn)
{
case NUM:
t=new treenode();
t->kind=constk;
t->value=stoi(tok.name);
t->name=NULL;
//t->opr=NUL;
t->leftchild=NULL;
t->rightchild=NULL;
return(t);
break;
case UID:
t=new treenode();
t->kind=idk;
//t->value=atoi(tok.name);
t->name=NULL;
//t->opr=NUL;
t->leftchild=NULL;
t->rightchild=NULL;
return(t);
break;
case LPRN:
return(t);
break;
}
}

bool expr()// expresion checked


{

if(expression())
{
tokn=gettoken();
if(tokn.tkn==GREATER||tokn.tkn==LESS||tokn.tkn==GREATEREQ||
tokn.tkn==LESSEQ||tokn.tkn==EQEQ)
{
if(expr())
return true;
else
return false;
}
ungettoken();
return true;
}
return false;
}
//..............display..................
int evaluate(treenode* temp)
{
int v1,v2;
if(temp!=NULL)
{
if(temp->kind==opk)
{
v1=evaluate(temp->leftchild);
v2=evaluate(temp->rightchild);
}
else
{
return (temp->value);
}

switch(temp->opr)
{
case PLUS:
v1=v1+v2;
return v1;
break;
case MINUS:
v1=v1-v2;
return v1;
break;
case MULTI:
v1=v1*v2;
return v1;
break;
case DIV:
v1=v1/v2;
return v1;
break;
}
}
}

You might also like