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

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

The document describes an experiment to design a shift-reduce parser. It includes code to take a string as input, check for specific symbols and operations like "e+e" and "(e)", and reduce the string by removing symbols according to parsing rules. If the string is fully reduced, it is accepted, otherwise it is rejected. The code iterates through the string in reverse order to apply the shift-reduce parsing operations.

Uploaded by

Rahul Jha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

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

The document describes an experiment to design a shift-reduce parser. It includes code to take a string as input, check for specific symbols and operations like "e+e" and "(e)", and reduce the string by removing symbols according to parsing rules. If the string is fully reduced, it is accepted, otherwise it is rejected. The code iterates through the string in reverse order to apply the shift-reduce parsing operations.

Uploaded by

Rahul Jha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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