Advertisement
Guest User

Lexical Analyzer

a guest
Mar 27th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. void lexical_Analyzer(char s[]);
  5. void isKey();
  6. void isConstant();
  7. int isIdent(char ch);
  8. int isAlpha(char ch);
  9. int isDigit(char ch);
  10. int isOperator(char ch);
  11. int isDelim(char ch);
  12. int isLiteral(char ch);
  13.  
  14. int i=0,j,k,l,n,m,constlenth,idi=0,idj=0,obi=0,obj=0, dbi=0,dbj=0,liti=0,litj=0,ci=0, ki=0;
  15. char A[1000], W[100][100],ident[100][50],keyword[100][100],constant[1000][100],oper[100][50],deli[100][50],literal[1000][100],op[]={'=','+','-','/','*','%','<','>','&','!','|'}, dm[]={'(',')','{','}','[',']',',',';'};
  16. char *key[]={"auto","auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
  17.  
  18. int main()
  19. {
  20.     gets (A);
  21.     lexical_Analyzer(A);
  22.     return 0;
  23. }
  24. void lexical_Analyzer(char s[])
  25. {
  26.  
  27.     while(s[i])
  28.     {
  29.         if(isIdent(s[i]))
  30.         {
  31.            while(isIdent(s[i]))
  32.            {
  33.              ident[idi][idj]=s[i];
  34.                idj++;
  35.                i++;
  36.            }
  37.              ident[idi][idj]='\0';
  38.              idj=0;
  39.              idi++;
  40.          }
  41.         else if(isOperator(s[i]))
  42.         {
  43.           while(isOperator(s[i]))
  44.           {
  45.             oper[obi][obj]=s[i];
  46.             obj++;
  47.             i++;
  48.           }
  49.             oper[obi][obj]='\0';
  50.             obj=0;
  51.             obi++;
  52.  
  53.         }
  54.         else if(isDelim(s[i]))
  55.         {
  56.           while(isDelim(s[i]))
  57.           {
  58.             deli[dbi][dbj]=s[i];
  59.             dbj++;
  60.             i++;
  61.           }
  62.             deli[dbi][dbj]='\0';
  63.             dbj=0;
  64.             dbi++;
  65.         }
  66.          else if(isLiteral(s[i]))
  67.         {
  68.           literal[liti][litj]=s[i];
  69.           litj++;
  70.           i++;
  71.           while(!isLiteral(s[i]))
  72.           {
  73.             literal[liti][litj]=s[i];
  74.             litj++;
  75.             i++;
  76.           }
  77.             literal[liti][litj++]=s[i++];
  78.             literal[liti][litj]='\0';
  79.             litj=0;
  80.             liti++;
  81.         }
  82.  
  83.  
  84.          else{
  85.              i++;
  86.          }
  87.  
  88.     }
  89.     isKey();
  90.     isConstant();
  91.     printf("Identifier: \n");
  92.     for(i=0; i<=idi;i++)
  93.           {
  94.  
  95.           printf("%s\n", ident[i]);
  96.           }
  97.     printf("Oper: \n");
  98.     for(i=0; i<=obi;i++)
  99.           {
  100.  
  101.           printf("%s\n", oper[i]);
  102.           }
  103.     printf("Delimiter: \n");
  104.     for(i=0; i<=dbi;i++)
  105.           {
  106.  
  107.           printf("%s\n", deli[i]);
  108.           }
  109.     printf("Literal: \n");
  110.     for(i=0; i<=liti;i++)
  111.           {
  112.  
  113.           printf("%s\n", literal[i]);
  114.           }
  115.     printf("Keyword: \n");
  116.     for(i=0; i<=ki;i++)
  117.           {
  118.  
  119.           printf("%s\n", keyword[i]);
  120.           }
  121.     printf("Constant: \n");
  122.     for(i=0; i<=ci;i++)
  123.           {
  124.  
  125.           printf("%s\n", constant[i]);
  126.           }
  127.  
  128.     }
  129.  
  130.  
  131.  
  132.  
  133.     int isIdent(char ch)
  134.     {
  135.         if(isAlpha(ch)||isDigit(ch)||ch=='_')
  136.         {
  137.             return 1;
  138.         }
  139.         else
  140.         return 0;
  141.     }
  142.     int isAlpha(char ch)
  143.     {
  144.         if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
  145.         {
  146.             return 1;
  147.         }
  148.         else
  149.         return 0;
  150.     }
  151.     int isDigit(char ch)
  152.     {
  153.         if((ch>='0'&& ch<='9'))
  154.         {
  155.             return 1;
  156.         }
  157.         else
  158.         return 0;
  159.     }
  160.    int isOperator(char ch)
  161.     {
  162.       for(k=0;k<11;k++)
  163.  
  164.          if(ch==op[k])
  165.  
  166.             return 1;
  167.  
  168.             return 0;
  169.     }
  170.     int isDelim(char ch)
  171.     {
  172.       for(l=0;l<8;l++)
  173.  
  174.          if(ch==dm[l])
  175.  
  176.             return 1;
  177.  
  178.             return 0;
  179.     }
  180.     int isLiteral(char ch)
  181.     {
  182.          if(ch=='"')
  183.  
  184.             return 1;
  185.  
  186.             return 0;
  187.     }
  188.  
  189.     void isKey()
  190.     {
  191.       for(i=0;i<idi;i++)
  192.       {
  193.         for(j=0;j<32;j++)
  194.         {
  195.           if(!strcmp(ident[i],key[j]))
  196.              {
  197.                strcpy(keyword[ki++],ident[i]);
  198.  
  199.                for(k=i; k<idi; k++)
  200.                 {
  201.                 strcpy(ident[k],ident[k+1]);
  202.                }
  203.                i=i-1;
  204.                idi=idi-1;
  205.                break;
  206.              }
  207.         }
  208.       }
  209.     }
  210.   void isConstant()
  211.     {
  212.       for(i=0;i<idi;i++)
  213.       {
  214.         constlenth = strlen(ident[i]);
  215.         int f=0;
  216.         for(m=0;m<constlenth;m++)
  217.         {
  218.           if(!isDigit(ident[i][m]))
  219.           {
  220.             f=1;
  221.             break;
  222.           }
  223.         }
  224.         if(f==0)
  225.         {
  226.           strcpy(constant[ci++],ident[i]);
  227.  
  228.           for(k=i; k<idi; k++)
  229.                 {
  230.                 strcpy(ident[k],ident[k+1]);
  231.                }
  232.                i=i-1;
  233.                idi=idi-1;
  234.         }
  235.       }
  236.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement