0% found this document useful (0 votes)
15 views9 pages

Predictive Parser Table Implementation

The document describes implementing a predictive parser by writing a program to generate a predictive parsing table from a context-free grammar. The program takes the grammar as input, calculates the FIRST and FOLLOW sets, and uses these to populate the predictive parsing table with parsing rules.

Uploaded by

padmanabanm47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Predictive Parser Table Implementation

The document describes implementing a predictive parser by writing a program to generate a predictive parsing table from a context-free grammar. The program takes the grammar as input, calculates the FIRST and FOLLOW sets, and uses these to populate the predictive parsing table with parsing rules.

Uploaded by

padmanabanm47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exp no :-7 1

Date :- 23/03/2022

IMPLEMENTING PREDICTIVE PARSER

AIM
To write a program for implementation of predictive parser table

" ALGORITHM
Start the program.
Initialize the required variables.
Get the number of coordinates and productions from the user.
Perform the following
for (each production Aa in G) {for (each terminal a in FIRST(a)add A->ato
M[A, a];
if (e is in FIRST(a) for (each symbol b in FOLLOWA) add A-> a to M[A, b]; 5.
Print the resulting stack.
Print if the grammar is accepted or not.
Exit the program.

CODE
#include<stdio.h>

#include<conio.h>

#include<string.h>
void main()

char fin[10][20],st[10]20],t(20][20],fol[20]|20]:
int a=0,e,i,t,b,c,n, k,l=0,j,s, m,p;
printf("enter the no. of nonterminals\n");
scanf("%d",&n);
printf("enter the productions in a grammarn");
for(i=0;i<n;i++)
scanf("%s",st();:
for(i=0;i<n;ji++) 2
fol[][0]=\0;
for(s=0;s<n;s++)
{
for(i=0;i<n;i++)

j=3;
I=0;
a=0;
I1:if((st[]Ü]>64)&&(st[]U]<91))

for(m=0;mal;m++)

if(t[l[m]==st(|])
goto s1;

O]=st(i)0):
I=l+1;
s1ijj+1;

else

if(s>0)

while(st[][J!=st[al[0])

a++;

b=0;
while(t[a|[(b]!=10)
3
for(m=0;mel;m++)
{
if(ft(l[m]==ft[a][b])
goto s2;

OJ=f[a][b]:
I=l+1;
s2:b=b+1;

while(st[]liJ!=101)

if(stJU]==1)

jj+1;
goto I1;

j-j+1;

ift[]J=10";

printf("first n");
for(i=0;i<n;ji++)
printf("FIRS[%c]=%s\in",st[][0],f():
fol[0][0]="$';
for(i=0;i<n;i++)

k=0;
j=3;
if(i==0)
l=1;
else

|=0;
ki:while(st[][0]!=st(kJ[I)&&(k<n))

if(st(k]p]==\0)
{
k++;
j=2;
}
j++;

j=j+1;
if(st[)[O]==st[k][j-1)

if(st(kJÚ]!=")&&(st[k]U]!=\0")
{
a=0;
i((st(k|Ü]>64)&&(st(kJi]<91)

for(m=0;m<l;m++)

if(fol[]m]==st(K|Ú1)
goto q3;
fol[i]=st[k]Ü):
I++; 5
q3:;

else

while(st[K]!=st[a|[0)

a++;

p=0;
while(ft(allp]=\0)

if(fta][p]!=@)

for(m=0;mel;m++)

if(fol[][m]==ft[al[pl)
goto q2;

fol[]||]|=ft[a][pl:
|=l+1;

else

e=1;
q2:p++;

if(e==1)
e=0;
goto a1;
6

else

{
a1:c=0;
a=0;
while(st[(kO]!=st[a|[0)

a++:

while(fol[al[c]!=10)&&(stjal[0]!=st][0)

for(m=0;m<l;m++)

if(fol[|[m]=-folla|[c|)
goto q1;

fol[il[l]=fol[al[c];
++;
ql:c++;

}
goto k1;

fol[][]=\0";

printf("follow \n");
for(i=0;i<n;i++)
printf("FOLLOW[%c]=%s\n", st(][o],fol[il); 7
print("n"):
s=0;
for(i=0;i<n;i++)

j=3;
while(st(J[j!=10)

if(st[][-1]==)|l(==3)
{
for(p=0;p<=2;p++)

fin[sl[p]=st(l[pl:

t-j;
for(p=3;(st()[)!=1)&&(st[j]Ü]!=10");p++)

fin[sllp]=st(jJi:
jt+;
)
fin[sl[pl=\0';
if(st([kl==@)

b=0:
a=0;
while(st[a][O]!=st()[0])

a++:
while(fol[a<[b]!=\0")

printf("M[?%c,%c]=%sin",st[i[0],fol[al[b],fin[s]);
8
b++;

else if(!((st[i[t|>64)&&(st[[t|<91)
printf("M[°%c,%c<=%s\n",st()[0],st[[,fin[s]);
else

{
b=0;
a=0;
while(st[a[O]!=st[][3])
{
a++;

while(ft(a[b]!=10")

printi("M[°%c,%c]=%s\n",st[][0],ft[a][b],fin(s]);
b++;

S++;

if(st(U]==)
j++;
OUTPUT

Enter the number of nonterminals : 3

Enter the productions in a grammar


3->LFR|R
L->*R|a
R->L

Computing FIRST

FIRST (s] = ( *a )
FIRST (L]= ( *a )
FIRST [R] = { *a }

Computing FOLLOW

FOLLOw (3] = ($ )
FOLLOW [L] = (=}
FOLLOW [R] = ( $= )

M[3, *]= 3->LFR


M[3, a] = 9->LFR
M[3, *]= ->R
M[3, a)= S->R
M[L, *]= I-*R
M[L, )
a = L-a
M(R, *] =R->L
M[R, a)=R->L

... Program finished with exit code 0


Press ENTER to ex

RESULT
The program is successfully implemented and the table is made

You might also like