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

Program:
%{
   
    int lnno=0,wordno=0,charno=0;
%}

word [.* .*\t]
eol [\n]

%%
{word}    {wordno++; charno+=yyleng;}
{eol}     {charno++;lnno++;wordno++;}
.    {charno++;}
%%
main()
{
    yylex();
    printf("Line number= %d\n",lnno);
    printf("Word number= %d\n",wordno);
    printf("Character number= %d\n",charno);
    return 0;
}

Output:

nn@linuxmint ~ $ lex l6.l
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out <c1.c
Line number= 13
Word number= 43
Character number= 232
nn@linuxmint ~ $
( input file : c1.c

#include<stdio.h>
main ()
{
    int i,n,fact=1;
    printf("Enter the number: ");
    scanf("%d",&n);            //inputing the number
    for(i=1;i<=n;i++)        /* finding factorial */
    {
        fact = fact*i;
    }
    printf("Factorial=%d\n",fact);

}
)

4 comments:

  1. plz explain the above program code.

    ReplyDelete
  2. Such a nice article ! I share this article with my friends. keep this type of blog in future.
    Word Count Software

    ReplyDelete
  3. Well explained string operation .
    there are good String program collection visit count the number of occurrences of a character in a string

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...