Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- void lexical_Analyzer(char s[]);
- void isKey();
- void isConstant();
- int isIdent(char ch);
- int isAlpha(char ch);
- int isDigit(char ch);
- int isOperator(char ch);
- int isDelim(char ch);
- int isLiteral(char ch);
- 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;
- 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[]={'(',')','{','}','[',']',',',';'};
- 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"};
- int main()
- {
- gets (A);
- lexical_Analyzer(A);
- return 0;
- }
- void lexical_Analyzer(char s[])
- {
- while(s[i])
- {
- if(isIdent(s[i]))
- {
- while(isIdent(s[i]))
- {
- ident[idi][idj]=s[i];
- idj++;
- i++;
- }
- ident[idi][idj]='\0';
- idj=0;
- idi++;
- }
- else if(isOperator(s[i]))
- {
- while(isOperator(s[i]))
- {
- oper[obi][obj]=s[i];
- obj++;
- i++;
- }
- oper[obi][obj]='\0';
- obj=0;
- obi++;
- }
- else if(isDelim(s[i]))
- {
- while(isDelim(s[i]))
- {
- deli[dbi][dbj]=s[i];
- dbj++;
- i++;
- }
- deli[dbi][dbj]='\0';
- dbj=0;
- dbi++;
- }
- else if(isLiteral(s[i]))
- {
- literal[liti][litj]=s[i];
- litj++;
- i++;
- while(!isLiteral(s[i]))
- {
- literal[liti][litj]=s[i];
- litj++;
- i++;
- }
- literal[liti][litj++]=s[i++];
- literal[liti][litj]='\0';
- litj=0;
- liti++;
- }
- else{
- i++;
- }
- }
- isKey();
- isConstant();
- printf("Identifier: \n");
- for(i=0; i<=idi;i++)
- {
- printf("%s\n", ident[i]);
- }
- printf("Oper: \n");
- for(i=0; i<=obi;i++)
- {
- printf("%s\n", oper[i]);
- }
- printf("Delimiter: \n");
- for(i=0; i<=dbi;i++)
- {
- printf("%s\n", deli[i]);
- }
- printf("Literal: \n");
- for(i=0; i<=liti;i++)
- {
- printf("%s\n", literal[i]);
- }
- printf("Keyword: \n");
- for(i=0; i<=ki;i++)
- {
- printf("%s\n", keyword[i]);
- }
- printf("Constant: \n");
- for(i=0; i<=ci;i++)
- {
- printf("%s\n", constant[i]);
- }
- }
- int isIdent(char ch)
- {
- if(isAlpha(ch)||isDigit(ch)||ch=='_')
- {
- return 1;
- }
- else
- return 0;
- }
- int isAlpha(char ch)
- {
- if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
- {
- return 1;
- }
- else
- return 0;
- }
- int isDigit(char ch)
- {
- if((ch>='0'&& ch<='9'))
- {
- return 1;
- }
- else
- return 0;
- }
- int isOperator(char ch)
- {
- for(k=0;k<11;k++)
- if(ch==op[k])
- return 1;
- return 0;
- }
- int isDelim(char ch)
- {
- for(l=0;l<8;l++)
- if(ch==dm[l])
- return 1;
- return 0;
- }
- int isLiteral(char ch)
- {
- if(ch=='"')
- return 1;
- return 0;
- }
- void isKey()
- {
- for(i=0;i<idi;i++)
- {
- for(j=0;j<32;j++)
- {
- if(!strcmp(ident[i],key[j]))
- {
- strcpy(keyword[ki++],ident[i]);
- for(k=i; k<idi; k++)
- {
- strcpy(ident[k],ident[k+1]);
- }
- i=i-1;
- idi=idi-1;
- break;
- }
- }
- }
- }
- void isConstant()
- {
- for(i=0;i<idi;i++)
- {
- constlenth = strlen(ident[i]);
- int f=0;
- for(m=0;m<constlenth;m++)
- {
- if(!isDigit(ident[i][m]))
- {
- f=1;
- break;
- }
- }
- if(f==0)
- {
- strcpy(constant[ci++],ident[i]);
- for(k=i; k<idi; k++)
- {
- strcpy(ident[k],ident[k+1]);
- }
- i=i-1;
- idi=idi-1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement