Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int isKeyword(char buffer[])
- {
- char keywords[32][10] = {"auto","break","case","char","const","continue","default",
- "do","double","else","enum","extern","float","for","goto",
- "if","int","long","register","return","short","signed",
- "sizeof","static","struct","switch","typedef","union",
- "unsigned","void","volatile","while"
- };
- bool f=false;
- for(int i=0; i<32; ++i)
- {
- if(strcmp(keywords[i], buffer)== 0)
- {
- f = true;
- break;
- }
- }
- return f;
- }
- int main()
- {
- ifstream fin("input.txt");
- char logical_op[] = ">!^&|<", math_op[]="+-*/=", numer[]=".0123456789", other[]=",;\(){}[]'':";
- char ch, buffer[15],b[30];
- 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;
- }
- }
- }
- for(i = 0; i < 5; ++i)
- {
- if(ch == math_op[i])
- {
- int aa=ch;
- if(mark[aa]!=1)
- {
- ma.push_back(ch);
- mark[aa]=1;
- ++mc;
- }
- }
- }
- for(i = 0; i < 6; ++i)
- {
- if(ch == logical_op[i])
- {
- int aa=ch;
- if(mark[aa]!=1)
- {
- lo.push_back(ch);
- mark[aa]=1;
- ++lc;
- }
- }
- }
- 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(ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='8' || ch=='9' || ch=='.') b[aaa++]=ch;
- if((ch == ' ' || ch == '\n' || ch == ';') && (aaa != 0))
- {
- b[aaa] = '\0';
- aaa = 0;
- char arr[30];
- strcpy(arr,b);
- nu.push_back(arr);
- ++nc;
- }
- }
- 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
- {
- if(buffer[0]>=97 && buffer[0]<=122)
- {
- if(mark[buffer[0]-'a']!=1)
- {
- id.push_back(buffer[0]);
- ++ic;
- mark[buffer[0]-'a']=1;
- }
- }
- }
- }
- }
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- cout<<"|";
- cout<<"symbol";
- for(i=0; i<14; i++) cout<<" ";
- cout<<"|";
- cout<<"symbol_id";
- for(i=0; i<11; i++) cout<<" ";
- cout<<"|";
- cout<<"token_type";
- for(i=0; i<10; i++) cout<<" ";
- cout<<"|";
- cout<<"value";
- for(i=0; i<15; i++) cout<<" ";
- cout<<"|\n";
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- fin.close();
- j=0;
- for(int f=0; f<ic; ++f)
- {
- cout<<"|"<<id[f];
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|id_"<<++j;
- for(i=0; i<16; i++) cout<<" ";
- cout<<"|Identifiers";
- for(i=0; i<9; i++) cout<<" ";
- cout<<"|"<<"_";
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<endl;
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- }
- j=0;
- for(int f=0; f<mc; ++f)
- {
- cout<<"|"<<ma[f];
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<"op"<<"_"<<++j;
- for(i=0; i<16; i++) cout<<" ";
- cout<<"|operator";
- for(i=0; i<12; i++) cout<<" ";
- cout<<"|"<<"_";
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<endl;
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- }
- for(int f=0; f<lc; ++f)
- {
- cout<<"|"<<lo[f];
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<"op"<<"_"<<++j;
- for(i=0; i<16; i++) cout<<" ";
- cout<<"|operator";
- for(i=0; i<12; i++) cout<<" ";
- cout<<"|"<<"_";
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<endl;
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- }
- j=0;
- for(int f=0; f<nc; ++f)
- {
- int g=nu[f].size();
- cout<<"|"<<nu[f];
- for(i=0; i<20-g; i++) cout<<" ";
- cout<<"|"<<"num"<<"_"<<++j;
- for(i=0; i<15; i++) cout<<" ";
- cout<<"|number";
- for(i=0; i<14; i++) cout<<" ";
- cout<<"|"<<nu[f];
- for(i=0; i<20-g; i++) cout<<" ";
- cout<<"|"<<endl;
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- }
- j=0;
- int l=0;
- for(int f=0; f<oc; ++f)
- {
- if(ot[f]==';')
- {
- cout<<"|"<<ot[f];
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<"semicolon"<<"_"<<++l;
- for(i=0; i<9; i++) cout<<" ";
- cout<<"|semicolon";
- for(i=0; i<11; i++) cout<<" ";
- cout<<"|"<<"_";
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<endl;
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- }
- else
- {
- cout<<"|"<<ot[f];
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<"other"<<"_"<<++j;
- for(i=0; i<13; i++) cout<<" ";
- cout<<"|other";
- for(i=0; i<15; i++) cout<<" ";
- cout<<"|"<<"_";
- for(i=0; i<19; i++) cout<<" ";
- cout<<"|"<<endl;
- for(i=0; i<85; i++) cout<<"-";
- cout<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement