Write Lexical Analyzer program in C language.
Below is the sample input and ouput.
/* output
Enter the string:
if(a<b){a=10;}
Tokens are
identifier :if
punctuation mark : (
identifier :a
operator:<
identifier :b
punctuation mark : )
punctuation mark : {
identifier :a
operator:=
constant :10
punctuation mark : ;
punctuation mark : }
*/
/ Name: Lexical Analyzer in C // Description:It will lexically Analyze the given file(C program) and it willgive the various tokens present in it.. #include
#include #include void keyw(char *p); int i=0,id=0,kw=0,num=0,op=0; char keys[32][10]={\"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\"}; main() { char ch,str[25],seps[15]=\" \\t\ ,;(){}[]#\\\"<>\",oper[]=\"!%^&*-+=~|.<>/?\"; int j; char fname[50]; FILE *f1; //clrscr(); printf(\"enter file path (drive:\\\\fold\\\\filename)\ \"); scanf(\"%s\",fname); f1 = fopen(fname,\"r\"); //f1 = fopen(\"Input\",\"r\"); if(f1==NULL) { printf(\"file not found\"); exit(0); } while((ch=fgetc(f1))!=EOF) { for(j=0;j<=14;j++) { if(ch==oper[j]) { printf(\"%c is an operator\ \",ch); op++; str[i]=\'\\0\'; keyw(str); } } for(j=0;j<=14;j++) { if(i==-1) break; if(ch==seps[j]) { if(ch==\'#\') { while(ch!=\'>\') { printf(\"%c\",ch); ch=fgetc(f1); } printf(\"%c is a header file\ \",ch); i=-1; break; } if(ch==\'\"\') { do { ch=fgetc(f1); printf(\"%c\",ch); }while(ch!=\'\"\'); printf(\"\\b is an argument\ \"); i=-1; break; } str[i]=\'\\0\'; keyw(str); } } if(i!=-1) { str[i]=ch; i++; } else i=0; } printf(\"Keywords: %d\ Identifiers: %d\ Operators: %d\ Numbers: %d\ \",kw,id,op,num); //getch(); } void keyw(char *p) { int k,flag=0; for(k=0;k<=31;k++) { if(strcmp(keys[k],p)==0) { printf(\"%s is a keyword\ \",p); kw++; flag=1; break; } } if(flag==0) { if(isdigit(p[0])) { printf(\"%s is a number\ \",p); num++; } else { //if(p[0]!=13&&p[0]!=10) if(p[0]!=\'\\0\') { printf(\"%s is an identifier\ \",p); id++; } } } i=-1; }