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.