0% found this document useful (0 votes)
8 views3 pages

LEX Programs for Text Analysis in C

The document contains several LEX programs that demonstrate different functionalities, including matching strings, counting vowels and consonants, and counting blank spaces, words, and lines. Each program includes a brief solution with code snippets and expected output. Additionally, there is a mention of a program to find the First of any grammar, but no details are provided for that program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

LEX Programs for Text Analysis in C

The document contains several LEX programs that demonstrate different functionalities, including matching strings, counting vowels and consonants, and counting blank spaces, words, and lines. Each program includes a brief solution with code snippets and expected output. Additionally, there is a mention of a program to find the First of any grammar, but no details are provided for that program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 11: Write a basic LEX program to match and print

Solution:

%{

#include<stdio.h>

%}

%%
"hi" {printf("By");};

.* {printf("wrong");};

%%

int main()

{
printf ("enter input ");

yylex();

int yywrap()

return 1;

Output:

enter input hi

By

Program 12:LEX Program to Count Vowels and Consonants in a C File


Solution:

%{

#include<stdio.h>

int vow_count=0;

int const_count =0;

%}
%%

[aeiouAEIOU] {vow_count++;};

[a-zA-Z] {const_count++;};

%%
int main()

printf("Enter the string of vowels and consonants:");

yylex();

printf("Number of vowels are: %d\n", vow_count);

printf("Number of consonants are: %d\n", const_count);

int yywrap()

return 1;

}
Output:

Enter the string of vowels and consonants: my name

Number of vowels are: 2


Number of consonants are: 4

Program 13: Write a LEX program to count Blank Space, Words and Lines
Solution:

%{

#include <stdio.h>

int spaces = 0, words = 0, lines = 0;

%}

%%
"" { spaces++; }

"\t" { spaces++; }
\n { lines++; }

[^ \t\n]+ { words++; }

%%

int main()
{

printf("Enter text :\n");

yylex();

printf("\nNumber of lines: %d\n", lines);

printf("Number of words: %d\n", words);

printf("Number of spaces: %d\n", spaces);

return 0;

Output:

Enter text : Computer Engineering Department

RTU
Number of lines: 2

Number of words: 4

Number of spaces:2

Program 14: Write a program to find First of any Grammar.

You might also like