0% found this document useful (0 votes)
8 views4 pages

Exp 8

The document presents a C program designed to calculate leading sets for non-terminals in a given grammar. It includes a source code that processes production rules and outputs a table of leading sets. The program utilizes a stack to manage the processing of grammar symbols and displays the results in a formatted manner.

Uploaded by

garisharmaa13
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)
8 views4 pages

Exp 8

The document presents a C program designed to calculate leading sets for non-terminals in a given grammar. It includes a source code that processes production rules and outputs a table of leading sets. The program utilizes a stack to manage the processing of grammar symbols and displays the results in a formatted manner.

Uploaded by

garisharmaa13
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/ 4

Experiment No: 8

Aim: Program to calculate leading for all the non-terminals of the


given grammar.
Source Code:
#include <stdio.h>

char arr[18][3] = {
{'E', '+', 'F'}, {'E', '*', 'F'}, {'E', '(', 'F'}, {'E', ')', 'F'}, {'E', '1', 'F'}, {'E', '$', 'F'},
{'T', '+', 'F'}, {'T', '*', 'F'}, {'T', '(', 'F'}, {'T', ')', 'F'}, {'T', '1', 'F'}, {'T', '$', 'F'},
{'F', '+', 'F'}, {'F', '*', 'F'}, {'F', '(', 'F'}, {'F', ')', 'F'}, {'F', '1', 'F'}, {'F', '$', 'F'}
};

char prod[6] = "EETTFF";

char res[6][3] = {
{'E', '+', 'F'}, {'T', '*', '0'}, {'T', '(', '0'},
{'(', 'E', ')'}, {'T', '1', '0'}, {'F', '1', '0'}
};

char stack[5][2];
int top = -1;

void install(char pro, char re) {


int i;
for (i = 0; i < 18; ++i) {
if (arr[i][0] == pro && arr[i][1] == re) {
arr[i][2] = 'T';
break;
}
}
++top;
stack[top][0] = pro;
stack[top][1] = re;
}

int main() {
int i, j;
char pro, re, pri = ' ';

for (i = 0; i < 6; ++i) {


for (j = 0; j < 3 && res[i][j] != '\0'; ++j) {
if (res[i][j] == '+' || res[i][j] == '*' || res[i][j] == '(' ||
res[i][j] == ')' || res[i][j] == '1' || res[i][j] == '$') {
install(prod[i], res[i][j]);
break;
}
}
}

while (top >= 0) {


pro = stack[top][0];
re = stack[top][1];
--top;

for (i = 0; i < 6; ++i) {


if (res[i][0] == pro && res[i][0] != prod[i]) {
install(prod[i], re);
}
}
}

printf("Processed Table:\n");
for (i = 0; i < 18; ++i) {
for (j = 0; j < 3; ++j)
printf("%c\t", arr[i][j]);
printf("\n");
}

printf("\nLEADING sets:\n");
for (i = 0; i < 18; ++i) {
if (pri != arr[i][0]) {
pri = arr[i][0];
printf("\n%c -> ", pri);
}
if (arr[i][2] == 'T')
printf("%c ", arr[i][1]);
}

return 0;
}
Output:

You might also like