CD_LAB_10
CD_LAB_10
/*Description:
A shift-reduce parser is a type of bottom-up parser used in syntax analysis within
compilers. It processes an input string by shifting symbols onto a stack and
reducing sequences of symbols to grammar rules. The parser operates in two main
phases:
The goal is to reduce the entire input string to the start symbol of the grammar,
indicating that the string is syntactically correct. If the parser successfully
reduces the input to the start symbol, it accepts the string; otherwise, it reports
a syntax error.
*/
/*Source Code: */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Global Variables
int z = 0, i = 0, j = 0, c = 0;
// This function will check whether the stack contains a production rule to be
reduced.
// Rules can be E -> 2E2, E -> 3E3, E -> 4
void check() {
// Copying string to be printed as action
strcpy(ac, "REDUCE TO E -> ");
// Driver Function
int main() {
printf("GRAMMAR is -\nE -> 2E2 \nE -> 3E3 \nE -> 4\n");
// a is input string
strcpy(a, "32423");
// Printing action
printf("\n$%s\t%s$\t", stk, a);
// Call check function which will check the stack whether it contains any
production or not
check();
}
// If the top of the stack is E (starting symbol), then it will accept the
input
if (stk[0] == 'E' && stk[1] == '\0') {
printf("Accept\n");
} else { // Else reject
printf("Reject\n");
}
return 0;
}
/*Output:
GRAMMAR is -
E -> 2E2
E -> 3E3
E -> 4