Saturday, September 3, 2011

3. A Lex Program to Count Number of Identifiers in a C program.


A Lex Program to Count Number of Identifiers in a C program.

Note: Only type : int/float/double/char are supported.

Program:



%option noyywrap
%{
int id_cnt=0;
char ch;
%}
%%
"int"|"float"|"double"|"char"  { ch=input();
for(;;)
{
if(ch==',')
       id_cnt++;
else if(ch==';')
       break;
ch=input();
}
id_cnt++;
}
%%
main(int argc,char **argv)
{

FILE *fp;
fp=fopen(argv[1],"r");

yyin=fp;
yylex();
printf("\n total number of identifiers are %d",id_cnt);
}

No comments:

Post a Comment