Advertisement
Mohammad_Dipu_Sultan

Untitled

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