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

Implementation of Shift-Reduced Parsing Algorithms Ex - No

The document describes a program to implement a shift-reduce parsing algorithm using a C programming language. The algorithm gets an input expression, shifts and reduces symbols according to production rules using a stack, and displays the stack implementation table with the corresponding stack actions and input symbols. The program implements this algorithm by getting input, shifting symbols onto a stack, reducing based on grammar rules, and outputting the stack implementation table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
558 views

Implementation of Shift-Reduced Parsing Algorithms Ex - No

The document describes a program to implement a shift-reduce parsing algorithm using a C programming language. The algorithm gets an input expression, shifts and reduces symbols according to production rules using a stack, and displays the stack implementation table with the corresponding stack actions and input symbols. The program implements this algorithm by getting input, shifting symbols onto a stack, reducing based on grammar rules, and outputting the stack implementation table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Ex.

No:
IMPLEMENTATION OF SHIFT-REDUCED PARSING ALGORITHMS

AIM:
To write a program for implementing Shift Reduce Parsing using C.

ALGORITHM:

1. Get the input expression and store it in the input buffer.


2. Read the data from the input buffer one at the time.
3. Using stack and push & pop operation shift and reduce symbols
with respect to production rules available.
4. Continue the process till symbol shift and production rule reduce

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\t\t SHIFT REDUCE PARSER\n");


nn

printf("\n GRAMMER\n");
.a
w
w

printf("\n E->E+E\n E->E/E");


w

printf("\n E->E*E\n E->a/b");

printf("\n enter the input symbol:\t");

gets(ip_sym);

printf("\n\t stack implementation table");

printf("\n stack \t\t input symbol\t\t action");

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:

SHIFT REDUCE PARSER

GRAMMER

E->E+E

E->E/E

E->E*E

E->a/b

Enter the input symbol: a+b

Stack Implementation Table

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:

The program for implementation of Shift Reduce parsing


algorithm is executed and verified.
Ex.No:
CONSTRUCTION OF LR-PARSING TABLE

AIM:
To write a program for construction of LR Parsing table using C.

ALGORITHM:

1. Get the input expression and store it in the input buffer.


2. Read the data from the input buffer one at the time and convert in
to corresponding Non Terminal using production rules available.
3. Perform push & pop operation for LR parsing table construction.
4. Display the result with conversion of corresponding input symbols

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

else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*' ||s1[i]=='/' ||s1[i]=='d')


nn

{
.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:

The program for construction of LR Parsing table is executed


and verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy