Wednesday, September 2, 2020

Create a Lexer to take input from text file and count no of characters, no. of lines & no. of words.

Count no of characters no. of lines & no. of words in Lex Program.

Step 1 :- lex filename.l or lex filename.lex depending on the extension file is saved with
Step 2 :-  gcc lex.yy.c
Step 3 :- ./a.out

file.l 


%{ #include<stdio.h> int lines=0,words=0,char=0; %} %% \n {lines++ ; words;} [\t ' '] words++; [a-zA-Z] char++; %% int main() { yyin=fopen("s.txt" , "r"); yylex(); printf("\n File Contents..\n"); printf("\n\t%d Line",lines); printf("\n\t%d Words",words); printf("\n\t%d Characters",char); return 0; } int yywrap() { return 1; }


INPUT :

s.txt


All our dreams can come true if we have the
courage to pursue them The secret of getting ahead is getting started
Mark Twain


OUTPUT



sunny@sunny-PC:~/Desktop$ flex file.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out
File Contents.. 1 Line 26 Words 111 Characters










No comments:

Post a Comment

Please do not any spam link in Comment Box