counting valid identifiers lex program
The program will count and print the number of valid identifiers.Users can input identifiers line by line and press ctrl-D for terminating the input.
**********************************************
digit [0-9]
letter [A-Za-z_]
%{
int count=0;
%}
%option noyywrap
%%
/* match identifier */
{letter}({letter}|{digit})* count++;
.* ;
\n ;
%%
int main(void)
{
yylex();
printf("number of identifiers = %d\n", count);
return 0;
}
Execution
$ flex id.lex
$ gcc lex.yy.c
$ ./a.out
hi
this
2dkd
_djd
d34
dkd-dfjf
number of identifiers = 4
Comments
Post a Comment