Sie sind auf Seite 1von 6

1. Implementation of a Lexical Analyzer using C Aim:To implement a Lexical Analyzer using C. Algorithm:1.

Assign some value as token attribute for all the keywords, operators, number, parenthesis etc. 2. Insert all the keywords along with its token attribute in the symbol table. 3. Scan whether the input expression involves new line character, assignment operator, arithmetic operator, relational operator, number, parenthesis, identifiers etc. 4. Use Look up function to check whether the identifier is available in the symbol table. 5. Append any new identifier other than those in symbol table to the symbol table. 6. Include function Error_Message to display the errors along with its line number. 7. After scanning, list the tokens identified from the given input. PROGRAM : #include<stdio.h> #include<string.h> #include<ctype.h> #include<conio.h> void main() { int i=0,j=0,z=0,f; char ch,ch1,head[4][10]={"stdio.h","conio.h","math.h","ctype.h"}; char word[7][10]={"include","int","float","char","void","main","printf"}; char temp[10]; FILE *fp1,*fp2; clrscr(); fp1=fopen("inttxt.c","r"); fp2=fopen("outtxt.c","w"); fscanf(fp1,"%c",&ch); while(!feof(fp1)) { f=0; for(z=0;z<=10;z++) { temp[z]=NULL; } while(isalpha(ch)||ch=='.') { temp[i]=ch; fscanf(fp1,"%c",&ch); i++; }

i=0; while(isdigit(ch)) { temp[i]=ch; fscanf(fp1,"%c",&ch); i++; f=5; } for(j=0;j<3;j++) if(strcmpi(temp,head[j])==0) { f=1; goto a; } if(isalpha(temp[0])) for(j=0;j<3;j++) { if(strcmpi(temp,word[j])==0) f=2; else f=3; if(f==2) break; } if(isdigit(temp[0])) f=5; a: switch(f) { case 1: fprintf(fp2,"\n\%s\t\t headerfile\n\t",temp); break; case 2: fprintf(fp2,"\n\%s\t\tkeyword\n\t",temp); break; case 3: fprintf(fp2,"\n\%s\t\tidentifier\n\t",temp); break; case 4: fprintf(fp2,"\n\%s\t\tnumerical constant\n\t",temp); break; case 5: fprintf(fp2,"\n\%s\t\t digit\n\t",temp); break; } i=0;

switch(ch) { case'#': case',': case'<': case'>': case'{': case '}': case';': case'(': case')': case'%': case'=': fprintf(fp2,"\n\%c\t\tspecial character\n\t",ch); break; } fscanf(fp1,"%c",&ch); if(feof(fp1)) break; } fclose(fp1); printf("\n\n sucessfully done!!\n\n\n"); printf("\n\n\tINPUT\n\n\tintxt.c"); printf("\n\n\tOUTPUT\n\n\touttxt.c"); getch(); } INPUT: inttxt.c #include<stdio.h> void main() { int a,b,c; a=1; b=10; c=5; printf("%d%d%d",a,b,c); } OUTPUT: outtxt.c # special character include keyword < special character stdio.h headerfile > special character void identifier main identifier

( special character ) special character { special character int keyword a identifier , special character b identifier , special character c identifier ; special character a identifier = special character 1 digit ; special character b identifier = special character 10 digit ; special character c identifier = special character 5 digits ; special character printf identifier ( special character % special character d identifier % special character d identifier % special character d identifier , special character a identifier , special character b identifier , special character c identifier ) special character ; special character } special character

#include<stdio.h> int main() { FILE *fp; Char key[30][30]={if,else,void,for,switch,char,int,while,do}; Char op[20]={+,-,*,/,%}; Char sym[20]={{,},(,),&,/,=,[,],<,>,#,.,,}; Int I,j=0,num=0,flag,f,s; Char file[20],ch,id[30]; Printf(\n\t\t Token separation\t); Printf(\n\t Enter the filename); Scanf(%s,&file); Fp=fopen(file,r); Printf(\n\t Line no\ttoken\ttype); Printf(\n\t----------------------------\n); While(!feof(fp)) { Flag=0; Ch=getc(fp); i=0; if(isalpha(ch)||isdigit(ch)) { f=0; while(isalpha(ch)||isdigit(ch)) { id[j++]=ch; ch=fgetc(fp); } id[j]=\0; j=0; while(i<10) { If(strcmp(id,key[i])==0) { Printf(\n\t%d\t\t%s\t\tKeyword,num,id); f=1; } i++; } if(f==0) printf(\n\t\t%d\t\t%s\t\tidentifier,num,id); } i=0; while(i<s&&flag!=1)

{ if(ch==op[i]) { Printf(\n\t\t%d\t\t%c\t\toperator,num,ch); flag=1; } i++; } i=0; while(i<13&&flag==0) { if(ch==sym[i]) { Printf(\n\t%d\t\t%c\t\tsymbol),num,ch); flag=1; } i++; } if(ch==;) printf(\n\t%\t\t%c\t\tdelimeter,num,ch); if(ch==\n) num++; getch(); } }

Das könnte Ihnen auch gefallen