Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shift reduce parser
- #include<stdio.h>
- #include<stdlib.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()
- {
- clrscr();
- printf("\n\n\t Shift Reduce Parser\n");
- printf("\n\t***** ****** ******");
- printf("\n Grammar\n\n");
- printf("E->E+E\nE->E/E\n");
- printf("E->E*E\nE->a/b");
- printf("\n Enter the Input Symbol:\t");
- for(i=0;i<5;i++)
- {
- scanf("%c",&ip_sym[i]);
- }
- printf("\n\n\t Stack Implementation Table");
- printf("\n Stack\t\t Input Symbol\t\t Action");
- strcpy(act,"shift");
- temp[0]=ip_sym[ip_ptr];
- temp[1]='\0';
- strcat(act,temp);
- printf("\n $\t\t %s$\t\t\t%s ",ip_sym,act);
- 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]=' ';
- //printf("\n$%s\t\t%s$\t\t\t",stack,ip_sym);
- ip_ptr++;
- strcpy(act,"shift");
- temp[0]=ip_sym[ip_ptr];
- temp[1]='\0';
- strcat(act,temp);
- check();
- st_ptr++;
- }
- st_ptr++;
- check();
- check();
- }
- void check()
- {
- int flag=0;
- temp2[0]=stack[st_ptr];
- temp2[1]='\0';
- if(!strcmp(temp2,"a"))
- {
- printf("\n$%s\t\t%s$\t\t\tE->a",stack,ip_sym);
- stack[st_ptr]='E';
- if(ip_ptr!=len)
- printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
- flag=1;
- }
- if(!strcmp(temp2,"b"))
- {
- printf("\n$%s\t\t%s$\t\t\tE->b",stack,ip_sym);
- stack[st_ptr]='E';
- if(ip_ptr!=len)
- printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
- flag=1;
- }
- if((!strcmp(temp2,"+"))||(!strcmp(temp2,"*"))||(!strcmp(temp2,"/")))
- {
- printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
- flag=1;
- }
- if((!strcmp(stack,"E+E"))||(!strcmp(stack,"E/E"))||(!strcmp(stack,"E*E")))
- {
- if(!strcmp(stack,"E+E"))
- printf("\n$%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
- else if(!strcmp(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);
- strcpy(stack,"E");
- st_ptr=0;
- flag=1;
- }
- if(!strcmpi(stack,"E")&&ip_ptr==len)
- {
- printf("\n$%s\t\t%s$\t\t\tAccept",stack,ip_sym);
- getch();
- exit(0);
- }
- if(flag==0)
- {
- printf("\n$%s\t\t%s$\t\t\tReject",stack,ip_sym);
- getch();
- exit(0);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement