Saturday, December 4, 2010

LexicalAnalyser for data types(using LEX)

%{
int nreal=0;
int nint=0;
int ndec=0;
int nimg=0;
%}
int [\+|\-]?[0-9]+
dec [\+|\-]?[0-9]+[\.][0-9]+
real [\+|\-]?[0-9]+[\.][0-9]*e[\+|\-]?[0-9]+
imag [\+|\-]?[0-9]+[i|j]*[\+|\-]?[0-9]+
%%
{int} {nint++;printf("\n%s is integr",yytext);}
{dec} {ndec++;printf("\n%s is decimal",yytext);}
{real} {nreal++;printf("\n%s is real",yytext);}
{imag} {nimg++;printf("\n%s is img",yytext);}
%%
int main()
{
yyin = fopen("d.txt","r");
yylex();
printf("\nno of integers is %d",nint);
printf("\nno of decimals is %d",ndec);
printf("\nno of real is %d",nreal+ndec+nint);
printf("\nno of imaginay is %d\n",nimg);
fclose(yyin);
return 0;
}
int yywrap(void)
{
return 1;
}

1 comment:

  1. this program identifies datatypes for the program in file d.txt..so for this program to execute properly create d.txt,or can change the filename as required..or use standard input!

    ReplyDelete