0% found this document useful (0 votes)
8 views2 pages

EXP12

The document contains a C program that implements a simple parser for a grammar defined as E-> E+E | E*E | (E) | id. It processes an input string, simulating a stack-based parsing mechanism, and outputs the stack, input, and action taken at each step. The program includes functions to handle shifting and reducing actions based on the grammar rules.

Uploaded by

leemong335
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)
8 views2 pages

EXP12

The document contains a C program that implements a simple parser for a grammar defined as E-> E+E | E*E | (E) | id. It processes an input string, simulating a stack-based parsing mechanism, and outputs the stack, input, and action taken at each step. The program includes functions to handle shifting and reducing actions based on the grammar rules.

Uploaded by

leemong335
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/ 2

EXPERIMENT : 12

#include<stdio.h>
#include<string.h>

int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();

int main()
{

printf("GRAMMAR IS E-> E+E | E*E | (E) | id\n");


printf("Enter input string: ");
scanf("%s",a);
c = strlen(a);
strcpy(act,"SHIFT->");
printf("STACK \t INPUT \t ACTION");
for(k=0,i=0;j<c;k++,i++,j++)
{
if(a[j]=='i' && a[j+1]=='d')
{
stk[i] = a[j];
stk[i+1] = a[j+1];
stk[i+2] = '\0';
a[j] = ' ';
a[j+1] = ' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);
check();
}
else
{
stk[i] = a[j];
stk[i+1] = '\0';
a[j] = ' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}
}
}

void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0;z<c;z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z] = 'E';
stk[z+1] = '\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
for(z=0;z<c;z++)
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')
{
stk[z] = 'E';
stk[z+1] = '\0';
stk[z+2] = '\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i = i-2 ;
}
for(z=0;z<c;z++)
if(stk[z]=='E' && stk[z+1]=='*' && stk[z+2]=='E')
{
stk[z] = 'E';
stk[z+1] = '\0';
stk[z+2] = '\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i = i-2 ;
}
for(z=0;z<c;z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{
stk[z] = 'E';
stk[z+1] = '\0';
stk[z+2] = '\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i = i-2 ;
}
}

OUTPUT

You might also like