Advertisement
Babul_420

dd

Jan 28th, 2022
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. set <string > veriable;
  7. set <string > keyword;
  8. set<string > oparator;
  9. set<string > integer;
  10. set<string> special;
  11. set< string > real_number;
  12. set< string > un_var;
  13.  
  14.  
  15. void demo_intput(){
  16.  
  17.    cout<<"Demo input\n---x---\n\n#include <stdio.h> \n#define max 100000000\nint a , b =1000;\nfloat d = 222.3444;\nint c;\nc=a+b;\n";
  18.  
  19.    cout<<"\n\nHere don't consider header file and #define  just take variable and it's value from #define\n\n";
  20.  
  21. }
  22.  
  23.  
  24. int valid =1;
  25. /// some header file i just implements
  26. char stdio[]="stdio.h";
  27. char strig[]="string.h";
  28. char algo[]="algorithm";
  29. char math[]="math.h";
  30. char stdlib[]="stdlib.h";
  31. char conio[]="conio.h";
  32. char ctyp[]="ctype.h";
  33. char inc[]="include";
  34. char defin[] ="define";
  35.  
  36. /// Include Header File
  37.  
  38. void inclu(char str[],int n)
  39. {
  40.     int i=0,j;
  41.     while(str[i]==' ')
  42.     {
  43.         i++;
  44.     }
  45.     i++;
  46.     while(str[i]==' ')
  47.     {
  48.         i++;
  49.     }
  50.     char st[10];
  51.     for(j=0; j<7; j++)
  52.     {
  53.         st[j]=str[i++];
  54.     }
  55.     st[j]='\0';
  56.  
  57.     if(strcmp(st,inc)!=0)
  58.     {
  59.         printf("Error!!!   include expected at %d line\n\n",n);
  60.         valid =0;
  61.         return ;
  62.     }
  63.     while(str[i]==' ')
  64.     {
  65.         i++;
  66.     }
  67.     if(str[i]!='<')
  68.     {
  69.         printf("Error Error!!!  ' < ' expected at %d line\n\n",n);
  70.         valid =0;
  71.         return ;
  72.  
  73.     }
  74.     i++;
  75.     while(str[i]==' ')
  76.     {
  77.         i++;
  78.     }
  79.     int c=0;
  80.     for(j=0; ; j++)
  81.     {
  82.         if(str[i]=='.')
  83.             c++;
  84.         if(str[i]==' ' || str[i]=='>' || c>=2)
  85.             break;
  86.         st[j]=str[i++];
  87.     }
  88.     st[j]='\0';
  89.     if((strcmp(st,stdio)!=0) &&  (strcmp(st,strig)!=0) && (strcmp(st,algo)!=0) && (strcmp(st,math)!=0) && (strcmp(st,stdio)!=0) && (strcmp(st,stdlib)!=0))
  90.     {
  91.         printf("Error!!!  valid header file expected at %d line\n\n",n);
  92.         valid =0;
  93.         return ;
  94.  
  95.     }
  96.     while(str[i]==' ')
  97.     {
  98.         i++;
  99.     }
  100.  
  101.     if(str[i]=='>')
  102.     {
  103.  
  104.     }
  105.     else
  106.     {
  107.         printf("Error!!!  ' > ' expected at %d line\n\n",n);
  108.         valid =0;
  109.         return ;
  110.  
  111.     }
  112.     i++;
  113.     while(str[i]==' ')
  114.     {
  115.         i++;
  116.     }
  117.     if(str[i]==';')
  118.     {
  119.         printf("syntax error  in %d line\n\n",n);
  120.         valid =0;
  121.         return ;
  122.  
  123.     }
  124. }
  125.  
  126. /// #define function
  127.  
  128. char ss[1000];
  129. void def(char str[],int n)
  130. {
  131.     int i=0,j=0;
  132.     while(str[i]==' ')
  133.     {
  134.         i++;
  135.     }
  136.     i++;
  137.     while(str[i]==' ')
  138.     {
  139.         i++;
  140.     }
  141.     char st[100];
  142.     for(j=0; j<6; j++)
  143.     {
  144.         st[j]=str[i++];
  145.     }
  146.     st[j]='\0';
  147.     if(strcmp(st,defin)!=0)
  148.     {
  149.         printf("syntax error in %d line \n",n);
  150.         valid =0;
  151.         return ;
  152.  
  153.     }
  154.     while(str[i]==' ')
  155.     {
  156.         i++;
  157.     }
  158.     int ln = strlen(str);
  159.     for(j=0; str[i]!=' ' ; j++)
  160.     {
  161.         ss[j]=str[i++];
  162.     }
  163.     ss[j]='\0';
  164.     while(str[i]==' ')
  165.     {
  166.         i++;
  167.     }
  168.     int jj=0;
  169.     int num=0;
  170.     char nu[100];
  171.     for(jj=0; str[i]!=' ' && str[i]!=';' && str[i]!='\0'; jj++)
  172.     {
  173.         nu[jj]=str[i++];
  174.     }
  175.     nu[jj]='\0';
  176.     for(jj=0; nu[jj]!='\0'; jj++)
  177.     {
  178.         if(nu[jj]>='0' &&  nu[jj]<='9') {}
  179.         else
  180.         {
  181.             printf("there is not define number in %d line \n",n);
  182.             valid =0;
  183.             return ;
  184.  
  185.         }
  186.     }
  187.     while(str[i]==' ')
  188.     {
  189.         i++;
  190.     }
  191.     if( str[i]==';')
  192.     {
  193.         printf("Syntax error at %d line \n",n);
  194.         valid =0;
  195.         return ;
  196.  
  197.     }
  198.     printf("\n\n\nvalue of %s = ",ss);
  199.     veriable.insert(ss);
  200.     printf("%s\n",nu);
  201.     integer.insert(nu);
  202. }
  203.  
  204. /// Check for keyword
  205.  
  206. int keyword_( string str)
  207. {
  208.     if (str == "if"  || str == "else" ||
  209.             str == "while" ||  str == "do"  ||
  210.             str == "break"  ||
  211.             str == "continue" || str == "int"
  212.             ||  str == "double" ||  (str == "float")
  213.             ||  (str == "return") ||  (str == "char")
  214.             ||  (str == "case") ||  (str == "char")
  215.             ||  (str == "sizeof") ||  (str == "long")
  216.             ||  (str == "short") ||  (str == "typedef")
  217.             ||  (str == "switch") ||  (str == "unsigned")
  218.             ||  (str == "void") ||  (str == "static")
  219.             ||  (str == "struct") ||  (str == "goto"))
  220.         return 1;
  221.     return 0;
  222. }
  223.  
  224. /// # missing check
  225.  
  226. bool isvalid(char str[] )
  227. {
  228.     int i=0;
  229.     string st;
  230.     st.clear();
  231.     while(str[i]==' ')
  232.         i++;
  233.     for(int j=i; str[j]>='a' && str[j]<='z' ; j++)
  234.         st+= str[j];
  235.  
  236.     if( st== "include" || st =="define")
  237.         return 0;
  238.     else
  239.         return 1;
  240. }
  241.  
  242. /// check Special Character
  243.  
  244. int special_char(char ch)
  245. {
  246.     if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '=' ||
  247.             ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
  248.             ch == '<' ||   ch == '(' || ch == ')' ||
  249.             ch == '[' || ch == ']' || ch == '{' || ch == '}')
  250.         return 1;
  251.     return 0;
  252. }
  253.  
  254. /// check integer number
  255.  
  256. int integer_( string str)
  257. {
  258.     int i, len = str.length();
  259.  
  260.     if (len == 0)
  261.         return 0;
  262.     for (i = 0; i < len; i++)
  263.     {
  264.         if (str[i] != '0' && str[i] != '1' && str[i] != '2'
  265.                 && str[i] != '3' && str[i] != '4' && str[i] != '5'
  266.                 && str[i] != '6' && str[i] != '7' && str[i] != '8'
  267.                 && str[i] != '9' || (str[i] == '-' && i > 0))
  268.             return 0;
  269.     }
  270.     return 1;
  271.  
  272. }
  273.  
  274. /// check floating number
  275.  
  276. int real_number_(string str)
  277. {
  278.     int i, len = str.length();
  279.     bool flag = 0;
  280.     if (len == 0)
  281.         return 0;
  282.     for (i = 0; i < len; i++)
  283.     {
  284.         if (str[i] != '0' && str[i] != '1' && str[i] != '2'
  285.                 && str[i] != '3' && str[i] != '4' && str[i] != '5'
  286.                 && str[i] != '6' && str[i] != '7' && str[i] != '8'
  287.                 && str[i] != '9' && str[i] != '.' || (str[i] == '-' && i > 0))
  288.             return 0;
  289.         if (str[i] == '.')
  290.             flag = 1;
  291.     }
  292.     return flag;
  293.  
  294. }
  295.  
  296.  
  297. /// check operator
  298.  
  299. int oparator_(char ch)
  300. {
  301.     if (ch == '+' || ch == '-' || ch == '*' ||
  302.             ch == '/' || ch == '>' || ch == '<' ||
  303.             ch == '=')
  304.         return 1;
  305.     return 0;
  306.  
  307. }
  308.  
  309. /// check ' ; ' at last of a statement
  310.  
  311. int syntaxanalysis(string str)
  312. {
  313.     int i= str.length();
  314.     i--;
  315.     while(str[i]==' ')
  316.         i--;
  317.     if(str[i]==';')
  318.         return 1;
  319.     else
  320.         return 0;
  321.  
  322. }
  323.  
  324. /// check variable
  325.  
  326. int var(string str)
  327. {
  328.  
  329.     if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
  330.             str[0] == '3' || str[0] == '4' || str[0] == '5' ||
  331.             str[0] == '6' || str[0] == '7' || str[0] == '8' ||
  332.             str[0] == '9' || special_char(str[0]) == 1)
  333.         return 0;
  334.     return 1;
  335.  
  336. }
  337.  
  338. /// Execute all statement
  339.  
  340. void valid_stament( char str[], int n)
  341. {
  342.     if(!syntaxanalysis(str))
  343.     {
  344.         printf("syntax error in %d line \n",n);
  345.         valid =0;
  346.         return ;
  347.  
  348.     }
  349.     stack <string> stck;
  350.     string st;
  351.     string sp;
  352.     int i=0;
  353.     while(str[i]==' ')
  354.         i++;
  355.     for( ; i<strlen(str); i++)
  356.     {
  357.         if(str[i]!=' ')
  358.         {
  359.             if(!special_char(str[i]))
  360.                 st+=str[i];
  361.             else
  362.             {
  363.                 sp+= str[i];
  364.                 stck.push(sp);
  365.                 stck.push(st);
  366.                 st.clear();
  367.                 sp.clear();
  368.             }
  369.         }
  370.         else
  371.         {
  372.  
  373.             stck.push(st);
  374.             st.clear();
  375.         }
  376.     }
  377.     if(str[strlen(str)-1]!=';')
  378.         stck.push(st);
  379.     string stt;
  380.     string s= stck.top();
  381.  
  382.     while(!stck.empty())
  383.     {
  384.         stt= stck.top();
  385.  
  386.         if(stt.length()==1 && oparator_(stt[0]))
  387.             oparator.insert(stck.top());
  388.  
  389.         else if(stt.length()==1 && special_char(stt[0]))
  390.             special.insert(stck.top());
  391.  
  392.  
  393.         else if( keyword_(stck.top()))
  394.             keyword.insert(stck.top());
  395.  
  396.         else if(integer_(stck.top()))
  397.             integer.insert(stck.top());
  398.  
  399.         else if(real_number_(stck.top()))
  400.             real_number.insert(stck.top());
  401.  
  402.         else if(var(stck.top()))
  403.             veriable.insert(stck.top());
  404.  
  405.         else
  406.             un_var.insert(stck.top());
  407.  
  408.         stck.pop();
  409.     }
  410.  
  411. }
  412.  
  413. int main()
  414. {
  415.  
  416.  
  417.     demo_intput();
  418.  
  419.     ios_base::sync_with_stdio(false);
  420.     cin.tie(NULL);
  421.  
  422.     char str[100][1000];
  423.     int i=0;
  424.     vector< string > vt;
  425.     printf("%d ",i+1) ;
  426.     while(gets(str[i]))
  427.     {
  428.         i++;
  429.         printf("%d ",i+1) ;
  430.     }
  431.     int j=0,k=0;
  432.     string st;
  433.     for(j=0; j<i; j++)
  434.     {
  435.  
  436.         int k=0;
  437.         if(str[j][0]=='\0' || str[j][0]=='\n')
  438.             continue;
  439.         while(str[j][k]==' ')
  440.         {
  441.             k++;
  442.         }
  443.         if(str[j][k]!='#')
  444.         {
  445.             if(isvalid(str[j])==0)
  446.             {
  447.                 printf("Error!!! ' # ' missing at %d line\n\n",j+1);
  448.                 valid =0;
  449.                 return 0;
  450.  
  451.             }
  452.         }
  453.         else
  454.             k++;
  455.         while(str[j][k]==' ')
  456.         {
  457.             k++;
  458.         }
  459.  
  460.         for(int ii=k ; str[j][ii]>='a' && str[j][ii]<='z'; ii++)
  461.             st+=str[j][ii];
  462.         if(st=="include")
  463.             inclu(str[j],j+1);
  464.  
  465.         else if(st=="define")
  466.         {
  467.             def(str[j],j+1);
  468.         }
  469.         else
  470.         {
  471.             valid_stament(str[j],j+1);
  472.         }
  473.  
  474.         st.clear();
  475.     }
  476.  
  477.     set< string > :: iterator it;
  478.  
  479.     if(un_var.size()>0)
  480.     {
  481.         cout<<"variable is not valid "<<endl;
  482.     }
  483.     else if(valid==1)
  484.     {
  485.  
  486.         if(keyword.size()>0)
  487.         {
  488.             cout<<"\n\n\nkeyWord's : ";
  489.             for(it = keyword.begin(); it!=keyword.end(); it++)
  490.                 cout<<*it<<"  ";
  491.  
  492.         }
  493.  
  494.         if(oparator.size()>0)
  495.         {
  496.             cout<<"\n\n\nOperator's : ";
  497.             for(it=oparator.begin(); it!=oparator.end(); it++)
  498.                 cout<<*it<<"  ";
  499.         }
  500.  
  501.  
  502.         if(veriable.size()>0)
  503.         {
  504.             cout<<"\n\n\nVeriable's  : ";
  505.             for(it=veriable.begin(); it!=veriable.end(); it++)
  506.                 cout<<*it<<"  " ;
  507.         }
  508.  
  509.         if(integer.size()>0)
  510.         {
  511.             cout<<"\n\n\nInteger number's : ";
  512.             for(it=integer.begin(); it!=integer.end(); it++)
  513.                 cout<<*it<<"  ";
  514.         }
  515.  
  516.         if(real_number.size()>0)
  517.         {
  518.             cout<<"\n\n\nreal number's : ";
  519.             for(it=real_number.begin(); it!=real_number.end(); it++)
  520.                 cout<<*it<<"  ";
  521.         }
  522.  
  523.         if(special.size()>0)
  524.         {
  525.             cout<<"\n\n\nSpacial character's   : ";
  526.             for(it=special.begin(); it!=special.end(); it++)
  527.                 cout<<*it<<"  ";
  528.         }
  529.         cout<<"\n\n";
  530.          cout<<"CONGRATES !!! ........ No error founded"<<endl;
  531.     }
  532.     cout<<"\n\n";
  533.  
  534.     return 0;
  535. }
  536.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement