LEXICAL ANALYSER #include #include #include #include<string.h> #include class lexical { private: char st[100]; public: void token(); }; void lexical::token() { fstream fin,in; ofstream fout; char ke[25]; int f=0; fin.open("TEXT.TXT",ios::in); fin>>st; fout.open("OUT.TXT",ios::in); while(fin.get()!=EOF) { in.open("KEY.TXT",ios::in); in>>ke; while(in.get()!=EOF) { if(strcmp(st,ke)==0) { fout<<"\n"<<st<<"\tKEYWORD\t"; f=1; } in>>ke; } in.close(); in.open("SYMBOL.TXT",ios::in); in>>ke;
while(in.get()!=EOF) { if(strcmp(st,ke)==0) { fout<<"\n"<<st<<"\t"<<"OPERATOR"; f=1; } in>>ke; } in.close(); if(f==0) { if(isdigit(st[0])) { fout<<"\n"<<st<<"\t"<<"CONSTANT"; } else { fout<<"\n"<<st<<"\tIDENTIFIER\t"; } } f=0; fin>>st; } fin.close(); fout.close(); } void main() { clrscr(); lexical l; l.token(); cout<<"\nTHE OUTPUT IS STORED IN OUT.TXT"; getch(); }
INPUT: TEXT.TXT: INT A = INT B + INT C + 50 ;
KEY.TXT: INT FLOAT DOUBLE LONG STR CHAR
SYMBOL.TXT: = + * -
OUTPUT: THE OUTPUT IS STORED IN OUT.TXT OUT.TXT: INT A = INT B + INT C + 50
KEYWORD IDENTIFIER OPERATOR KEYWORD IDENTIFIER OPERATOR KEYWORD IDENTIFIER OPERATOR CONSTANT