You are on page 1of 6

PART B

(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded at the end of the practical)

Roll. No. 60 Name: Shubhankar vijay kanoje


ClassTE-A-Comp Batch:A3
Date of Experiment:1.02.2022 Date of Submission: 8.02.2022
Grade:

B.1 Software Code written by student:


(Paste your code completed during the 2 hours of practical in the lab here)

#include<stdio.h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(int i=0;i<num;i++){
scanf("%s",production[i]);
}
for(int i=0;i<num;i++){
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];
if(non_terminal==production[i][index]) {
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 && production[i][index]!='|')
index++;
if(production[i][index]!=0) {
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
}

/////////////////////////////////////////////////////

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main(){
int i,z;
char c,ch;
//clrscr();
printf("Enter the no of prooductions:\n");
scanf("%d",&n);
printf("Enter the productions:\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the elemets whose fisrt & follow is to be found:");
scanf("%c",&c);
first(c);
printf("First(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
strcpy(f," ");
//flushall();
m=0;
follow(c);
printf("Follow(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c",&z,&ch);
}while(z==1);
return(0);
}
void first(char c)
{
int k;
if(!isupper(c))
f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$')
follow(a[k][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else first(a[k][2]);
}
}
}
void follow(char c)
{
if(a[0][0]==c)
f[m++]='$';
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]);
}
}
}
}

B.2 Input and Output:


B.3 Observations and learning:
(Students are expected to comment on the output obtained with clear observations and
learning for each task/ sub part assigned)
Problem with Left Recursion:
If a left recursion is present in any grammar then, during parsing in the the syntax analysis
part of compilation there is a chance that the grammar will create infinite loop. This is
because at every time of production of grammar S will produce another S without checking
any condition.
If the compiler would have come to know in advance, that what is the “first character of the
string produced when a production rule is applied”, and comparing it to the current character
or token in the input string it sees, it can wisely take decision on which production rule to
apply.
we need to find FIRST and FOLLOW sets for a given grammar so that the parser can
properly apply the needed rule at the correct position.

B.4 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above
and learning/observation noted in section B.3)
In this experiment,we implement the program to remove left recursion from grammar and
find first and follow of the given grammar

B.5 Question of Curiosity


(To be answered by student based on the practical performed and learning/observations)

1. What is the need of Predictive parser?


Predictive parser is a recursive descent parser, which has the capability to predict which
production is to be used to replace the input string. The predictive parser does not suffer
from backtracking.
To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to the
next input symbols. To make the parser back-tracking free, the predictive parser puts some
constraints on the grammar and accepts only a class of grammar known as LL(k) grammar.

2.Difference between top-down and bottom-up parser?

S.No Top Down Parsing Bottom Up Parsing

It is a parsing strategy that first looks It is a parsing strategy that first looks at
at the highest level of the parse tree the lowest level of the parse tree and
1.
and works down the parse tree by works up the parse tree by using the
using the rules of grammar. rules of grammar.

Top-down parsing attempts to find the Bottom-up parsing can be defined as an


2. left most derivations for an input attempts to reduce the input string to
string. start symbol of a grammar.
In this parsing technique we start In this parsing technique we start
parsing from top (start symbol of parsing from bottom (leaf node of parse
3.
parse tree) to down (the leaf node of tree) to up (the start symbol of parse
parse tree) in top-down manner. tree) in bottom-up manner.

This parsing technique uses Left Most This parsing technique uses Right Most
4.
Derivation. Derivation.

It’s main decision is to select what It’s main decision is to select when to
5. production rule to use in order to use a production rule to reduce the string
construct the string. to get the starting symbol.

3. Why there is a necessity of removing a left recurssion ?


If a left recursion is present in any grammar then, during parsing in the the syntax analysis
part of compilation there is a chance that the grammar will create infinite loop. This is
because at every time of production of grammar S will produce another S without checking
any condition.

You might also like