0% found this document useful (0 votes)
23 views

Rules For Calculating Follow Function-: Experiment No.3 Aim:-Write C Program To Calculate Follow

The document describes a C program to calculate the follow function for grammar productions. The program takes grammar productions as input, finds the FOLLOW of a given non-terminal and outputs its FOLLOW set. It uses three rules - placing $ in FOLLOW(S), FOLLOW(B) = FOLLOW(A) if A → αB, and determining FOLLOW(B) based on First(β) if A → αBβ.

Uploaded by

Lion
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)
23 views

Rules For Calculating Follow Function-: Experiment No.3 Aim:-Write C Program To Calculate Follow

The document describes a C program to calculate the follow function for grammar productions. The program takes grammar productions as input, finds the FOLLOW of a given non-terminal and outputs its FOLLOW set. It uses three rules - placing $ in FOLLOW(S), FOLLOW(B) = FOLLOW(A) if A → αB, and determining FOLLOW(B) based on First(β) if A → αBβ.

Uploaded by

Lion
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
You are on page 1/ 3

EXPERIMENT NO.

3
AIM:- WRITE C PROGRAM TO CALCULATE FOLLOW()

Rules For Calculating Follow Function-

Rule-01:
For the start symbol S, place $ in Follow(S).
Rule-02:
For any production rule A → αB,
Follow(B) = Follow(A)
Rule-03:
For any production rule A → αBβ,
• If ∈ ∉ First(β), then Follow(B) = First(β)
• If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ } ∪ Follow(A)
• ∈ will never appear in the follow function of a non-terminal.

PROGRAM:-

#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{
int i;
int choice;
char c,ch;
printf("Enter the no.of productions: ");
scanf("%d", &n);
printf(" Enter %d productions\nProduction with multiple terms should be give as
separate productions \n", n);
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
// gets(a[i]);
do
{
m=0;
printf("Find FOLLOW of -->");
scanf(" %c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",followResult[i]);
printf(" }\n");
printf("Do you want to continue(Press 1 to continue....)?");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void follow(char c)
{
if(a[0][0]==c)addToResult('$');
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))
//f[m++]=c;
addToResult(c);
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))
//f[m++]=a[k][2];
addToResult(a[k][2]);
else first(a[k][2]);
}
}
}
void addToResult(char c)
{
int i;
for( i=0;i<=m;i++)
if(followResult[i]==c)
return;
followResult[m++]=c;
}
RESULT
:

You might also like