Lex Program to count the number of words in a given string.
Source Code:
%{
#include<stdio.h>
#include<string.h>
int word=0;
%}
%%
[\n] {printf("Total Number of Words: %d\n",word); word=0; printf("\nEnter the string: ");}
([a-zA-Z0-9])* {word++;}
%%
int yywrap(void){}
int main()
{
printf("\nEnter the string: ");
yylex();
return 0;
}
Output:#include<stdio.h>
#include<string.h>
int word=0;
%}
%%
[\n] {printf("Total Number of Words: %d\n",word); word=0; printf("\nEnter the string: ");}
([a-zA-Z0-9])* {word++;}
%%
int yywrap(void){}
int main()
{
printf("\nEnter the string: ");
yylex();
return 0;
}
0 Comments