Sample Lex Programs contd

Sample Lex Programs contd

%{

/* unix utility wc simulated. counts chars words and lines*/

%}

int nchar,nword,nlines;

%%

\n nchar++;nlines++;

[^ \t\n]+ {nword++;nchar+=yyleng; /*yyleng gives the length of the pattern*/}

. nchar++;

%%

void main(void)

{ yylex();

printf(“%d\t%d\t%d\n”,nchar,nword,nlines);

}

Previous slide Next slide Back to the first slide View Graphic Version