Experiment - 3 Aim: To Design Shift Reduce Parser.: #Include #Include #Include #Include Void

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Experiment -3

Aim: To design shift reduce parser.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
cout<<"enter a string \n";
char str[100];
gets(str);
int len = strlen(str);
cout<<"\n the length of the string is "<< len<<"\n";
int top=-1,dollar=0;
top=len-1;
for(int i=top;i>=0 ;i--)
{
if(str[top]!='e'|| str[top]!='+' || str[top]!='*' || str[top]!='(' ||
str[top]!=')' || str[top]!='$' || str[top]=='i')
{
cout<<"string rejected\n";
break;
}
if(str[top]=='i')
{
str[top]='e';
cout<<"i processed\n";
}
if(str[top]=='e' && str[top-1]=='+' && str[top-2]=='e')
{
top=top-2;
str[top]='e';
cout<<"e+e processed\n";
continue;
}
if(str[top]=='e' && str[top-1]=='*' && str[top-2]=='e')
{
top=top-2;
str[top]='e';
cout<<"e*e processed\n";
continue;
}
if(str[top]=='(' && str[top-1]=='e' && str[top-2]==')')
{
top=top-2;
str[top]='e';
cout<<"(e) processed";
continue;
}

if(str[top]=='e')
{
--top;
cout<<"e processed\n";
}
if(str[top]=='$')
{
--top;
cout<<"$ processed\n";
if(top==-1)
{
cout<<"string accepted\n";
}
}
}
getch();
}

Output:

You might also like