LAB5
LAB5
Design operator precedence parser along with parsing table and also
find leading and trailing for the same
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
int searchNT(char a)
{
int count = -1, i;
for (i = 0; i < nt; i++)
{
if (NT[i] == a)
return i;
}
return count;
}
int searchTER(char a)
{
int count = -1, i;
for (i = 0; i < t; i++)
{
if (T[i] == a)
return i;
}
return count;
}
void push(char a)
{
s[Top] =
a;
Top++;
}
char pop()
{
Top--;
return s[Top];
}
/* */
char *input;
int i = 0;
char lasthandle[6], stack[50], handles[][5] = {")E(", "E*E", "E+E", "i", "E^E"};
int top = 0, l;
char prec[9][9] = {
/* input */
/* stack + - * / ^ i ( ) $ */
/* + */ '>', '>', '<', '<', '<', '<', '<', '>', '>',
/* - */ '>', '>', '<', '<', '<', '<', '<', '>', '>',
/* * */ '>', '>', '>', '>', '<', '<', '<', '>', '>',
/* / */ '>', '>', '>', '>', '<', '<', '<', '>', '>',
/* ^ */ '>', '>', '>', '>', '<', '<', '<', '>', '>',
/* i */ '>', '>', '>', '>', '>', 'e', 'e', '>', '>',
/* ( */ '<', '<', '<', '<', '<', '<', '<', '>', 'e',
/* ) */ '>', '>', '>', '>', '>', 'e', 'e', '>', '>',
/* $ */ '<', '<', '<', '<', '<', '<', '<', '<', '>',
};
int getindex(char c)
{
switch (c)
{
case '+':
return 0;
case '-':
return 1;
case '*':
return 2;
case '/':
return 3;
case '^':
return 4;
case 'i':
return 5;
case '(':
return 6;
case ')':
return 7;
case '$':
return 8;
default:
return -1;
}
}
int shift()
{
stack[++top] = *(input + i++);
stack[top + 1] = '\0';
}
int reduce()
{
int i, len, found, t;
for (i = 0; i < 5; i++) // selecting handles
{
len = strlen(handles[i]);
if (stack[top] == handles[i][0] && top + 1 >= len)
{
found = 1;
for (t = 0; t < len; t++)
{
if (stack[top - t] != handles[i][t])
{
found = 0;
break;
}
}
if (found == 1)
{
stack[top - t + 1] = 'E';
top = top - t + 1;
strcpy(lasthandle, handles[i]);
stack[top + 1] = '\0';
return 1; // successful reduction
}
}
}
return 0;
}
void dispstack()
{
int j;
for (j = 0; j <= top; j++)
printf("%c", stack[j]);
}
void dispinput()
{
int j;
for (j = i; j < l; j++)
printf("%c", *(input + j));
}
int searchter(char a)
{
int count = -1, i;
char T[] = "+-*/^i()$";
for (i = 0; T[i] != '$'; i++)
{
if (T[i] == a)
return i;
}
return count;
}
int searchnt(char a)
{
int count = -1, i;
char NT[] = "E";
for (i = 0; NT[i] != '\0'; i++)
{
if (NT[i] == a)
return i;
}
return count;
}
/* */
void LeadTrail()
{
int i, s, k, j, n;
char pr[30][30], b, c;
printf("Enter the no of productions:");
scanf("%d", &n);
printf("Enter the productions one by one\n");
for (i = 0; i < n; i++)
scanf("%s", pr[i]);
nt = 0;
t = 0;
for (i = 0; i < n; i++)
{
if ((searchNT(pr[i][0])) == -1)
NT[nt++] = pr[i][0];
}
for (i = 0; i < n; i++)
{
for (j = 3; j < strlen(pr[i]); j++)
{
if (searchNT(pr[i][j]) == -1)
{
if (searchTER(pr[i][j]) == -1)
T[t++] = pr[i][j];
}
}
}
for (i = 0; i < nt; i++)
{
for (j = 0; j < t; j++)
lo[i][j] = 'f';
}
for (i = 0; i < nt; i++)
{
for (j = 0; j < t; j++)
tr[i][j] = 'f';
}
for (i = 0; i < nt; i++)
{
for (j = 0; j < n; j++)
{
if (NT[(searchNT(pr[j][0]))] == NT[i])
{
if (searchTER(pr[j][3]) != -1)
installL(searchNT(pr[j][0]), searchTER(pr[j][3]));
else
{
for (k = 3; k < strlen(pr[j]); k++)
{
if (searchNT(pr[j][k]) == -1)
{
installL(searchNT(pr[j][0]), searchTER(pr[j][k]));
break;
}
}
}
}
}
}
while (top != 0)
{
b = pop();
c = pop();
for (s = 0; s < n; s++)
{
if (pr[s][3] == b)
installL(searchNT(pr[s][0]), searchTER(c));
}
}
for (i = 0; i < nt; i++)
{
printf("Leading[%c]\t{", NT[i]);
for (j = 0; j < t; j++)
{
if (lo[i][j] == 't')
printf("%c,", T[j]);
}
printf("}\n");
}
top = 0;
for (i = 0; i < nt; i++)
{
for (j = 0; j < n; j++)
{
if (NT[searchNT(pr[j][0])] == NT[i])
{
if (searchTER(pr[j][strlen(pr[j]) - 1]) != -1)
installT(searchNT(pr[j][0]), searchTER(pr[j][strlen(pr[j]) - 1]));
else
{
for (k = (strlen(pr[j]) - 1); k >= 3; k--)
{
if (searchNT(pr[j][k]) == -1)
{
installT(searchNT(pr[j][0]), searchTER(pr[j][k]));
break;
}
}
}
}
}
}
while (top != 0)
{
b = pop();
c = pop();
for (s = 0; s < n; s++)
{
if (pr[s][3] == b)
installT(searchNT(pr[s][0]), searchTER(c));
}
}
for (i = 0; i < nt; i++)
{
printf("Trailing[%c]\t{", NT[i]);
for (j = 0; j < t; j++)
{
if (tr[i][j] == 't')
printf("%c,", T[j]);
}
printf("}\n");
}
/* */
int main()
{
int j;
LeadTrail();
input = (char *)malloc(50 * sizeof(char));
printf("\nEnter the string\n");
scanf("%s", input);
input = strcat(input,
"$"); l = strlen(input);
strcpy(stack, "$");
printf("\nSTACK\tINPUT\tACTION");
while (i <= l)
{
shift();
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tShift");
if (prec[getindex(stack[top])][getindex(input[i])] == '>')
{
while (reduce())
{
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tReduced: E->%s", lasthandle);
}
}
else if (prec[getindex(stack[top])][getindex(input[i])] == '<')
{
if (prec[searchter(stack[top])][searchter(input[i])] == '<')
{
while (reduce())
{
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tReduced: E->%s", lasthandle);
}
}
}
}
if (strcmp(stack, "$E$") ==
0) printf("\nAccepted;");
else
printf("\nNot Accepted;");
return 0;
}
OUTPUT-