Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DEAPRTMENT OF COMPUTER ENGINEERING

Experiment No: 04

Aim: Design and implementation of Operator Precedence Parser

Software Used: C, C++, JAVA

Theory:
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Operator precedence grammars rely on the following three precedence relations between the
terminals:
Relation Meaning

a <• b a yields precedence to b

a =• b a has the same precedence as b

a •> b a takes precedence over b


These operator precedence relations allow to delimit the handles in the right sentential forms:
<• marks the left end, =• appears in the interior of the handle, and •> marks the right end.
Contrary to other shift-reduce parsers, all nonterminals are considered equal for the purpose of
identifying handles.[3] The relations do not have the same properties as their un-dotted
counterparts; e. g. a =• b does not generally imply b =• a, and b •> a does not follow from a <•
b. Furthermore, a =• a does not generally hold, and a •> a is possible.
Let us assume that between the terminals ai and ai+1 there is always exactly one precedence
relation. Suppose that $ is the end of the string. Then for all terminals b we define: $ <• b and
b •> $. If we remove all nonterminals and place the correct precedence relation: <•, =•, •>
between the remaining terminals, there remain strings that can be analyzed by an easily
developed bottom-up parser.
id + * $

id •> •> •>

+ <• •> <• •>


DEAPRTMENT OF COMPUTER ENGINEERING

* <• •> •> •>

$ <• <• <•

They follow from the following facts:


 + has lower precedence than * (hence + <• * and * •> +).
 Both + and * are left-associative (hence + •> + and * •> *).
The input string
id1 + id2 * id3
after adding end markers and inserting precedence relations becomes
$ <• id1 •> + <• id2 •> * <• id3 •> $
Algorithm:
Initialize: Set ip to point to the first symbol of w$
Repeat:
If $ is on the top of the stack and ip points to $ then return
else
Let a be the top terminal on the stack, and b the symbol pointed to by ip
if a <• b or a =• b then
push b onto the stack
advance ip to the next input symbol
else if a •> b then
repeat
pop the stack
until the top stack terminal is related by <• to the terminal most recently popped
else error()
end
Program:

#include<stdio.h>
#include<string.h>
char *input;
int i=0;
char lasthandle[6],stack[50],handles[][5]={")E(","E*E","E+E","i","E^E"};
//(E) becomes )E( when pushed to stack
DEAPRTMENT OF COMPUTER ENGINEERING

int top=0,l;
char prec[9][9]={

/*input*/

/*stack + - * / ^ i ( ) $ */

/* + */ '>', '>','<','<','<','<','<','>','>',

/* - */ '>', '>','<','<','<','<','<','>','>',

/* * */ '>', '>','>','>','<','<','<','>','>',

/* / */ '>', '>','>','>','<','<','<','>','>',

/* ^ */ '>', '>','>','>','<','<','<','>','>',

/* i */ '>', '>','>','>','>','e','e','>','>',

/* ( */ '<', '<','<','<','<','<','<','>','e',

/* ) */ '>', '>','>','>','>','e','e','>','>',

/* $ */ '<', '<','<','<','<','<','<','<','>',

};

int getindex(char c)
{
switch(c)
DEAPRTMENT OF COMPUTER ENGINEERING

{
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;
DEAPRTMENT OF COMPUTER ENGINEERING

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;
input=(char*)malloc(50*sizeof(char));
printf("\nEnter the string\n");
DEAPRTMENT OF COMPUTER ENGINEERING

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;");}
DEAPRTMENT OF COMPUTER ENGINEERING

Conclusion: This experiment will help students to understand the concept of bottom-up
parsing technique.

You might also like