Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Compiler 5-10

The document contains a series of programming experiments demonstrating various tasks such as checking string acceptance by a grammar, converting NFA to DFA, recognizing keywords and numbers, identifying capital words, counting vowels and consonants, and counting lines, words, spaces, and characters in a string. Each experiment includes the aim and corresponding C code for implementation. The experiments cover fundamental concepts in automata theory and lexical analysis.

Uploaded by

Pulkit Mathur
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 views11 pages

Compiler 5-10

The document contains a series of programming experiments demonstrating various tasks such as checking string acceptance by a grammar, converting NFA to DFA, recognizing keywords and numbers, identifying capital words, counting vowels and consonants, and counting lines, words, spaces, and characters in a string. Each experiment includes the aim and corresponding C code for implementation. The experiments cover fundamental concepts in automata theory and lexical analysis.

Uploaded by

Pulkit Mathur
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/ 11

Experiment 5

Aim:- WAP to check whether the input string is accepted or not by S-> aS|Sb|b

Code:-
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

// Function to check if string matches the grammar S -> aS | Sb | b


bool is_accepted(const char *str) {
int i = 0;
int len = strlen(str);

// Count all 'a's from the start


while (str[i] == 'a') {
i++;
}

// After 'a's, there must be at least one 'b'


if (i == len) return false; // No 'b's at all

// The rest of the string must be all 'b's


while (i < len) {
if (str[i] != 'b') return false;
i++;
}

return true;
}

int main() {
char input[100];

printf("Enter a string: ");


scanf("%s", input);

if (is_accepted(input)) {
printf("The string is accepted by the grammar.\n");
} else {
printf("The string is NOT accepted by the grammar.\n");
}
return 0;
}
OUTPUT:
Experiment 6

Aim:- WAP to convert NFA to DFA (without null moves)


Code:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_STATES 20
#define MAX_SYMBOLS 2 // assuming binary alphabet {0,1}

int nfa[MAX_STATES][MAX_SYMBOLS][MAX_STATES];
int nfa_states, symbols;
int dfa[MAX_STATES * MAX_STATES][MAX_SYMBOLS];
int dfa_states = 0;

int state_set[MAX_STATES * MAX_STATES][MAX_STATES]; // store DFA states as sets


int visited[MAX_STATES * MAX_STATES];

// Check if two sets are equal


int is_same_set(int *a, int *b, int n) {
for (int i = 0; i < n; i++)
if (a[i] != b[i]) return 0;
return 1;
}

// Find if a state set already exists


int set_exists(int *set) {
for (int i = 0; i < dfa_states; i++) {
if (is_same_set(state_set[i], set, nfa_states))
return i;
}
return -1;
}

// Add a new state set


int add_state_set(int *set) {
for (int i = 0; i < nfa_states; i++)
state_set[dfa_states][i] = set[i];
return dfa_states++;
}

// Print DFA table


void print_dfa() {
printf("\nDFA Transition Table:\n");
printf("State\tInput0\tInput1\n");
for (int i = 0; i < dfa_states; i++) {
printf("{");
for (int j = 0; j < nfa_states; j++)
if (state_set[i][j]) printf("%d", j);
printf("}\t");

for (int s = 0; s < symbols; s++) {


int target = dfa[i][s];
printf("{");
for (int j = 0; j < nfa_states; j++)
if (state_set[target][j]) printf("%d", j);
printf("}\t");
}
printf("\n");
}}

