Ex. No: Implementation of Operator Precedence Parser Date
Ex. No: Implementation of Operator Precedence Parser Date
No:
IMPLEMENTATION OF OPERATOR PRECEDENCE
PARSER
Date:
AIM:
To write a program to implement operator precedence parser.
ALGORITHM:
1. Start the program.
2. Set the ip to print the first symbol of ws.
3. Let x be the top of the stack pointed by the ip.
4. If $ is on the top of the stack and the ip point to $ then return else let a be the terminal on
the stack and b be the symbol pointed by ip.
5. I a<b or a+b push b to the stack and advance the ip to the next input symbol.
6. Else if a>b then pop the stack until the top stack terminal is related by < to terminal most
recentiy popped else error();
7. Stop the program.
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int scan(int);
int number(char);
int findingG();
int findingL();
int erase(char);
char opers[6]={'i','+','*','$','/','-'},input[50];
char
table[6][6]={'=','>','>','>','>','>','<','>','<','>','>','>','<','>','>','>','>','>','<','<','<','=','<','<','<','>','<','>','<
','>'};
int scan(int position)
{
int i;
for(i=strlen(input);i>=position;i--)
input[i]=input[i-1];
return i;
}
int number(char operator)
{
int i;
for(i=0;i<sizeof(opers);i++)
if(opers[i]==operator)
return i;
return -1;
}
int findingG()
{
int i;
for(i=0;i<strlen(input);i++)
if(input[i]=='>')
return i;
return-1;
}
int findingL(int position)
{
int i;
for(i=position;i>=0;i--)
if(input[i]=='<')
return i;
return -1;
}
int erase(char ch)
{
int i,j;
for(i=0;i<strlen(input);i++)
if(input[i]==ch)
for(j=i;j<strlen(input);j++)
input[j]=input[j+1];
return -1;
}
void main()
{
int i,G,L;
clrscr();
printf("\n\n\t\t***OPERATOR PRECEDENCE PARSING***\n\n");
printf("\tEnter the input:");
scanf("%s",input);
for(i=1;i<strlen(input);i+=2)
{
scan(i);
input[i]=table[number(input[i])][number(input[i+1])];
}
printf("\n\tThe parsed output is \n");
while(strcmp(input,"$$"))
{
G=findingG();
L=findingL(G);
input[L]='x';
input[L+1]=table[number(input[L-1])][number(input[G+1])];
input[G]='x';
erase('x');
erase('=');
printf("\nNext stage:\n");
printf("%s",input);
}
getch();
}
OUTPUT:
RESULT:
Thus the above the program is executed and the required output is obtained.