CD_LAB_8
CD_LAB_8
Parser*/
/*Description:
A predictive parser is a type of top-down parser used in the process of syntax
analysis within compilers. It constructs the parse tree for a given input string by
predicting which grammar rules to apply based on the current input symbol and the
current state of the parser. The parser uses a lookahead symbol (typically one or
more tokens) to make decisions about which production rules to use. Predictive
parsers rely on a precomputed parsing table that contains information about which
grammar rules to apply for specific combinations of input symbols and parser
states. They are efficient and avoid backtracking, making them suitable for real-
time parsing applications.
*/
/*Source Code: */
#include <stdio.h>
#include <string.h>
char a[10];
int top = -1, i;
void error() {
printf("Syntax Error");
}
char TOS() {
/* Returns TOP of the Stack */
return a[top];
}
void pop() {
/* Pops 1 element from the Stack */
if (top >= 0)
a[top--] = '\0';
}
void display() {
/* Displays Elements Of Stack */
for (i = 0; i <= top; i++)
printf("%c", a[i]);
}
int main() {
char ip[20], r[20], st, an;
int ir, ic, j = 0, k;
char t[5][6][10] = {
"$", "$", "TH", "$", "TH", "$",
"+TH", "$", "e", "e", "$", "e",
"$", "$", "FU", "$", "FU", "$",
"e", "*FU", "e", "e", "$", "e",
"$", "$", "(E)", "$", "i", "$"
};
strcpy(r, strrev(t[ir][ic]));
strrev(t[ir][ic]);
pop();
push(r);
if (TOS() == 'e') {
pop();
display();
display1(ip, j);
printf("\t%c->%c\n", st, 238);
} else {
display();
display1(ip, j);
printf("\t%c->%s\n", st, t[ir][ic]);
}
if (TOS() == '$' && an == '$')
break;
if (TOS() == '$') {
error();
break;
}
}
k = strcmp(stack(), "$");
if (k == 0)
printf("\n Given String is accepted");
else
printf("\n Given String is not accepted");
return 0;
}
/*
Output:
$E i+i$
$HT i+i$ E->TH
$HUF i+i$ T->FU
$HUi i+i$ F->i
$HU +i$ POP
$H +i$ U->ε
$HT+ +i$ H->+TH
$HT i$ POP
$HUF i$ T->FU
$HUi i$ F->i
$HU $ POP
$H $ U->ε
$ $ H->ε