int main() {
printf("Enter number of NFA states: ");
scanf("%d", &nfa_states);
symbols = MAX_SYMBOLS; // fixed alphabet: 0,1

// Initialize NFA table


for (int i = 0; i < nfa_states; i++)
for (int j = 0; j < symbols; j++)
for (int k = 0; k < nfa_states; k++)
nfa[i][j][k] = 0;

// Input NFA transitions


printf("Enter transitions (from_state input_symbol to_state). Enter -1 to stop.\n");
while (1) {
int from, sym, to;
printf("From State: "); scanf("%d", &from);
if (from == -1) break;
printf("Input Symbol (0 or 1): "); scanf("%d", &sym);
printf("To State: "); scanf("%d", &to);
nfa[from][sym][to] = 1;
}

// Initial DFA state = {0}


int start[MAX_STATES] = {0};
start[0] = 1;
add_state_set(start);

// Process DFA construction


for (int i = 0; i < dfa_states; i++) {
for (int s = 0; s < symbols; s++) {
int next[MAX_STATES] = {0};

// For each NFA state in the current DFA state


for (int j = 0; j < nfa_states; j++) {
if (state_set[i][j]) {
for (int k = 0; k < nfa_states; k++) {
if (nfa[j][s][k])
next[k] = 1;
}
}
}
int idx = set_exists(next);
if (idx == -1)
idx = add_state_set(next);

dfa[i][s] = idx;
}
}
print_dfa();

return 0;
}
Experiment 7:

Aim:- Write a lex program to recognize and display keywords, numbers and words from a
sentence.
Code:-

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isKeyword(const char *word) {


// List of keywords (add more if needed)
const char *keywords[] = {"if", "else", "for", "while", "return", "int", "float", "char", NULL};
for (int i = 0; keywords[i] != NULL; i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1;
}}
return 0;
}

int isNumber(const char *word) {


int i = 0;
if (word[0] == '\0') return 0;
while (word[i]) {
if (!isdigit(word[i])) return 0;
i++;
}
return 1;
}
int main() {
char input[256];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);

// Tokenize input by space and punctuation


char *delimiters = " \t\n,.!;(){}";
char *token = strtok(input, delimiters);

while (token != NULL) {


if (isKeyword(token)) {
printf("\"%s\" is a keyword\n", token);
} else if (isNumber(token)) {
printf("\"%s\" is a number\n", token);
} else {
printf("\"%s\" is a word\n", token);
}
token = strtok(NULL, delimiters);
}
return 0;
}

OUTPUT:
Experiment 8

Aim:- Write a lex program to identify the capital words from the given input string.
Code:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isCapitalWord(const char *word) {


for (int i = 0; word[i] != '\0'; i++) {
if (!isupper(word[i])) {
return 0; // Not all uppercase letters
}
}
return 1;
}
int main() {
char input[256];
printf("Enter the input string: \n");
fgets(input, sizeof(input), stdin);

// Tokenize input by space and punctuation


char *delimiters = " \t\n,.!;(){}";
char *token = strtok(input, delimiters);

printf("Capital words found:\n");


while (token != NULL) {
if (isCapitalWord(token)) {
printf("%s\n", token);
}
token = strtok(NULL, delimiters);
}

return 0;
}

OUTPUT:
Experiment 9

Aim:- Write a lex program to count the number of vowels and consonants from the sentence.
Code:-
#include <stdio.h>
#include <ctype.h>

int main() {
char input[256];
int v = 0, c = 0;

printf("Enter the input string:\n");


fgets(input, sizeof(input), stdin);

for (int i = 0; input[i] != '\0'; i++) {


char ch = input[i];

if (isalpha(ch)) { // check if alphabet


ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
v++;
} else {
c++;
}
}
}

printf("No of vowels are %d \n", v);


printf("No of consonants are %d \n", c);

return 0;
}

OUTPUT:
Experiment 10

Aim:- Write a lex program to count the number of lines, words, spaces and character from
the statement.
Code:-
#include <stdio.h>
#include <ctype.h>

int main() {
char input[1000];
int lc = 0, sc = 0, wc = 0, cc = 0;
int inWord = 0;

printf("Enter the input (press Enter and Ctrl+D to finish):\n");

while (fgets(input, sizeof(input), stdin)) {


for (int i = 0; input[i] != '\0'; i++) {
char ch = input[i];
cc++;

if (ch == '\n') {
lc++;
}
if (ch == ' ' || ch == '\t') {
sc++;
}

if (isalnum(ch)) {
if (!inWord) {
wc++;
inWord = 1;
}
} else {
inWord = 0;
}
}
}

printf("No. of words are: %d\n", wc);


printf("No. of characters are: %d\n", cc);
printf("No. of new lines are: %d\n", lc);
printf("No. of spaces are: %d\n", sc);

return 0;
}
OUTPUT:

You might also like