Count The Number of lines ending with "com" - Lex Program - Compiler Design

Program:

//Lex file: com.l

%{
int count=0;
%}
DIGIT [0-9]
ALPHA [a-zA-Z]
%%
({ALPHA}|{DIGIT})*com {count++;}
%%

main()
{
    yylex();
    printf("Count= %d\n",count);
    return 0;
   
}

Output :

n@linuxmint ~ $ lex com.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<com.txt
www.2k8618.blogspot.
www.2k8cs.tk
www.google.
www.gmail.

Count= 3
nn@linuxmint ~ $

2 comments:

  1. what will be the expression to count the number of words ending with a vowel?

    ReplyDelete
  2. Com comprises of 3 letters. Below are Total 4 words Ending with Com (Suffix). COM is not a word but only a combination of letters com is made up of letters C, O and M.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...