Compiler Lab Assignment-1
Compiler Lab Assignment-1
AIM: Write a C++ Programme to count Identifiers, keywords, Numbers and literals.
SOURCE CODE:
#include<bits/stdc++.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
return flag;
}
int main(){
char ch, buffer[15],b[30], logical_op[] = "><",math_op[]="+-
*/=",numer[]=".0123456789",other[]=",;\(){}[]'':";
ifstream fin("txt.txt");
int mark[1000]={0};
int i,j=0,kc=0,ic=0,lc=0,mc=0,nc=0,oc=0,aaa=0;
vector < string > k;
vector<char >id;
vector<char>lo;
vector<char>ma;
vector<string>nu;
vector<char>ot;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}
while(!fin.eof()){
ch = fin.get();
for(i = 0; i < 12; ++i){
if(ch == other[i]){
int aa=ch;
if(mark[aa]!=1){
ot.push_back(ch);
mark[aa]=1;
++oc;
}
}
}
}
if(ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='8' ||
ch=='9' || ch=='.' ||ch == ' ' || ch == '\n' || ch == ';'){
if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1){
k.push_back(buffer);
++kc;
}
else{
fin.close();
printf("Keywords: ");
for(int f=0;f<kc;++f){
if(f==kc-1){
cout<<k[f]<<"\n";
}
else {
cout<<k[f]<<", ";
}
}
printf("Identifiers: ");
for(int f=0;f<ic;++f){
if(f==ic-1){
cout<<id[f]<<"\n";
}
else {
cout<<id[f]<<", ";
}
}
printf("Math Operators: ");
for(int f=0;f<mc;++f){
if(f==mc-1){
cout<<ma[f]<<"\n";
}
else {
cout<<ma[f]<<", ";
}
}
printf("Logical Operators: ");
for(int f=0;f<lc;++f){
if(f==lc-1){
cout<<lo[f]<<"\n";
}
else {
cout<<lo[f]<<", ";
}
}
printf("Numerical Values: ");
for(int f=0;f<nc;++f){
if(f==nc-1){
cout<<nu[f]<<"\n";
}
else {
cout<<nu[f]<<", ";
}
}
printf("Others: ");
for(int f=0;f<oc;++f){
if(f==oc-1){
cout<<ot[f]<<"\n";
}
else {
cout<<ot[f]<<" ";
}
return 0;
}
INPUT FILE:
OUTPUT: