0% found this document useful (0 votes)
48 views14 pages

Compiler Da2

This document contains the code submitted by Harshil Nanda for their digital assignment 2 in compiler design. The code includes two C++ programs, the first converts a regular expression to a deterministic finite automaton, and the second program finds the first and follow sets of a given context-free grammar. Both programs take input from the user and output the resulting DFA or first/follow sets.

Uploaded by

John Welsh
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)
48 views14 pages

Compiler Da2

This document contains the code submitted by Harshil Nanda for their digital assignment 2 in compiler design. The code includes two C++ programs, the first converts a regular expression to a deterministic finite automaton, and the second program finds the first and follow sets of a given context-free grammar. Both programs take input from the user and output the resulting DFA or first/follow sets.

Uploaded by

John Welsh
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/ 14

21BCE0918 HARSHIL NANDA C1+TC1

DIGITAL ASSIGNMENT 2
BCSE307L
COMPILER DESIGN

Name: Harshil Nanda


Reg No: 21BCE0918
Slot: C1+TC1

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

Q1. Write a C/C++ program to convert regular


expression to DFA (regular expression should
consist of only relational operators).

Code:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STATES 50
#define MAX_SYMBOLS 128

typedef struct {
bool isFinal;
int transitions[MAX_SYMBOLS];
} DFAState;

typedef struct {
DFAState states[MAX_STATES];
int initialState;
} DFA;

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

DFAState createDFAState() {
DFAState state;
state.isFinal = false;
for (int i = 0; i < MAX_SYMBOLS; i++) {
state.transitions[i] = -1;
}
return state;
}

void addTransition(DFAState *state, char symbol, int nextState) {


int index = symbol;
state->transitions[index] = nextState;
}

bool isValidRegex(char *regex) {


int length = strlen(regex);
if (length == 0) {
return false;
}

// Check if the regex contains only valid characters (digits and relational
operators)
for (int i = 0; i < length; i++) {
char symbol = regex[i];
if (!((symbol >= '0' && symbol <= '9') || symbol == '>' || symbol == '<' ||
symbol == '=' || symbol == '!')) {

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

return false;
}
}

return true;
}

