Thursday 1 March 2012

CONSTRUCTION OF PREDICTIVE PARSER TABLE USING C


CONSTRUCTION OF PREDICTIVE PARSER TABLE USING C
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char fin[10][20],st[10][20],ft[20][20],fol[20][20];
int a=0,e,i,t,b,c,n,k,l=0,j,s,m,p;
clrscr();
printf("enter the no. of coordinates\n");
scanf("%d",&n);
printf("enter the productions in a grammar\n");
for(i=0;i<n;i++)
scanf("%s",st[i]);
for(i=0;i<n;i++)
fol[i][0]='\0';
for(s=0;s<n;s++)
{
for(i=0;i<n;i++)
{
j=3;
l=0;
a=0;
l1:if(!((st[i][j]>64)&&(st[i][j]<91)))
{
for(m=0;m<l;m++)
{
if(ft[i][m]==st[i][j])
goto s1;
}
ft[i][l]=st[i][j];
l=l+1;
s1:j=j+1;
}
else
{
if(s>0)
{
while(st[i][j]!=st[a][0])
{
a++;
}
b=0;
while(ft[a][b]!='\0')
{
for(m=0;m<l;m++)
{
if(ft[i][m]==ft[a][b])
goto s2;
}
ft[i][l]=ft[a][b];
l=l+1;
s2:b=b+1;
}
}
}
while(st[i][j]!='\0')
{
if(st[i][j]=='|')
{
j=j+1;
goto l1;
}
j=j+1;
}
ft[i][l]='\0';
}
}
printf("first pos\n");
for(i=0;i<n;i++)
printf("FIRS[%c]=%s\n",st[i][0],ft[i]);
fol[0][0]='$';
for(i=0;i<n;i++)
{
k=0;
j=3;
if(i==0)
l=1;
else
l=0;
k1:while((st[i][0]!=st[k][j])&&(k<n))
{
if(st[k][j]=='\0')
{
k++;
j=2;
}
j++;
}
j=j+1;
if(st[i][0]==st[k][j-1])
{
if((st[k][j]!='|')&&(st[k][j]!='\0'))
{
a=0;
if(!((st[k][j]>64)&&(st[k][j]<91)))
{
for(m=0;m<l;m++)
{
if(fol[i][m]==st[k][j])
goto q3;
}
fol[i][l]=st[k][j];
l++;
q3:
}
else
{
while(st[k][j]!=st[a][0])
{
a++;
}
p=0;
while(ft[a][p]!='\0')
{
if(ft[a][p]!='@')
{
for(m=0;m<l;m++)
{
if(fol[i][m]==ft[a][p])
goto q2;
}
fol[i][l]=ft[a][p];
l=l+1;
}
else
e=1;
q2:p++;
}
if(e==1)
{
e=0;
goto a1;
}
}
}
else
{
a1:c=0;
a=0;
while(st[k][0]!=st[a][0])
{
a++;
}
while((fol[a][c]!='\0')&&(st[a][0]!=st[i][0]))
{
for(m=0;m<l;m++)
{
if(fol[i][m]==fol[a][c])
goto q1;
}
fol[i][l]=fol[a][c];
l++;
q1:c++;
}
}
goto k1;
}
fol[i][l]='\0';
}
printf("follow pos\n");
for(i=0;i<n;i++)
printf("FOLLOW[%c]=%s\n",st[i][0],fol[i]);
printf("\n");
s=0;
for(i=0;i<n;i++)
{
j=3;
while(st[i][j]!='\0')
{
if((st[i][j-1]=='|')||(j==3))
{
for(p=0;p<=2;p++)
{
fin[s][p]=st[i][p];
}
t=j;
for(p=3;((st[i][j]!='|')&&(st[i][j]!='\0'));p++)
{
fin[s][p]=st[i][j];
j++;
}
fin[s][p]='\0';
if(st[i][k]=='@')
{
b=0;
a=0;
while(st[a][0]!=st[i][0])
{
a++;
}
while(fol[a][b]!='\0')
{
printf("M[%c,%c]=%s\n",st[i][0],fol[a][b],fin[s]);
b++;
}
}
else if(!((st[i][t]>64)&&(st[i][t]<91)))
printf("M[%c,%c]=%s\n",st[i][0],st[i][t],fin[s]);
else
{
b=0;
a=0;
while(st[a][0]!=st[i][3])
{
a++;
}
while(ft[a][b]!='\0')
{
printf("M[%c,%c]=%s\n",st[i][0],ft[a][b],fin[s]);
b++;
}
}
s++;
}
if(st[i][j]=='|')
j++;
}
}
getch();
}

OutPut:
Enter the no. of co-ordinates
2
Enter the productions in a grammar
S->CC
C->eC | d
First pos
FIRS[S] = ed
FIRS[C] = ed

Follow pos
FOLLOW[S] =$
FOLLOW[C] =ed$

M [S , e] =S->CC
M [S , d] =S->CC
M [C , e] =C->eC
M [C , d] =C->d

34 comments:

  1. Thank you so much sir for the program it is really really very helpful as iwas unable to complete it on my own ...it was really really very helpful...i have a few doubts i would be really grateful if you can clear them by the earliest.than you once again sir

    ReplyDelete
    Replies
    1. Thanks for your feedback send me your bug coding i will clear it...........

      Delete
  2. thank you so much brother....

    ReplyDelete
  3. sir thanks for the program! but there are few errors..need some help

    ReplyDelete
  4. This program was a great help...thankyou...

    ReplyDelete
  5. what is the input for NULL production like X->NULL

    ReplyDelete
  6. follow answer seems not correct

    ReplyDelete
  7. give me the stack implementation along with table

    ReplyDelete
  8. Thnx Sir,it is great,and i underantand the working of First set and Follow set.but please clearify what is the last set in your output section?
    i mean what you write after the follow section.is it the select operation or M table or the parsing table.

    ReplyDelete
  9. Or one more question sir ,what to take for the null production.please answer my both questions sir..Thankyou Sir

    ReplyDelete
  10. Hello sir, i have tried your code with below input but program calculates first and then system hangs what to do?

    generate Predictive Parsing Table for the given grammar.
    S -> ABE | a | f
    A -> p | t | w
    B -> Aq

    thanks in advance....

    ReplyDelete
  11. please explain the program execution and why s is used in program,

    ReplyDelete
  12. If error occors in line 105 i.e q3: then add z=0;. It looks like " q3: z=0; ". Above "q3: z=0; " write "int z;" and run. Hope it helps.

    ReplyDelete
  13. what is after q3: label?? In line number 102.
    please reply.

    ReplyDelete
  14. how to enter E->e where e denote empty production?

    ReplyDelete
  15. How to denote an empty production, i mean wat is the symbol used to indicate epsilon..

    ReplyDelete
  16. Pls explain this program. I am not able to understand this program correctly

    ReplyDelete
  17. plz give me mean of fin,fol,st,ft.....<3

    ReplyDelete
  18. not clearly understandable

    ReplyDelete
  19. Those who r using this program use @ for epsilon and also correct the if condition, if(st[i][k]) as if(st[i][t])(line no.37 from below).

    Thanks to the creater of this program.

    ReplyDelete
  20. please give the output in the form of a table...

    ReplyDelete
  21. please give me the output in the form of table because the input of compiler driver is in the form form table. please give me solution.

    for more information give me your contact number.

    ReplyDelete
  22. What is q3 label definition in the program

    ReplyDelete
  23. Hello sir
    Getting "compilation terminated" error...at the line #include
    please solve it sir

    ReplyDelete
  24. their is a one error on line no 105

    ReplyDelete