Wednesday, September 23, 2015

Filled Under:

Lex program to count tokens in a program

Share
NOTE: You need to create a file in.txt first to provide input program to count tokens in that program.

%{
int k=0;
int n=0;
int o=0;
int id=0;
int u=0;
%}
digit [0-9]+
id [a-zA-Z]+[0-9]*
operator [-+*/]+
relop [<=>]
%%
"quit" {printf("Keyword = %d\nNumber = %d\nOperator = %d\nIdentifier = %d\n",k,n,o,i,d,u);}
"int"|"float"|"void"|"main"|"while"|"if"|"then"|"else"|"do"|"char"|"printf"|"scanf" {k=k+1;}
{id} {id=id+1;}
{digit} {n=n+1;}
{operator} {o=o+1;}
. {u=u+1;}
%%
main()
{
yyin=fopen("in.txt","r");
yylex();
fclose(yyin);
}

0 comments:

Post a Comment