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

Popular posts from this blog

KTU Compiler Lab CSL411 - Dr Binu V P

lexical analyzer for a c program

13.Precedence and conflict resolution in yacc