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

Compiler Lab A

The document discusses a C program that identifies and removes left factoring from a given grammar. It explains left factoring and its importance in allowing grammars to be accepted by LL(1) parsers, which is useful for syntax analysis. The program takes a production as input, separates it using pipes, finds the common prefix and generates new productions with one having the prefix as leftmost symbol and another replacing it.

Uploaded by

ashutosh02basak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Compiler Lab A

The document discusses a C program that identifies and removes left factoring from a given grammar. It explains left factoring and its importance in allowing grammars to be accepted by LL(1) parsers, which is useful for syntax analysis. The program takes a production as input, separates it using pipes, finds the common prefix and generates new productions with one having the prefix as leftmost symbol and another replacing it.

Uploaded by

ashutosh02basak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Aishika Pal

Roll No:225
Enrollment No:12021002004060
Subject Code: PCCCS691
Subject Name: Compiler Design
Assignment No: 6
Date: 15.03.24
Assignment-06
Program Title:
Write a C program to identify and remove left factoring from a given grammar.

Objective: To understand how the syntax analyzer detects and removes left factoring from a
given CFG while carrying out parsing.
Resources: Linux terminal, text editor, gcc compiler.

Source Code:
#include<stdio.h>
#include<string.h>
int main()
{
char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,l=0,pos;
printf("Enter Production : A->");
gets(gram);
for(i=0;gram[i]!='|';i++,j++)
part1[j]=gram[i];
part1[j]='\0';
for(j=++i,i=0;gram[j]!='\0';j++,i++)
part2[i]=gram[j];
part2[i]='\0';
for(i=0;i<strlen(part1)||i<strlen(part2);i++)
{
if(part1[i]==part2[i])
{
modifiedGram[k]=part1[i];
k++;
pos=i+1;
}
}
for(i=pos,j=0;part1[i]!='\0';i++,j++){
newGram[j]=part1[i];
}
newGram[j++]='|';
for(i=pos;part2[i]!='\0';i++,j++){
newGram[j]=part2[i];
}
modifiedGram[k]='X';
modifiedGram[++k]='\0';
newGram[j]='\0';
printf("\n A->%s",modifiedGram);
printf("\n X->%s\n",newGram);
}
Output:

Discussion:
In this question we learnt how to identify and remove left factoring from a set of production
rules for a given grammar. Removal of left factoring is important because it allows the
grammar to be accepted by an LL(1) parser. This is later useful in syntax analysis.

You might also like