LAB3CC 06062022 042621pm
LAB3CC 06062022 042621pm
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
string user_Variable[200];
ifstream infile;
int n=0;
tokentype lexical();
tokentype tokn;
void main()
{
infile.open("compiler.txt");
if(infile.is_open())
{
while(!infile.eof())
{
tokn= lexical();
cout << "\nTOKEN TYPE: " << tokn << endl;
}
}
else
{
cout<<"file could not found"<<endl;
}
infile.close();
_getch();
}
// to get alphabets....
else if(isalpha(ch))
{
while(!infile.eof())
{
if(isalnum(ch))
{
s+=ch;
infile.get(ch);
}
else { infile.unget(); break;}
if(ch==' '|| !isalnum(ch))
{
infile.unget();
break;
}
}
// for keyword like do, for...
if(s=="int") return INT;
else if(s=="while") return WHILE;
else if(s=="string") return STRING;
else if(s=="char") return CHAR;
else if(s=="for") return FOR;
else if(s=="float") return FLOAT;
else if(s=="double") return DOUBLE;
else if(s=="while") return WHILE;
else if(s=="do") return DO;
else if(s=="if") return IF;
else if(s=="else") return ELSE;
// for user defined
else return UID;
}
}
}
Question no 2:
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
string user_Variable[200];
ifstream infile;
int n=0;
struct token
{
tokentype tokn;
int entryno;
};
tokentype lexical();
tokentype tokn;
void main()
{
infile.open("compiler.txt");
if(infile.is_open())
{
while(!infile.eof())
{
tokn= lexical();
cout << "\nTOKEN TYPE: " << tokn << endl;
}
}
else
{
cout<<"file could not found"<<endl;
}
infile.close();
_getch();
}
}
//for special character
if(ch=='(') return LPRN;
else if (ch==')') return RPRN;
else if(ch=='[') return LSQBR;
else if(ch==']') return RSQBR;
else if(ch==',') return COM;
else if(ch=='.') return DOT;
else if(ch=='{') return LCRBR;
else if(ch=='}') return RCRBR;
else if(ch==';') return COL;
else if(ch==':') return SEMICOL;
//for + operator
else if(ch=='+')
{
infile.get(ch);
if(ch=='=') return PLUSEQ;
else if(ch=='+') return PLUSPLUS;
else { infile.unget(); return PLUS; }
}
//for - operator
else if(ch=='-')
{
infile.get(ch);
if(ch=='=') return MINUSEQ;
else if(ch=='-') return MINUSMINUS;
else { infile.unget(); return MINUS; }
}
//for * operator
else if(ch=='*')
{
infile.get(ch);
if(ch=='=') return MULTIEQ;
else { infile.unget(); return MULTI; }
}
//for / operator
else if(ch=='/')
{
infile.get(ch);
if(ch=='=') return DIVEQ;
else { infile.unget(); return DIV; }
}
//for < operator
else if(ch=='<')
{
infile.get(ch);
if(ch=='=') return LESSEQ;
else if(ch=='<') return INSERTION;
else { infile.unget(); return LESS; }
}
//for > operator
else if(ch=='>')
{
infile.get(ch);
if(ch=='=') return GREATEREQ;
else if(ch=='>') return DGREATER;
else { infile.unget(); return GREATER; }
}
//for digit like 1,2,3....
else if(isdigit(ch))
{
s+=ch;
//infile.get(ch);
while(isdigit(ch))
{
s+=ch;
infile.get(ch);
}
// to get alphabets....
else if(isalpha(ch))
{
while(!infile.eof())
{
if(isalnum(ch))
{
s+=ch;
infile.get(ch);
}
else { infile.unget(); break;}
if(ch==' '|| !isalnum(ch))
{
infile.unget();
break;
}
}
// for keyword like do, for...
if(s=="int") return INT;
else if(s=="while") return WHILE;
else if(s=="string") return STRING;
else if(s=="char") return CHAR;
else if(s=="for") return FOR;
else if(s=="float") return FLOAT;
else if(s=="double") return DOUBLE;
else if(s=="while") return WHILE;
else if(s=="do") return DO;
else if(s=="if") return IF;
else if(s=="else") return ELSE;
// for user defined
else return UID;
}
}
}
Question no 3 and 4:
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
string user_Variable[200];
ifstream infile;
int n=0;
struct token
{
tokentype tokn;
int entryno;
};
token lexical();
token tok;
token t1[20];
int n1=1;
void main()
{
infile.open("compiler.txt");
if(infile.is_open())
{
while(!infile.eof())
{
tok= lexical();
cout << "\nTOKEN TYPE: " << tok.tokn << endl;
cout << "\nTOKEN NO: " << tok.entryno << endl;
}
}
else
{
cout<<"file could not found"<<endl;
}
infile.close();
cout<<" "<<"\n\nsymbol table" <<endl;
cout<<" Entry No "<< " Token Type " <<endl;
for(int i=0;i<n;i++)
cout<<" "<<t1[i].entryno<<" "<<t1[i].tokn<<endl;
_getch();
}
int INSERT_IN_TABLE(tokentype t , int nn)
{ int r=0;
t1[n1].entryno=n1;
t1[n1].tokn=t;
r=n1;
return r;
}
// lexical function for compiler
token lexical()
{
char ch;
token t1;
string s="";
infile.get(ch);
while(!infile.eof())
{
// for space
if(ch==' ')
{
do
{
infile.get(ch);
}while(isspace(ch));
}
//for special character
if(ch=='(') { t1.tokn=LPRN; t1.entryno=n; n++; return t1;}
else if (ch==')') { t1.tokn=RPRN; t1.entryno=n; n++; return t1; }
else if(ch=='[') { t1.tokn= LSQBR; t1.entryno=n; n++; return t1;
}
else if(ch==']') { t1.tokn= RSQBR; t1.entryno=n; n++; return
t1; }
else if(ch==',') { t1.tokn= COM; t1.entryno=n; n++; return t1; }
else if(ch=='.') { t1.tokn= DOT; t1.entryno=n; n++; return t1; }
else if(ch=='{') { t1.tokn= LCRBR; t1.entryno=n; n++; return
t1; }
else if(ch=='}') { t1.tokn= RCRBR; t1.entryno=n; n++; return t1; }
else if(ch==';') { t1.tokn= COL; t1.entryno=n; n++; return t1; }
else if(ch==':') { t1.tokn= SEMICOL; t1.entryno=n; n++; return
t1; }
//for + operator
else if(ch=='+')
{
infile.get(ch);
if(ch=='=') { t1.tokn= PLUSEQ; t1.entryno=n; n++;
return t1; }
else if(ch=='+') { t1.tokn= PLUSPLUS; t1.entryno=n; n++;
return t1; }
else { infile.unget(); t1.tokn= PLUS; t1.entryno=n;
n++; return t1; }
}
//for - operator
else if(ch=='-')
{
infile.get(ch);
if(ch=='=') { t1.tokn= MINUSEQ; t1.entryno=n; n++;
return t1; }
else if(ch=='-') { t1.tokn= MINUSMINUS;
t1.entryno=n; n++; return t1; }
else { infile.unget(); t1.tokn= MINUS;
t1.entryno=n; n++; return t1; }
}
//for * operator
else if(ch=='*')
{
infile.get(ch);
if(ch=='=') { t1.tokn= MULTIEQ; t1.entryno=n; n++;
return t1; }
else { infile.unget(); t1.tokn= MULTI;
t1.entryno=n; n++; return t1; }
}
//for / operator
else if(ch=='/')
{
infile.get(ch);
if(ch=='=') { t1.tokn= DIVEQ; t1.entryno=n; n++;
return t1; }
else { infile.unget(); t1.tokn= DIV;
t1.entryno=n; n++; return t1; }
}
//for < operator
else if(ch=='<')
{
infile.get(ch);
if(ch=='=') { t1.tokn= LESSEQ; t1.entryno=n; n++;
return t1; }
else if(ch=='<') { t1.tokn= INSERTION;
t1.entryno=n; n++; return t1; }
else { infile.unget(); t1.tokn= LESS;
t1.entryno=n; n++; return t1; }
}
//for > operator
else if(ch=='>')
{
infile.get(ch);
if(ch=='=') { t1.tokn= GREATEREQ; t1.entryno=n; n++;
return t1; }
else if(ch=='>') { t1.tokn= DGREATER;
t1.entryno=n; n++; return t1; }
else { infile.unget(); t1.tokn= GREATER;
t1.entryno=n; n++; return t1; }
}
//for digit like 1,2,3....
else if(isdigit(ch))
{
s+=ch;
//infile.get(ch);
while(isdigit(ch))
{
s+=ch;
infile.get(ch);
}
// to get alphabets....
else if(isalpha(ch))
{
while(!infile.eof())
{
if(isalnum(ch))
{
s+=ch;
infile.get(ch);
}
else { infile.unget(); break;}
if(ch==' '||((int) ch < 97 || (int) ch > 122))
{
infile.unget();
break;
}
}
// for keyword like do, for...
if(s=="int") { t1.tokn= INT; t1.entryno=0; n++;
return t1; }
else if(s=="while") { t1.tokn= WHILE; t1.entryno=0; n++;
return t1; }
else if(s=="string"){ t1.tokn= STRING; t1.entryno=0; n++;
return t1; }
else if(s=="char") { t1.tokn= CHAR; t1.entryno=0; n++;
return t1; }
else if(s=="for") { t1.tokn= FOR; t1.entryno=0; n++;
return t1; }
else if(s=="float") { t1.tokn=FLOAT; t1.entryno=0; n++;
return t1; }
else if(s=="double"){ t1.tokn= DOUBLE; t1.entryno=0; n++;
return t1; }
else if(s=="while") { t1.tokn= WHILE; t1.entryno=0; n++;
return t1; }
else if(s=="do") { t1.tokn= DO; t1.entryno=0; n++;
return t1; }
else if(s=="if") { t1.tokn= IF; t1.entryno=0; n++;
return t1; }
else if(s=="else") { t1.tokn= ELSE; t1.entryno=0; n++;
return t1; }
// for user defined
else
{
}
}
return t1;
}