ACD9
ACD9
ACD9
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;
stack[top++] = '$';
printf("Stack\t\t\tInput\t\t\tAction\n\n");
while (true) {
ch = buffer[ctr] == '\0' ? '$' : 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");
return false;
}
int main() {
Productions grammar;
initializeProductions(&grammar);
grammar.endChar[0] = 'P';
char input[MAX_INPUT_SIZE];
scanf(" %[^\n]", input);
removeSpaces(input);
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 ;