0% found this document useful (0 votes)
15 views

Example Lex

This document describes a LEX program that counts the number of vowels and consonants in a given string. It contains the code for the LEX program that uses regular expressions to match vowels and consonants and increments counters for each. It also provides steps for writing, compiling, and running a LEX program online using a Linux terminal in Cocalc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Example Lex

This document describes a LEX program that counts the number of vowels and consonants in a given string. It contains the code for the LEX program that uses regular expressions to match vowels and consonants and increments counters for each. It also provides steps for writing, compiling, and running a LEX program online using a Linux terminal in Cocalc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

LEX program to count the number of vowels and consonants in a given

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

You might also like