[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Compiler 3

The document contains three programs written in C and Lex. Program No-5 checks if an input string is accepted by a specific grammar, while Programs No-6 and No-7 count and display the number of vowels and consonants in a given string. Each program includes code snippets and prompts for user input.

Uploaded by

kumar08012005
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)
5 views6 pages

Compiler 3

The document contains three programs written in C and Lex. Program No-5 checks if an input string is accepted by a specific grammar, while Programs No-6 and No-7 count and display the number of vowels and consonants in a given string. Each program includes code snippets and prompts for user input.

Uploaded by

kumar08012005
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
You are on page 1/ 6

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 VERMA)(Roll No-2300320109014)

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:-

You might also like