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

Operator Precedence

This document contains the code for an operator precedence parsing program. It defines functions for shifting and reducing tokens on a stack based on a precedence table. The main function gets grammar rules as input, then the expression to parse. It uses shift and reduce functions in a loop to parse the input according to the grammar, and indicates if the input is accepted or not based on the final stack contents.

Uploaded by

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

Operator Precedence

This document contains the code for an operator precedence parsing program. It defines functions for shifting and reducing tokens on a stack based on a precedence table. The main function gets grammar rules as input, then the expression to parse. It uses shift and reduce functions in a loop to parse the input according to the grammar, and indicates if the input is accepted or not based on the final stack contents.

Uploaded by

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

//NAME:ROHITH R NAIR

//ROLL NO:52
//BATCH:S7R
//PROGRAM:OPERATOR PRECEDENCE PARSING
*******************************************

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *input;
int i;

char lasthandle[6],stack[50],handles[][5]={")E(","E*E","E+E","i","E^E"};
//(E) becomes )E( when pushed to stack */

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;
}
}

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));
}

void main()
{

int j=0;
printf("Enter grammar \n");
for(int i=0;i<=4;i++)
{
scanf(" %[^\n]",handles[j]);
}

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);
}
}
}

if(strcmp(stack,"$E$")==0)
printf("\nAccepted;");
else
printf("\nNot Accepted;");
}

You might also like