Faculty of Engineering & T echnology
Subject -N ame: compiler design Laboratory
Subject -C ode:303105350
B.Tech – 3r d Year – 6t h Sem
Practical - 4
Aim :- Program to check validation of UserName and Password in C.
Code :-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
#define EPSILON "e"
// Function to check if the production is left-recursive
int isLeftRecursive(char *production) {
return production[0] == production[3];
}
// Function to remove left recursion
void removeLeftRecursion(char *production) {
char nonTerminal;
char alpha[MAX_SIZE], beta[MAX_SIZE];
int i = 3, j = 0, k = 0;
nonTerminal = production[0];
if (isLeftRecursive(production)) {
i = 4;
while (production[i] != '|' && production[i] != '\0') {
alpha[j++] = production[i++];
}
alpha[j] = '\0';
if (production[i] == '|') {
i++;
while (production[i] != '\0') {
beta[k++] = production[i++];
}
}
beta[k] = '\0';
printf("The grammar has left recursion.\n");
printf("Remove Left Recursion:\n");
printf("%c -> %s%c'\n", nonTerminal, beta, nonTerminal);
printf("%c' -> %s%c' | %s\n", nonTerminal, alpha, nonTerminal, EPSILON);
} else {
printf("The grammar is not left-recursive.\n");
printf("Production: %s\n", production);
}
}
Enrollment no :- 2203031050516 Pg no. 7
Faculty of Engineering & T echnology
Subject -N ame: compiler design Laboratory
Subject -C ode:303105350
B.Tech – 3r d Year – 6t h Sem
// Function to find the First set
void findFirst(char *production, char *firstSet) {
char nonTerminal = production[0];
int i = 3, k = 0;
printf("\nFinding First(%c):\n", nonTerminal);
while (production[i] != '\0') {
if (production[i] == '|') {
i++;
continue;
}
if (!isupper(production[i]) && production[i] != '|') {
if (!strchr(firstSet, production[i])) {
firstSet[k++] = production[i];
}
} else if (isupper(production[i])) {
printf("Handling non-terminal in First(%c).\n", production[i]);
}
while (production[i] != '|' && production[i] != '\0') {
i++;
}
}
firstSet[k] = '\0';
printf("First(%c) = { ", nonTerminal);
for (int j = 0; j < k; j++) {
printf("%c", firstSet[j]);
if (j < k - 1) printf(", ");
}
printf(" }\n");
}
// Function to find the Follow set
void findFollow(char *production, char *followSet) {
char nonTerminal = production[0];
// Add '$' to follow set if the non-terminal is 'S'
if (nonTerminal == 'S' && !strchr(followSet, '$')) {
int len = strlen(followSet);
followSet[len] = '$';
followSet[len + 1] = '\0';
}
for (int i = 3; production[i] != '\0'; i++) {
if (production[i] == nonTerminal && production[i + 1] != '\0') {
if (!isupper(production[i + 1]) && production[i + 1] != '|') {
// Only add if it's not a non-terminal or '|'
if (!strchr(followSet, production[i + 1])) {
int len = strlen(followSet);
followSet[len] = production[i + 1];
followSet[len + 1] = '\0';
Enrollment no :- 2203031050516 Pg no. 8
Faculty of Engineering & T echnology
Subject -N ame: compiler design Laboratory
Subject -C ode:303105350
B.Tech – 3r d Year – 6t h Sem
}
}
}
}
printf("Follow(%c) = { ", nonTerminal);
for (int j = 0; j < strlen(followSet); j++) {
printf("%c", followSet[j]);
if (j < strlen(followSet) - 1) {
printf(", ");
}
}
printf(" }\n");
}
// Function to generate the parse table
void generateParseTable(char *production, char *firstSet, char *followSet) {
printf("\nParse Table:\n");
printf("Non-Terminal | Input Symbol | Production\n");
printf("----------------------------------------\n");
char nonTerminal = production[0];
for (int i = 0; i < strlen(firstSet); i++) {
if (firstSet[i] != EPSILON[0]) {
printf(" %c | %c | %s \n", nonTerminal, firstSet[i], production);
}
}
if (strchr(firstSet, EPSILON[0])) {
for (int i = 0; i < strlen(followSet); i++) {
printf(" %c | %c | %s \n", nonTerminal, followSet[i], EPSILON);
}
}
}
int main() {
char production[MAX_SIZE], firstSet[MAX_SIZE] = "", followSet[MAX_SIZE] = "";
printf("Enter the production : ");
scanf("%s", production);
if (strlen(production) < 4 || production[1] != '-' || production[2] != '>') {
printf("Invalid production format. Please use the format A->Aa|b.\n");
return 1;
}
removeLeftRecursion(production);
findFirst(production, firstSet);
findFollow(production, followSet);
generateParseTable(production, firstSet, followSet);
return 0;
}
Enrollment no :- 2203031050516 Pg no. 9
Faculty of Engineering & T echnology
Subject -N ame: compiler design Laboratory
Subject -C ode:303105350
B.Tech – 3r d Year – 6t h Sem
Output :-
Enrollment no :- 2203031050516 Pg no. 10