ACD9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab Assignment 9

Roll no-221IT024
Name-Dilip Sagar M

Q)Code-
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX_PRODUCTIONS 20
#define MAX_INPUT_SIZE 1000
#define MAX_STACK_SIZE 1000

typedef struct {
int size;
char arr[MAX_PRODUCTIONS][20];
char endChar[MAX_PRODUCTIONS];
} Productions;

void initializeProductions(Productions *p) {


p->size = 0;
}

void addProduction(Productions *p, char endChar, const char *production) {


strcpy(p->arr[p->size], production);
p->endChar[p->size] = endChar;
p->size++;
}

void printProduction(const char *production) {


printf("%s", production);
}

void printProductions(const Productions *p) {


for (int i = 0; i < p->size; i++) {
printf("%c -> ", p->endChar[i]);
printProduction(p->arr[i]);
printf("\n");
}
}

void removeSpaces(char* str) {


int i, j;
for (i = 0, j = 0; str[i] != '\0'; i++) {
if (!isspace(str[i]))
str[j++] = str[i];
}
str[j] = '\0';
}

bool shiftReduceParser(const Productions *set, const char *buffer) {


char stack[MAX_STACK_SIZE];
int top = 0, ctr = 0;
char ch;

stack[top++] = '$';

printf("Stack\t\t\tInput\t\t\tAction\n\n");

while (true) {
ch = buffer[ctr] == '\0' ? '$' : buffer[ctr];

printf("%s\t\t\t%s\t\t\t", stack, buffer + ctr);

if (ch == '$') {
if (top == 2 && stack[1] == set->endChar[0]) {
printf("accepted\n");
return true;
} else if (top != 1 || stack[0] != set->endChar[0]) {
printf("rejected\n");
return false;
} else {
printf("accepted\n");
return true;
}
}

stack[top++] = ch;
ctr++;

printf("shift\n");

bool reduced = true;


while (reduced) {
reduced = false;

for (int i = 0; i < set->size; i++) {


int len = strlen(set->arr[i]);
if (top >= len && strncmp(stack + top - len, set->arr[i], len) == 0) {
top -= len;
stack[top++] = set->endChar[i];
stack[top] = '\0';
printf("%s\t\t\t%s\t\t\treduce by %c -> ", stack, buffer + ctr, set->endChar[i]);
printProduction(set->arr[i]);
printf("\n");
reduced = true;
break;
}
}
}
}

return false;
}

int main() {
Productions grammar;
initializeProductions(&grammar);

addProduction(&grammar, 'P', "QR;");


addProduction(&grammar, 'R', "int");
addProduction(&grammar, 'R', "float");
addProduction(&grammar, 'S', "R,id");
addProduction(&grammar, 'S', "id");
addProduction(&grammar, 'S', "(P)");

grammar.endChar[0] = 'P';

printf("\nEnter the string to parse: ");

char input[MAX_INPUT_SIZE];
scanf(" %[^\n]", input);

removeSpaces(input);

int len = strlen(input);

if (len > 0 && input[len - 1] == '$') {


input[len - 1] = '\0'; // Remove the trailing '$'
}

printf("\nGrammar Productions:\n\n");
printProductions(&grammar);
printf("\n\n");

if (shiftReduceParser(&grammar, input)) {
printf("\nString \"%s\" is accepted\n", input);
} else {
printf("\nString \"%s\" is rejected\n\n", input);
}

return 0;
}

output-
For test case 1-
Input : 1) int id , id ;
test case 2)
float id R , id ;

Production Rules : E → E + E | E ∗ E E → id | (E) E → E / E Input : id1+id2*id3 Output :


Grammar are E → E + E | E ∗ E E → id | (E) E → E / E

You might also like