Advertisement
backstreetimrul

Tokenizer

Jul 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.36 KB | None | 0 0
  1. /*** Imrul Hasan - Reg:16101034 - Sec:A(A2) ***/
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. int main ()
  6. {
  7.     //All the possible type
  8.     char keywords[32][10]={"break","case","char","const","continue","default","do","auto","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while",};
  9.     char arithmatical[8][5]={"+","-","*","/","%","++","--","="};
  10.     char punctuation[3][2]={",",":",";"};
  11.     char parenthesis[6][3]={"(",")","{","}","[","]"};
  12.     char logical[6][3]={"<",">","<=",">=","==","!="};
  13.  
  14.     //All the type from gets()
  15.     char *keywordss[15];
  16.     char *arithmaticall[10];
  17.     char *punctuationn[10];
  18.     char *parenthesiss[10];
  19.     char *logicall[10];
  20.  
  21.     //For Special Case
  22.     char *identifierr[20];
  23.     char *constantt[20];
  24.     char *invalid[20];
  25.  
  26.         //Input String
  27.         char str[80];
  28.         gets(str);
  29.         const char s[2] = " ";
  30.         char *token;
  31.         int i;
  32.         //size of *char
  33.         int key=0;
  34.         int arith=0;
  35.         int punc=0;
  36.         int peren=0;
  37.         int log=0;
  38.         int con_count=0;
  39.         int iden=0;
  40.         int inv=0;
  41.  
  42.         token = strtok(str, s);
  43.         while( token != NULL )
  44.         {
  45.         ///Code for Checking Keywords
  46.         for(i=0; i<32; i++)
  47.         {
  48.             if(((strcmp(keywords[i],token))==0))
  49.             {
  50.                 int flag=0;
  51.                 for(int c=1;c<=key;c++)
  52.                 {
  53.                     if(((strcmp(keywordss[c],token))==0))
  54.                     {
  55.                         flag=1;
  56.                         break;
  57.                     }
  58.                 }
  59.                 if(flag==0)
  60.                 {
  61.                     key++;
  62.                     keywordss[key]=token;
  63.                 }
  64.             }
  65.         }
  66.  
  67.         ///Code for Checking Arithmatic Operator
  68.         for(i=0; i<8; i++){
  69.             if(((strcmp(arithmatical[i],token))==0)){
  70.                 int flag=0;
  71.                 for(int c=1;c<=arith;c++){
  72.                     if(((strcmp(arithmaticall[c],token))==0)){
  73.                         flag=1;
  74.                         break;
  75.                     }
  76.                 }
  77.                 if(flag==0){
  78.                     arith++;
  79.                     arithmaticall[arith]=token;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         ///Code for Checking Punctuation
  85.         for(i=0; i<3; i++){
  86.             if(((strcmp(punctuation[i],token))==0)){
  87.                 int flag=0;
  88.                 for(int c=1;c<=punc;c++){
  89.                     if(((strcmp(punctuationn[c],token))==0)){
  90.                         flag=1;
  91.                         break;
  92.                     }
  93.                 }
  94.                 if(flag==0){
  95.                     punc++;
  96.                     punctuationn[punc]=token;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         ///Code for Checking Parenthesis
  102.         for(i=0; i<6; i++){
  103.             if(((strcmp(parenthesis[i],token))==0)){
  104.                 int flag=0;
  105.                 for(int c=1;c<=peren;c++){
  106.                     if(((strcmp(parenthesiss[c],token))==0)){
  107.                         flag=1;
  108.                         break;
  109.                     }
  110.                 }
  111.                 if(flag==0){
  112.                     peren++;
  113.                     parenthesiss[peren]=token;
  114.                 }
  115.             }
  116.         }
  117.  
  118.         ///Code for Checking Logical Operator
  119.         for(i=0; i<6; i++){
  120.             if(((strcmp(logical[i],token))==0)){
  121.                 int flag=0;
  122.                 for(int c=1;c<=log;c++){
  123.                     if(((strcmp(logicall[c],token))==0)){
  124.                         flag=1;
  125.                         break;
  126.                     }
  127.                 }
  128.                 if(flag==0){
  129.                     log++;
  130.                     logicall[log]=token;
  131.                 }
  132.             }
  133.         }
  134.  
  135.         ///Code for checking constant
  136.         int con=0;
  137.         int len;
  138.         if(isdigit(token[0])){
  139.             len = strlen(token);
  140.             for(int x=0;x<len;x++){
  141.                 if(isdigit(token[x])){
  142.                     con++;
  143.                 }
  144.             }
  145.             if(con==len){
  146.                 int flag=0;
  147.                 for(int c=1;c<=con_count;c++){
  148.                     if(((strcmp(constantt[c],token))==0)){
  149.                         flag=1;
  150.                         break;
  151.                     }
  152.                 }
  153.                 if(flag==0){
  154.                     con_count++;
  155.                     constantt[con_count]=token;
  156.                 }
  157.             }
  158.             else{
  159.                 //printf("Invalid Input\n");
  160.                 inv++;
  161.                 invalid[inv]=token;
  162.             }
  163.         }
  164.  
  165.         ///Code for checking Identifier
  166.         int yeah=0;
  167.         for(i=0; i<32; i++){
  168.             if(((strcmp(keywords[i],token))!=0)){
  169.                 yeah++;
  170.             }
  171.         }
  172.         if(yeah==32){
  173.             int l;
  174.             if(isalpha(token[0]) || token[0]=='_'){
  175.                 int flag=0;
  176.                 for(int c=1;c<=iden;c++){
  177.                     if(((strcmp(identifierr[c],token))==0)){
  178.                         flag=1;
  179.                         break;
  180.                     }
  181.                 }
  182.                 if(flag==0){
  183.                     iden++;
  184.                     identifierr[iden]=token;
  185.                 }
  186.             }
  187.         }
  188.         token = strtok(NULL, s);
  189.     }
  190.  
  191.     ///Printing all the possible types
  192.     printf("keyword(%d): ",key);
  193.     for(int i=1;i<=key;i++){
  194.         if(i==key){
  195.             printf("%s ",keywordss[i]);
  196.             break;
  197.         }
  198.         printf("%s, ",keywordss[i]);
  199.     }
  200.     printf("\n");
  201.  
  202.     printf("Arithmatical(%d): ",arith);
  203.     for(int i=1;i<=arith;i++){
  204.         if(i==arith){
  205.             printf("%s ",arithmaticall[i]);
  206.             break;
  207.         }
  208.         printf("%s, ",arithmaticall[i]);
  209.     }
  210.     printf("\n");
  211.  
  212.     printf("Punctuation(%d): ",punc);
  213.     for(int i=1;i<=punc;i++){
  214.         if(i==punc){
  215.             printf("%s ",punctuationn[i]);
  216.             break;
  217.         }
  218.         printf("%s, ",punctuationn[i]);
  219.     }
  220.     printf("\n");
  221.  
  222.     printf("Parenthesis(%d): ",peren);
  223.     for(int i=1;i<=peren;i++){
  224.         if(i==peren){
  225.             printf("%s ",parenthesiss[i]);
  226.             break;
  227.         }
  228.         printf("%s, ",parenthesiss[i]);
  229.     }
  230.     printf("\n");
  231.  
  232.     printf("Logical(%d): ",log);
  233.     for(int i=1;i<=log;i++){
  234.         if(i==log){
  235.             printf("%s ",logicall[i]);
  236.             break;
  237.         }
  238.         printf("%s, ",logicall[i]);
  239.     }
  240.     printf("\n");
  241.  
  242.     printf("Constant(%d): ",con_count);
  243.     for(int i=1;i<=con_count;i++){
  244.         if(i==con_count){
  245.             printf("%s ",constantt[i]);
  246.             break;
  247.         }
  248.         printf("%s, ",constantt[i]);
  249.     }
  250.     printf("\n");
  251.  
  252.     printf("Identifier(%d): ",iden);
  253.     for(int i=1;i<=iden;i++){
  254.         if(i==iden){
  255.             printf("%s ",identifierr[i]);
  256.             break;
  257.         }
  258.         printf("%s, ",identifierr[i]);
  259.     }
  260.     printf("\n");
  261.  
  262.     printf("Invalid(%d): ",inv);
  263.     for(int i=1;i<=inv;i++){
  264.         if(i==inv){
  265.             printf("%s ",invalid[i]);
  266.             break;
  267.         }
  268.         printf("%s, ",invalid[i]);
  269.     }
  270.     printf("\n");
  271.  
  272.     main();
  273.     return(0);
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement