Number of Lines,Words and Characters - Lex Program - Compiler Design

Program:

// lwc.l

%{

int lines=0,wrds=0,characters=0;

%}

whitespace    [ \n\t]*

%%

[ \t]{whitespace}    {wrds++;characters++;}
\n{whitespace}        {wrds++;characters++;lines++;}
.            {characters++;}

%%
main()
{
    yylex();
    printf("\nlines: %d\nwords: %d\ncharacters: %d\n",lines,wrds,characters);
}

Output:


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...