100% found this document useful (1 vote)
2K views

Remove Left Factoring

The program performs left factoring on a context-free grammar. It takes the grammar as input from the user, separates the left and right sides of the first production rule into strings b and c, finds the common prefix f between them, and outputs a factored grammar with productions S-fD and D-g/h. Left factoring is a process to transform a grammar from a left-recursive to a non-left-recursive form. It factors out common prefixes from multiple production rules. Left recursion is a property where a variable can derive a right-hand side starting with the same variable in one or more steps.

Uploaded by

sushil.singh.c
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
100% found this document useful (1 vote)
2K views

Remove Left Factoring

The program performs left factoring on a context-free grammar. It takes the grammar as input from the user, separates the left and right sides of the first production rule into strings b and c, finds the common prefix f between them, and outputs a factored grammar with productions S-fD and D-g/h. Left factoring is a process to transform a grammar from a left-recursive to a non-left-recursive form. It factors out common prefixes from multiple production rules. Left recursion is a property where a variable can derive a right-hand side starting with the same variable in one or more steps.

Uploaded by

sushil.singh.c
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/ 2

Ques- Write a program to remove left factoring?

CODE :

#include<stdio.h>
void main()
{
char a[100],b[100],c[100],f[100],g[100],h[100];
int i,n=0,j;
printf("Enter the grammar\n");
gets(a);

i=2;
j=0;
while(a[i]!='/')
{
b[j]=a[i];
i++;
j++;
}
b[j]='\0';
i++;
while(a[i]!='\0')
{
c[n]=a[i];
n++;
i++;
}
c[n]='\0';
i=0;
while(b[i]==c[i])
{f[i]=b[i];
i++;
}
f[i]='\0';
n=0;
j=i;
while(b[i]!='\0')
{ g[n]=b[i];
n++;
i++;
}

g[n]='\0';
n=0;
while(c[j]!='\0')
{ h[n]=c[j];
n++;
j++;
}

h[n]='\0';
printf("S-%sD\n",f);
printf("D-%s/%s/e\n",g,h);
}

VIVA Questions

Ques 1: What is left factoring?


Ans : A process of transformation, turning the grammar from a left-recursive form to an equivalent
non-left-recursive form.

Ques 2: Difference between left factoring and left recursion?


Ans : Left Factoring is a grammar transformation technique. It consists in "factoring out" prefixes
which are common to two or more productions.
Left Recursion is a property a grammar has whenever you can derive from a given variable (non
terminal) a RHS that begins with the same variable, in one or more steps.

OUTPUT:

You might also like