LOGIC: Read the input string. By using the FIRST AND FOLLOW values. Verify the FIRST of non terminal and insert the production in the FIRST value If we have any @ terms in FIRST then insert the productions in FOLLOW values Constructing the predictive parser table
#include
#include
#include
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"}; char first[7][10]={"abcd","ab","cd","a@","@","c@","@"}; char follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
numr(char c)
{
switch(c)
{
case 'S': return 0;
case 'A': return 1;
case 'B': return 2;
case 'C': return 3;
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case '$': return 4;
}
return(2);
}
void main()
{
int i,j,k;
clrscr();
for(i=0;i<5;i++)
for(j=0;j<6;j++)
strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for the following grammar:\n"); for(i=0;i<7;i++)
printf("%s\n",prod[i]);
printf("\nPredictive parsing table is\n");
fflush(stdin);
for(i=0;i<7;i++)
{
k=strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j]!='@')
strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
}
for(i=0;i<7;i++)
{
if(strlen(pror[i])==1)
{
if(pror[i][0]=='@')
{
k=strlen(follow[i]);
for(j=0;j
INPUT & OUTPUT: The following is the predictive parsing table for the following grammar: S->A A->Bb A->Cd B->aB B->@ C->Cc C->@ Predictive parsing table is ------------------------------------------------------------------ a b c d $ ------------------------------------------------------------------ S S->AS->AS->AS->A ------------------------------------------------------------------ A A->Bb A->BbA->Cd A->Cd ------------------------------------------------------------------ B B->aB B->@ B->@ B->@ ------------------------------------------------------------------ C C->@C->@ C->@ ------------------------------------------------------------------