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

Compiler Lab File

The document contains code to implement three natural language processing tasks: 1) Converting integers to their word representations (e.g. 1 to one) 2) Detecting and removing left recursion from context-free grammars 3) Removing left factoring from context-free grammar productions

Uploaded by

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

Compiler Lab File

The document contains code to implement three natural language processing tasks: 1) Converting integers to their word representations (e.g. 1 to one) 2) Detecting and removing left recursion from context-free grammars 3) Removing left factoring from context-free grammar productions

Uploaded by

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

Compiler Design

2020UCO1572

Write a program to convert integer into word string (for example 1=one, 2=two,
3=three....).

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void convert_to_words(char *num)


{
int len = strlen(
num);

if (len == 0)
{
fprintf(stderr, "empty string\n");
return;
}
if (len > 4)
{
fprintf(stderr,
"Length more than 4 is not supported\n");
return;
}

char *single_digits[] = {"zero", "one", "two", "three", "four",


"five", "six", "seven", "eight", "nine"};

char *two_digits[] = {"", "ten", "eleven", "twelve",


"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};

char *tens_multiple[] = {"", "", "twenty",


"thirty", "forty", "fifty",
"sixty", "seventy", "eighty",
"ninety"};

char *tens_power[] = {"hundred", "thousand"};

printf("\n%s: ", num);

if (len == 1)
{
printf("%s\n", single_digits[*num - '0']);
return;
}

while (*num != '\0')


{

if (len >= 3)
{
if (*num - '0' != 0)
{
printf("%s ", single_digits[*num - '0']);
printf("%s ",
tens_power[len - 3]);
}
--len;
}

else
{

if (*num == '1')
{
int sum = *num - '0' + *(num + 1) - '0';
printf("%s\n", two_digits[sum]);
return;
}

else if (*num == '2' && *(num + 1) == '0')


{
printf("twenty\n");
return;
}

else
{
int i = *num - '0';
printf("%s ", i ? tens_multiple[i] : "");
++num;
if (*num != '0')
printf("%s ",
single_digits[*num - '0']);
}
}
++num;
}
}

int main(void)
{
convert_to_words("9923");
convert_to_words("523");
convert_to_words("89");
convert_to_words("8");

return 0;
}

Output:

Write a program to implement the left recursion.

Code:
#include <stdio.h>
#include <string.h>
#define SIZE 10
int main()
{
char non_terminal;
char beta, alpha;
int num;
int i;
char production[10][SIZE];
int index = 3;
printf("Enter Number of Production : ");
scanf("%d", &num);
printf("Enter the grammar as E->E-A :\n");
for (i = 0; i < num; i++)
{
scanf("%s", production[i]);
}
for (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\'|^\n", non_terminal, alpha, non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index = 3;
}
getche();
}

Output:

Write a Program to remove Left Factoring.

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:

You might also like