Example Lex
Example Lex
string
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
{
printf("Enter the string of vowels and consonents:");
yylex();
printf("Number of vowels are: %d\n", vow_count);
printf("Number of consonants are: %d\n", const_count);
return 0;
}
Output:
~$ vi program1.l
~$ lex program1.l
~$ cc lex.yy.c -ll
~$ ./a.out
Enter the string of vowels and consonents: computers
Number of vowels are: 3
Number of consonants are: 6
Steps to execute LEX program in Online
1. Search for online linux terminal
2. Select https://cocalc.com/doc/terminal.html
3. Do sign in with google
4. Create new project
5. Select new -> linux terminal
6. Use vi editor for editing programs and press i ( typing program)
~$ vi filename.l
7. To save and quit use ESC and : wq
8. Steps to execute programs
~$ lex filename.l
~$ cc lex.yy.c -ll
~$ ./a.out
9. To see output Press ctrl + d