0% found this document useful (0 votes)
6 views4 pages

CD2 Ak71

The document outlines an experiment for computing the Follow set of a context-free grammar (CFG) at Medi-Caps University. It explains the theory behind Follow sets, their significance in parsing, and provides a C program to calculate the Follow set for given productions. The program includes functions for calculating Follow and First sets, as well as handling user input for multiple productions.
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)
6 views4 pages

CD2 Ak71

The document outlines an experiment for computing the Follow set of a context-free grammar (CFG) at Medi-Caps University. It explains the theory behind Follow sets, their significance in parsing, and provides a C program to calculate the Follow set for given productions. The program includes functions for calculating Follow and First sets, as well as handling user input for multiple productions.
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/ 4

YEAR: 2024-25

MEDI-CAPS UNIVERSITY, INDORE


SEM: EVEN

Experiment No. 5
Aim: Write a program to compute follow of a CFG.
Theory:
The Follow set of a non-terminal contains all terminals that can immediately follow it in any derivation of
the grammar. This means that if a non-terminal appears in the middle of a sentence, the Follow set tells us
what symbol might come right after it. For example, in a rule like S -> AB, the Follow set of A might include
anything that can follow B, depending on other rules in the grammar. Follow sets are especially useful for
parsers when making decisions about which rule to apply next and ensuring that we’re interpreting the
structure of a sentence correctly, especially for LL(1) parsers where we only look one symbol ahead..
The follow set of the start symbol will always contain “$”. Now the calculation of Follow falls under three
broad cases:
i. If a Non-Terminal on the R.H.S. of any production is followed immediately by a Terminal then it
can immediately be included in the Follow set of that Non-Terminal.
ii. If a Non-Terminal on the R.H.S. of any production is followed immediately by a Non-Terminal, then
the First Set of that new Non-Terminal gets included on the follow set of our original Non-Terminal.
In case encountered an epsilon i.e. ” # ” then, move on to the next symbol in the production.
Note: “#” is never included in the Follow set of any Non-Terminal.
iii. If reached the end of a production while calculating follow, then the Follow set of that non-terminal
will include the Follow set of the Non-Terminal on the L.H.S. of that production. This can easily be
implemented by recursion.

Code:
#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;

Aditya Tiwari CSE 6B EN22CS301071


YEAR: 2024-25
MEDI-CAPS UNIVERSITY, INDORE
SEM: EVEN

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++)
{

Aditya Tiwari CSE 6B EN22CS301071


YEAR: 2024-25
MEDI-CAPS UNIVERSITY, INDORE
SEM: EVEN

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]);
}
}

Aditya Tiwari CSE 6B EN22CS301071


YEAR: 2024-25
MEDI-CAPS UNIVERSITY, INDORE
SEM: EVEN

}
void addToResult(char c)
{
int i;
for( i=0;i<=m;i++)
if(followResult[i]==c)
return;
followResult[m++]=c;
}

Output:

Aditya Tiwari CSE 6B EN22CS301071

You might also like