0% found this document useful (0 votes)
2 views4 pages

CD Exp 7 Material

The document describes the implementation of a shift-reduce parsing algorithm, which uses a parse stack to process input symbols according to grammar rules. It outlines the operation of the parser, including the use of an action table to determine actions such as shifting, reducing, accepting, or signaling an error. A sample program in C is provided to demonstrate the shift-reduce parsing process with a specific grammar and input symbols.

Uploaded by

yuvaraj250921
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)
2 views4 pages

CD Exp 7 Material

The document describes the implementation of a shift-reduce parsing algorithm, which uses a parse stack to process input symbols according to grammar rules. It outlines the operation of the parser, including the use of an action table to determine actions such as shifting, reducing, accepting, or signaling an error. A sample program in C is provided to demonstrate the shift-reduce parsing process with a specific grammar and input symbols.

Uploaded by

yuvaraj250921
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/ 4

Roll No:

EXERCISE – 7

Aim: Implementation of shift-reduce parsing algorithm.


Description:
A shift-reduce parser uses a parse stack which (conceptually) contains grammar symbols. During the
operation of the parser, symbols from the input are shifted onto the stack. If a prefix of the symbols on top
of the stack matches the RHS of a grammar rule which is the correct rule to use within the current context,
then the parser reduces the RHS of the rule to its LHS, replacing the RHS symbols on top of the stack with
the non-terminal occurring on the LHS of the rule. This shift-reduce process continues until the parser
terminates, reporting either success or failure. It terminates with success when the input is legal and is
accepted by the parser. It terminates with failure if an error is detected in the input.
The operation of the parser is controlled by a couple of tables:
Action Table: The action table is a table with rows indexed by states and columns indexed by
terminal symbols. When the parser is in some state s and the current lookahead terminal is t, the
action taken by the parser depends on the contents of action[s][t], which can contain four
different kinds of entries:
Shift s'
→ Shift state s‟ onto the parse stack.
Reduce r
→ Reduce by rule r. This is explained in more detail below.
Accept
→ Terminate the parse with success, accepting the input.
Error
→ Signal a parse error

Algorithm:
Step 1: Initial State: the stack consists of the single state, s0; ip points to the first character in w.
Step 2: For top-of-stack symbol, s, and next input symbol, a case action of T[s, a].
Step 3: Shift x: (x is a STATE number) push a, then x on the top of the stack and Advance ip to point to the
next input symbol.
Step 4: Reduce y: (y is a PRODUCTION number) Assume that the production is of the form A ==> beta
pop 2 * |beta| symbols of the stack.
Step 5: At this point the top of the stack should be a state number, say s‟. push A, then go to of T[s‟, A] (a
state number) on the top of the stack.

29 | P a g e
Roll No:

Program:
#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];
void check();
void main() {
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->a/b");
printf("\n enter the input symbol: ");
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]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift");
temp[0]=ip_sym[ip_ptr];

30 | P a g e
Roll No:

temp[1]='\0';
strcat(act,temp);
check();
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
printf("\n $%s\t\t%s$\t\t\tE->b",stack,ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*"))||(!strcmpi(temp2,"/"))) {
flag=1;
}
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

31 | P a g e
Roll No:

printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
flag=1;
}
if(!strcmpi(stack,"E")&&ip_ptr==len) {
printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
exit(0);
}
if(flag==0) {
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
Stack Input Symbol Action
$ a+b$ ---
$a +b$ Shift (a)
$E +b$ E→a
$E+ b$ Shift (+)
$E+b $ Shift (b)
$E+E $ E→b
$E $ E→E+E
$E $ Accept

32 | P a g e

You might also like