Compiler Da2
Compiler Da2
DIGITAL ASSIGNMENT 2
BCSE307L
COMPILER DESIGN
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;
DFAState createDFAState() {
DFAState state;
state.isFinal = false;
for (int i = 0; i < MAX_SYMBOLS; i++) {
state.transitions[i] = -1;
}
return state;
}
// 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 == '!')) {
return false;
}
}
return true;
}
dfa->states[currentState].isFinal = true;
dfa->initialState = 0;
printf("\n");
}
}
int main() {
char regex[100];
DFA dfa;
if (!isValidRegex(regex)) {
printf("Invalid regular expression.\n");
return 0;
}
initializeDFA(&dfa);
convertRegexToDFA(regex, &dfa);
printDFA(&dfa);
return 0;
}
Output:
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 IsCap(char c) {
if (c >= 'A' && c <= 'Z')
return 1;
return 0;
}
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++;
}
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;
}
}
}
}
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]);
}
}
}
}
}
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);
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: