Mohammad_Dipu_Sultan

Untitled

Apr 22nd, 2022
1,148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.28 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. vector<string> k;
  5. vector<char>id;
  6. vector<char>lo;
  7. vector<char>ma;
  8. vector<string>nu;
  9. vector<char>ot;
  10. int isKeyword(char buffer[])
  11. {
  12.     char keywords[32][10] = {"auto","break","case","char","const","continue","default",
  13.                              "do","double","else","enum","extern","float","for","goto",
  14.                              "if","int","long","register","return","short","signed",
  15.                              "sizeof","static","struct","switch","typedef","union",
  16.                              "unsigned","void","volatile","while"
  17.                             };
  18.     bool f=false;
  19.     for(int i=0; i<32; ++i)
  20.     {
  21.         if(strcmp(keywords[i], buffer)== 0)
  22.         {
  23.             f = true;
  24.             break;
  25.         }
  26.     }
  27.     return f;
  28. }
  29.  
  30. int main()
  31. {
  32.     ifstream fin("input.txt");
  33.     char logical_op[] = ">!^&|<", math_op[]="+-*/=", numer[]=".0123456789", other[]=",;\(){}[]'':";
  34.     char ch, buffer[15],b[30];
  35.     int mark[1000]= {0};
  36.    
  37.     int i,j=0;
  38.     int kc=0,ic=0,lc=0,mc=0,nc=0,oc=0,aaa=0;
  39.    
  40.     while(!fin.eof())
  41.     {
  42.         ch = fin.get();
  43.         for(i = 0; i < 12; ++i)
  44.         {
  45.             if(ch == other[i])
  46.             {
  47.                 int aa=ch;
  48.                
  49.                 if(mark[aa]!=1)
  50.                 {
  51.                     ot.push_back(ch);
  52.                     mark[aa]=1;
  53.                     ++oc;
  54.                 }
  55.             }
  56.         }
  57.  
  58.         for(i = 0; i < 5; ++i)
  59.         {
  60.             if(ch == math_op[i])
  61.             {
  62.                 int aa=ch;
  63.                 if(mark[aa]!=1)
  64.                 {
  65.                     ma.push_back(ch);
  66.                     mark[aa]=1;
  67.                     ++mc;
  68.                 }
  69.             }
  70.         }
  71.        
  72.         for(i = 0; i < 6; ++i)
  73.         {
  74.             if(ch == logical_op[i])
  75.             {
  76.                 int aa=ch;
  77.                 if(mark[aa]!=1)
  78.                 {
  79.                     lo.push_back(ch);
  80.                     mark[aa]=1;
  81.                     ++lc;
  82.                 }
  83.             }
  84.  
  85.         }
  86.        
  87.         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 == ';')
  88.         {
  89.             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;
  90.             if((ch == ' ' || ch == '\n' || ch == ';') && (aaa != 0))
  91.             {
  92.                 b[aaa] = '\0';
  93.                 aaa = 0;
  94.                 char arr[30];
  95.                 strcpy(arr,b);
  96.                 nu.push_back(arr);
  97.                 ++nc;
  98.             }
  99.         }
  100.  
  101.  
  102.         if(isalnum(ch))
  103.         {
  104.             buffer[j++] = ch;
  105.         }
  106.         else if((ch == ' ' || ch == '\n') && (j != 0))
  107.         {
  108.             buffer[j] = '\0';
  109.             j = 0;
  110.  
  111.             if(isKeyword(buffer) == 1)
  112.             {
  113.                 k.push_back(buffer);
  114.                 ++kc;
  115.             }
  116.             else
  117.             {
  118.                 if(buffer[0]>=97 && buffer[0]<=122)
  119.                 {
  120.                     if(mark[buffer[0]-'a']!=1)
  121.                     {
  122.                         id.push_back(buffer[0]);
  123.                         ++ic;
  124.                         mark[buffer[0]-'a']=1;
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.     }
  130.  
  131.     for(i=0; i<81; i++) cout<<"-";
  132.  
  133.     cout<<endl;
  134.  
  135.     cout<<"|";
  136.  
  137.     cout<<"symbol";
  138.  
  139.     for(i=0; i<12; i++) cout<<" ";
  140.  
  141.     cout<<"|";
  142.  
  143.     cout<<"symbol_id";
  144.  
  145.     for(i=0; i<10; i++) cout<<" ";
  146.  
  147.     cout<<"|";
  148.  
  149.     cout<<"token_type";
  150.  
  151.     for(i=0; i<9; i++) cout<<" ";
  152.  
  153.     cout<<"|";
  154.  
  155.     cout<<"value";
  156.  
  157.     for(i=0; i<15; i++) cout<<" ";
  158.  
  159.     cout<<"|\n";
  160.  
  161.     for(i=0; i<81; i++) cout<<"-";
  162.  
  163.     cout<<endl;
  164.  
  165.     fin.close();
  166.  
  167.     j=0;
  168.  
  169.     for(int x=0; x<ic; x++)
  170.     {
  171.         cout<<id[x];
  172.  
  173.         for(i=0; i<19; i++) cout<<" ";
  174.  
  175.         cout<<"id_"<<++j;
  176.  
  177.         for(i=0; i<16; i++) cout<<" ";
  178.  
  179.         cout<<"Identifiers";
  180.  
  181.         for(i=0; i<9; i++) cout<<" ";
  182.  
  183.         cout<<"_";
  184.  
  185.         for(i=0; i<19; i++) cout<<" ";
  186.  
  187.         cout<<endl<<endl;
  188.  
  189.     }
  190.  
  191.     j=0;
  192.  
  193.     for(int x=0; x<mc; x++)
  194.     {
  195.         cout<<ma[x];
  196.  
  197.         for(i=0; i<19; i++) cout<<" ";
  198.  
  199.         cout<<"op"<<"_"<<++j;
  200.  
  201.         for(i=0; i<16; i++) cout<<" ";
  202.  
  203.         cout<<"operator";
  204.  
  205.         for(i=0; i<12; i++) cout<<" ";
  206.  
  207.         cout<<"_";
  208.  
  209.         for(i=0; i<19; i++) cout<<" ";
  210.  
  211.         cout<<endl;
  212.  
  213.         cout<<endl;
  214.     }
  215.  
  216.     for(int x=0; x<lc; x++)
  217.     {
  218.         cout<<lo[x];
  219.  
  220.         for(i=0; i<19; i++) cout<<" ";
  221.  
  222.         cout<<"op"<<"_"<<++j;
  223.  
  224.         for(i=0; i<16; i++) cout<<" ";
  225.  
  226.         cout<<"operator";
  227.  
  228.         for(i=0; i<12; i++) cout<<" ";
  229.  
  230.         cout<<"_";
  231.  
  232.         for(i=0; i<19; i++) cout<<" ";
  233.  
  234.         cout<<endl;
  235.  
  236.         cout<<endl;
  237.     }
  238.  
  239.     j=0;
  240.     for(int x=0; x<nc; x++)
  241.     {
  242.         int g=nu[x].size();
  243.  
  244.         cout<<nu[x];
  245.  
  246.         for(i=0; i<20-g; i++) cout<<" ";
  247.  
  248.         cout<<"num"<<"_"<<++j;
  249.  
  250.         for(i=0; i<15; i++) cout<<" ";
  251.  
  252.         cout<<"number";
  253.  
  254.         for(i=0; i<14; i++) cout<<" ";
  255.  
  256.         cout<<nu[x];
  257.  
  258.         for(i=0; i<20-g; i++) cout<<" ";
  259.  
  260.         cout<<endl;
  261.  
  262.         cout<<endl;
  263.     }
  264.  
  265.     j=0;
  266.  
  267.     int l=0;
  268.  
  269.     for(int x=0; x<oc; ++x)
  270.     {
  271.         if(ot[x]==';')
  272.         {
  273.             cout<<ot[x];
  274.  
  275.             for(i=0; i<19; i++) cout<<" ";
  276.  
  277.             cout<<"semicolon"<<"_"<<++l;
  278.  
  279.             for(i=0; i<9; i++) cout<<" ";
  280.  
  281.             cout<<"semicolon";
  282.  
  283.             for(i=0; i<11; i++) cout<<" ";
  284.  
  285.             cout<<"_";
  286.  
  287.             for(i=0; i<19; i++) cout<<" ";
  288.  
  289.             cout<<endl;
  290.  
  291.             cout<<endl;
  292.         }
  293.         else
  294.         {
  295.             cout<<ot[x];
  296.  
  297.             for(i=0; i<19; i++) cout<<" ";
  298.  
  299.             cout<<"other"<<"_"<<++j;
  300.  
  301.             for(i=0; i<13; i++) cout<<" ";
  302.  
  303.             cout<<"other";
  304.  
  305.             for(i=0; i<15; i++) cout<<" ";
  306.  
  307.             cout<<"_";
  308.  
  309.             for(i=0; i<19; i++) cout<<" ";
  310.  
  311.             cout<<endl;
  312.  
  313.             cout<<endl;
  314.         }
  315.  
  316.     }
  317.     for(i=0; i<81; i++) cout<<"-";
  318.  
  319.     cout<<endl;
  320.  
  321.     return 0;
  322. }
  323.  
Advertisement
Add Comment
Please, Sign In to add comment