%{
unsigned long charCount=0, wordCount=0, lineCount=0;
%}
word [^ \t\n]+
eol \n
%%
{word}    { wordCount++; charCount+=yyleng; }
{eol}    {charCount++; lineCount++; }
.    {charCount++;}
%%
int main(void) 
{
    FILE *file;
    char fn[20];
    printf("Type a ipnut file Name:");
    scanf("%s",fn);
    file=fopen(fn, "r");
    if (!file) {
        fprintf(stderr,"file '%s' could not be opened. \n",fn);
        exit(1);
        }

    yyin=file;
    yylex();
    printf("%d %d %d %s \n", lineCount, wordCount, charCount,fn);

    return 0;
}
yywrap() { return 1; }

+ Recent posts