void convertRegexToDFA(char *regex, DFA *dfa) {


int length = strlen(regex);
int currentState = 0;
int nextState = 1;

for (int i = 0; i < length; i++) {


char symbol = regex[i];
if (dfa->states[currentState].transitions[symbol] == -1) {
dfa->states[currentState].transitions[symbol] = nextState;
currentState = nextState;
nextState++;
} else {
currentState = dfa->states[currentState].transitions[symbol];
}
}

dfa->states[currentState].isFinal = true;
dfa->initialState = 0;

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

void initializeDFA(DFA *dfa) {


for (int i = 0; i < MAX_STATES; i++) {
dfa->states[i] = createDFAState();
}
dfa->initialState = -1;
}

void printDFA(DFA *dfa) {


printf("DFA:\n");
printf("Initial State: %d\n", dfa->initialState);
for (int i = 0; i < MAX_STATES; i++) {
DFAState state = dfa->states[i];
if (state.isFinal) {
printf("State %d (Final): ", i);
} else {
printf("State %d: ", i);
}
for (int j = 0; j < MAX_SYMBOLS; j++) {
int nextState = state.transitions[j];
if (nextState != -1) {
printf("%c-%d ", j, nextState);
}
}

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

printf("\n");
}
}

int main() {
char regex[100];
DFA dfa;

printf("Enter the regular expression (consisting of digits 0-9 and relational


operators): ");
fgets(regex, sizeof(regex), stdin);
regex[strcspn(regex, "\n")] = '\0'; // remove trailing newline character

if (!isValidRegex(regex)) {
printf("Invalid regular expression.\n");
return 0;
}

initializeDFA(&dfa);
convertRegexToDFA(regex, &dfa);
printDFA(&dfa);

return 0;
}

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

Output:

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

Q2. Write a C/C++ program to find the first and


follow of a given grammar.

Code:

#include <iostream>
#include <string.h>
#define max 20
using namespace std;

char prod[max][10];
char ter[10], nt[10];
char first[10][10], follow[10][10];
int eps[10];
int count_var = 0;

int findpos(char ch) {


int n;
for (n = 0; nt[n] != '\0'; n++)
if (nt[n] == ch)
break;
if (nt[n] == '\0')
return 1;
return n;

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

int IsCap(char c) {
if (c >= 'A' && c <= 'Z')
return 1;
return 0;
}

void add(char* arr, char c) {


int i, flag = 0;
for (i = 0; arr[i] != '\0'; i++) {
if (arr[i] == c) {
flag = 1;
break;
}
}
if (flag != 1)
arr[strlen(arr)] = c;
}

void addarr(char* s1, char* s2) {


int i, j, flag = 99;
for (i = 0; s2[i] != '\0'; i++) {
flag = 0;
for (j = 0;; j++) {

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

if (s2[i] == s1[j]) {
flag = 1;
break;
}
if (j == strlen(s1) && flag != 1) {
s1[strlen(s1)] = s2[i];
break;
}
}
}
}

void addprod(char* s) {
int i;
prod[count_var][0] = s[0];
for (i = 3; s[i] != '\0'; i++) {
if (!IsCap(s[i]))
add(ter, s[i]);
prod[count_var][i - 2] = s[i];
}
prod[count_var][i - 2] = '\0';
add(nt, s[0]);
count_var++;
}

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

void findfirst() {
int i, j, n, k, e, n1;
for (i = 0; i < count_var; i++) {
for (j = 0; j < count_var; j++) {
n = findpos(prod[j][0]);
if (prod[j][1] == (char)238)
eps[n] = 1;
else {
for (k = 1, e = 1; prod[j][k] != '\0' && e == 1; k++) {
if (!IsCap(prod[j][k])) {
e = 0;
add(first[n], prod[j][k]);
} else {
n1 = findpos(prod[j][k]);
addarr(first[n], first[n1]);
if (eps[n1] == 0)
e = 0;
}
}
if (e == 1)
eps[n] = 1;
}
}
}
}

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

void findfollow() {
int i, j, k, n, e, n1;
n = findpos(prod[0][0]);
add(follow[n], '#');
for (i = 0; i < count_var; i++) {
for (j = 0; j < count_var; j++) {
k = strlen(prod[j]) - 1;
for (; k > 0; k--) {
if (IsCap(prod[j][k])) {
n = findpos(prod[j][k]);
if (prod[j][k + 1] == '\0') {
n1 = findpos(prod[j][0]);
addarr(follow[n], follow[n1]);
}
if (IsCap(prod[j][k + 1])) {
n1 = findpos(prod[j][k + 1]);
addarr(follow[n], first[n1]);
if (eps[n1] == 1) {
n1 = findpos(prod[j][0]);
addarr(follow[n], follow[n1]);
}
} else if (prod[j][k + 1] != '\0')
add(follow[n], prod[j][k + 1]);
}

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

}
}
}
}

int main() {
char s[max], i;
char NT[10], T[10];
int no_NT, no_T;
cout << "Enter Number of non-terminals in the grammar:\n";
cin >> no_NT;
cout << "Enter Number of terminals in the grammar: \n";
cin >> no_T;
cout << "Enter List of non-terminals (separated by one whitespace):\n";
for (int j = 0; j < no_NT; ++j) {
cin >> NT;
}
cout << "Enter List of terminals (separated by one whitespace):\n";
for (int h = 0; h < no_T; ++h) {
cin >> T;
}
cout << "Production rules of the grammar\n";
cin >> s;
while (strcmp("end", s)) {
addprod(s);

21BCE0918 HARSHIL NANDA C1+TC1


21BCE0918 HARSHIL NANDA C1+TC1

cin >> s;
}
findfirst();
findfollow();
for (i = 0; i < strlen(nt); i++) {
cout << nt[i] << "\t";
cout << first[i];
if (eps[i] == 1)
cout << ((char)238) << "\t";
else
cout << "\t";
cout << follow[i] << "\n";
}
return 0;
}

Output:

21BCE0918 HARSHIL NANDA C1+TC1

You might also like