0% found this document useful (0 votes)
2 views2 pages

assignment2 compiler lab

Uploaded by

Filza Zarqa
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)
2 views2 pages

assignment2 compiler lab

Uploaded by

Filza Zarqa
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

ASSIGNMENT-2

Write a C program to remove left recursion from a given grammar.


Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void removeLeftRecursion(char nonTerminal, char alpha[], char
beta[])
{
char newNonTerminal = nonTerminal + ('a' - 'A');
printf("\nTransformed Grammar:\n");
printf("%c -> %s%c'\n", nonTerminal, beta, newNonTerminal);
printf("%c' -> %s%c' | ε\n", newNonTerminal, alpha,
newNonTerminal);
}
int main()
{
char input[100], nonTerminal, alpha[50], beta[50];
printf("Enter a grammar rule (Format: A -> Aα | β):\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%c -> %[^|] | %[^\n]", &nonTerminal, alpha,
beta);
if (alpha[0] == nonTerminal)
{
removeLeftRecursion(nonTerminal, alpha + 1, beta);
}
else
{
printf("\nNo Left Recursion Detected.\nOriginal Grammar:
%s", input);
}

Zarqa Gulrez(22310237)
return 0;
}
Output:

Zarqa Gulrez(22310237)

You might also like