Program No-5
Ques:- WAP to check whether the input string is accepted or not by S->
aS|Sb|b code in c.
(Name:-SHIVANI
(NAYAN KUMAR VERMA)(Roll No-2300320109014)
2200320100104)
Code:-
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isAccepted(char str[]) {
int len = strlen(str);
int i = 0;
while (i < len && str[i] == 'a') {
i++;
int b_start = i;
while (i < len && str[i] == 'b') {
i++;
if (i == len && strchr(str, 'b') != NULL && strrchr(str, 'a') < strchr(str, 'b')) {
return true;
} else {
return false;
}
int main() {
char input[100];
printf("Enter the string: ");
scanf("%s", input);
if (isAccepted(input)) {
printf("String is accepted by the grammar.\n");
} else {
printf("String is NOT accepted by the grammar.\n");
return 0;
Output:-
Program No-6
Ques-Lex program to recognize and display keywords, numbers and words in a
statement.
Code:-
%{
#include <stdio.h>
int vowels = 0, consonants = 0;
%}
%%
[aAeEiIoOuU] { vowels++; }
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] { consonants++; }
.|\n ;
%%
int main() {
printf("Enter a string:\n");
yylex();
printf("Total Vowels: %d\n", vowels);
printf("Total Consonants: %d\n", consonants);
return 0;
int yywrap() {
return 1;
Output:-
Program No-7
Ques:- Lex program to count the number of vowels and consonants in a given string.
Code:-
%{
#include <stdio.h>
int vowels = 0;
int consonants = 0;
%}
%%
[aAeEiIoOuU] { vowels++; }
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] { consonants++; }
.|\n ;
%%
int main() {
printf("Enter a string:\n");
yylex();
printf("Total Vowels: %d\n", vowels);
printf("Total Consonants: %d\n", consonants);
return 0;
int yywrap() {
return 1;
Output:-