Implementation of Shift-Reduced Parsing Algorithms Ex - No
Implementation of Shift-Reduced Parsing Algorithms Ex - No
No:
IMPLEMENTATION OF SHIFT-REDUCED PARSING ALGORITHMS
AIM:
To write a program for implementing Shift Reduce Parsing using C.
ALGORITHM:
om
reaches the start symbol.
.c
5. Display the Stack Implementation table with corresponding Stack
us
actions with input symbols.
pl
ity
rs
ve
ni
au
nn
.a
w
w
w
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
om
void check();
.c
us
void main()
pl
ity
{
rs
ve
clrscr();
ni
au
printf("\n GRAMMER\n");
.a
w
w
gets(ip_sym);
printf("\n________\t\t____________\t\t____________\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
om
.c
ip_ptr++; us
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
pl
ity
strcpy(act,"shift");
rs
ve
temp[0]=ip_sym[ip_ptr];
ni
au
temp[1]='\0';
nn
.a
strcat(act,temp);
w
w
check();
w
st_ptr++;
st_ptr++;
check();
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a"))||(!strcmpi(temp2,"b")))
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n $%s\t\t%s$\t\t\tE->a",stack,ip_sym);
else
om
.c
printf("\n $%s\t\t%s$\t\t\tE->b",stack,ip_sym);
us
flag=1;
pl
ity
}
rs
ve
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*"))||(!strcmpi(temp2,"/")))
ni
au
{
nn
.a
flag=1;
w
w
}
w
if((!strcmpi(stack,"E+E"))||(!strcmpi(stack,"E\E"))||(!strcmpi(stack,"E*E")))
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
else
if(!strcmpi(stack,"E\E"))
printf("\n $%s\t\t%s$\t\t\tE->E\E",stack,ip_sym);
else
if(!strcmpi(stack,"E*E"))
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
flag=1;
om
.c
if(!strcmpi(stack,"E")&&ip_ptr==len) us
{
pl
ity
printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
rs
ve
getch();
ni
au
exit(0);
nn
.a
}
w
w
if(flag==0)
w
printf("\n%s\t\t\t%s\t\t reject",stack,ip_sym);
exit(0);
return;
}
OUTPUT:
GRAMMER
E->E+E
E->E/E
E->E*E
E->a/b
om
Stack Input Symbol Action
.c
------- ----------------- ---------
us
pl
$ a+b$ --
ity
rs
$a +b$ shift a
ve
$E +b$ E->a
ni
au
$E+ b$ shift +
nn
$E+b $ shift b
.a
w
$E+E $ E->b
w
w
$E $ E->E+E
$E $ ACCEPT
RESULT:
AIM:
To write a program for construction of LR Parsing table using C.
ALGORITHM:
om
to production and production reduction to start symbol. No
.c
operation performed on the operator.us
pl
ity
rs
ve
ni
au
nn
.a
w
w
w
PROGRAM:
#include<stdio.h>
#include<conio.h>
char stack[30];
int top=-1;
void push(char c)
{
top++;
om
stack[top]=c;
.c
} us
char pop()
pl
ity
{
rs
char c;
ve
if(top!=-1)
ni
au
{
nn
c=stack[top];
.a
top--;
w
return c;
w
w
}
return'x';
}
void printstat()
{
int i;
printf("\n\t\t\t $");
for(i=0;i<=top;i++)
printf("%c",stack[i]);
}
void main()
{
int i,j,k,l;
char s1[20],s2[20],ch1,ch2,ch3;
clrscr();
printf("\n\n\t\t LR PARSING");
printf("\n\t\t ENTER THE EXPRESSION");
scanf("%s",s1);
l=strlen(s1);
j=0;
printf("\n\t\t $");
for(i=0;i<l;i++)
{
if(s1[i]=='i' && s1[i+1]=='d')
{
om
s1[i]=' ';
.c
s1[i+1]='E'; us
printstat(); printf("id");
pl
ity
push('E');
rs
printstat();
ve
}
ni
au
{
.a
push(s1[i]);
w
w
printstat();
w
}
}
printstat();
l=strlen(s2);
while(l)
{
ch1=pop();
if(ch1=='x')
{
printf("\n\t\t\t $");
break;
}
if(ch1=='+'||ch1=='/'||ch1=='*'||ch1=='-')
{
ch3=pop();
if(ch3!='E')
{
printf("errror");
exit();
}
else
{
push('E');
printstat();
}
om
}
.c
ch2=ch1; us
}
pl
ity
getch();
rs
}
ve
ni
au
nn
.a
w
w
w
OUTPUT:
LR PARSING
ENTER THE EXPRESSION
id+id*id-id
$
$id
$E
$E+
$E+id
om
$E+E
$E+E*
.c
us
pl
$E+E*id
ity
$E+E*E
rs
$E+E*E-
ve
$E+E*E-id
ni
au
$E+E*E-E
nn
$E+E*E-E
.a
$E+E*E
w
w
$E
w
RESULT: