Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<string.h>
  3. //#incldue<ctype.h>
  4. void main()
  5. {
  6.     char exp[50]="\0",con[50]="\0",kwd[50]="\0",id[50]="\0",sym[50]="\0",opr[50]="\0";
  7.     char key[6][10]={"if","for","do","while","int","float"};
  8.     char ch;
  9.     char ptr[10][10]={"\0"};
  10.     int i=0,j=0,k=-1,n=-1,p=-1,s=-1;
  11.     puts("Enter expression::");
  12.     gets(exp);
  13.     puts("\n Tokens are::");
  14.     do
  15.     {
  16.         ch=exp[i];
  17.         if(isalpha(ch))
  18.         {
  19.             k=-1;
  20.             ptr[++n][++k]=ch;
  21.             i++;
  22.             ch=exp[i];
  23.             if(isalpha(ch)||isdigit(ch))
  24.             {
  25.                 while(isalpha(ch)||isdigit(ch))
  26.                 {
  27.                     ptr[n][++k]=ch;
  28.                     i++;
  29.                     ch=exp[i];
  30.                 }
  31.                 while(j<6)
  32.                 {
  33.                     if(strcmp(key[j],ptr[n])==0)
  34.                     {
  35.                         ptr[n][++k]=' ';
  36.                         strcat(kwd,ptr[n]);
  37.                         break;
  38.                     }
  39.                     if(j==5)//that means not in key array
  40.                     {
  41.                      ptr[n][++k]=' ';
  42.                      strcat(id,ptr[n]);
  43.                     }
  44.                     j++;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 ptr[n][++k]=' ';
  50.                 strcat(id,ptr[n]);
  51.             }
  52.             i--;//i is increasing in above statement to check other option it must be decreased.
  53.             ch=exp[i];
  54.             j=0;
  55.         }
  56.         else if(isdigit(ch))
  57.         {
  58.             k=-1;
  59.             ptr[++n][++k]=ch;
  60.             i++;
  61.             ch=exp[i];
  62.             if(isdigit(ch))
  63.             {
  64.                 while(isdigit(ch))
  65.                 {
  66.                     ptr[n][++k]=ch;
  67.                     i++;
  68.                     ch=exp[i];
  69.                 }
  70.             }
  71.             i--;
  72.             ch=exp[i];
  73.             ptr[n][++k]=' ';
  74.             strcat(con,ptr[n]);
  75.         }
  76.         else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='<'||ch=='>'||ch=='='||ch=='!')
  77.         {
  78.             opr[++p]=ch;
  79.             i++;
  80.             ch=exp[i];
  81.             if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='<'||ch=='>'||ch=='='||ch=='!')
  82.             {
  83.                 opr[++p]=ch;
  84.             }
  85.             else
  86.             {
  87.                 i--;
  88.                 ch=exp[i];
  89.                 opr[++p]=' ';
  90.             }
  91.         }
  92.         else
  93.         {
  94.             sym[++s]=ch;
  95.             sym[++s]=' ';
  96.         }
  97.         i++;
  98.  
  99.     }while(exp[i]!='\0');
  100.     puts("\nKeyword ::");
  101.     puts(kwd);
  102.     puts("\nIdentifier::");
  103.     puts(id);
  104.     puts("\nConstant::");
  105.     puts(con);
  106.     puts("\nOperator::");
  107.     puts(opr);
  108.     puts("\nSymbol::");
  109.     puts(sym);
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement