















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This is some compiler design problems needed to be uploaded for some verification. Compiler design is a very difficult subject but with consistent practice we can all
Typology: Exercises
1 / 23
This page cannot be seen from the preview
Don't miss anything!
1.Consider the following grammar rules: E->E+T|T, T->T*F|F, F->(E)|id where E, T and F are non-terminals and +, *, (, ), id are terminals (tokens). Design a shift-reduce parser for it. Solution: #include<bits/stdc++.h> using namespace std; struct grammer{ char p[20]; char prod[20]; }g[10]; int main() { cout<<"\t\t\t SHIFT REDUCE PARSER\t\t\t\n"; int i,stpos,j,k,l,m,o,p,f,r; int np,tspos,cr; cout<<"\nEnter Number of productions:"; cin>>np; char sc,ts[10]; cout<<"\nEnter productions:\n"; for(i=0;i<np;i++) {
cin>>ts; strncpy(g[i].p,ts,1); strcpy(g[i].prod,&ts[3]); } char ip[10]; cout<<"\nEnter Input:"; cin>>ip; int lip=strlen(ip); char stack[10]; stpos=0; i=0; //moving input sc=ip[i]; stack[stpos]=sc; i++;stpos++; cout<<"\n\nStack\t\tInput\t\tAction"; do { r=1; while(r!=0) { cout<<"\n"; for(p=0;p<stpos;p++) {
ts[tspos]=stack[l]; tspos++; } //now compare each possibility with production for(m=0;m<np;m++) { cr = strcmp(ts,g[m].prod); //if cr is zero then match is found if(cr==0) { for(l=k;l<10;l++) //removing matched part from stack { stack[l]='\0'; stpos--; } stpos=k; //concatinate the string strcat(stack,g[m].p); stpos++; r=2; } } } } //moving input
sc=ip[i]; stack[stpos]=sc; i++;stpos++; }while(strlen(stack)!=1 && stpos!=lip); if(strlen(stack)==1) { cout<<"\n\n \t\t\tSTRING IS ACCEPTED\t\t\t"; } else cout<<"\n\n \t\t\tSTRING IS REJECTED\t\t\t"; return 0; } Output:
/* i / '>', '>','>','>','>','e','e','>','>', / ( / '<', '<','<','<','<','<','<','>','e', / ) / '>', '>','>','>','>','e','e','>','>', / $ / '<', '<','<','<','<','<','<','<','>', }; int getindex(char c) { switch(c) { case '+':return 0; case '-':return 1; case '':return 2; case '/':return 3; case '^':return 4; case 'i':return 5; case '(':return 6; case ')':return 7; case '$':return 8; } }
int shift() { stack[++top]=*(input+i++); stack[top+1]='\0'; } int reduce() { int i,len,found,t; for(i=0;i<5;i++)//selecting handles { len=strlen(handles[i]); if(stack[top]==handles[i][0]&&top+1>=len) { found=1; for(t=0;t<len;t++) { if(stack[top-t]!=handles[i][t]) { found=0; break; } } if(found==1) { stack[top-t+1]='E'; top=top-t+1; strcpy(lasthandle,handles[i]);
input=(char)malloc(50sizeof(char)); printf("\nEnter the string\n"); scanf("%s",input); input=strcat(input,"$"); l=strlen(input); strcpy(stack,"$"); printf("\nSTACK\tINPUT\tACTION"); while(i<=l) { shift(); printf("\n"); dispstack(); printf("\t"); dispinput(); printf("\tShift"); if(prec[getindex(stack[top])][getindex(input[i])]=='>') { while(reduce()) { printf("\n"); dispstack(); printf("\t"); dispinput(); printf("\tReduced: E->%s",lasthandle); } } } if(strcmp(stack,"$E$")==0) printf("\nAccepted;");
else printf("\nNot Accepted;"); } Output:
beta = production[i][index+1]; printf("Grammar without left recursion:\n"); printf("%c->%c%c'", non_terminal, beta, non_terminal); printf("\n%c'->%c%c'|^\n", non_terminal, alpha, non_terminal); } else printf(" can't be reduced\n"); } else printf(" is not left recursive.\n"); index = 3; }return 0; } To eliminate left recursion: #include <bits/stdc++.h> using namespace std; struct production { char lf; char rt[10]; int prod_rear; int fl; }; struct production prodn[20],prodn_new[20]; //Creation of object //Variables Declaration for left factoring int b=-1,d,q,f,n,m=0,c=0; char terminal[20],nonterm[20],alpha[10],extra[10];
char epsilon='^'; void left_fact(vector
cnt++; } if((prodn[cnt1].rt[cnt]==0)&&(m==0)) { int h=0; prodn_new[++b].lf=prodn[cnt1].lf; strcpy(prodn_new[b].rt,extra); //prodn_new[b].rt[p+1]=alpha[c]; prodn_new[b].rt[p+1]=prodn[cnt1].lf; //prodn_new[++b].lf=alpha[c]; prodn_new[++b].lf=prodn[cnt1].lf; prodn_new[b].rt[0]=epsilon; //prodn_new[++b].lf=alpha[c]; prodn_new[++b].lf=prodn[cnt1].lf; for(q=cnt;q<prodn[cnt2].prod_rear;q++) prodn_new[b].rt[h++]=prodn[cnt2].rt[q]; } if((prodn[cnt2].rt[cnt]==0)&&(m==0)) { int h=0; prodn_new[++b].lf=prodn[cnt1].lf; strcpy(prodn_new[b].rt,extra); //prodn_new[b].rt[p+1]=alpha[c]; prodn_new[b].rt[p+1]=prodn[cnt1].lf; //prodn_new[++b].lf=alpha[c]; prodn_new[++b].lf=prodn[cnt1].lf; prodn_new[b].rt[0]=epsilon; //prodn_new[++b].lf=alpha[c]; prodn_new[++b].lf=prodn[cnt1].lf; for(q=cnt;q<prodn[cnt1].prod_rear;q++)
prodn_new[b].rt[h++]=prodn[cnt1].rt[q]; } c++; m=0; } } } //Display of Output cout<<"\n After Left Factoring \n"; cout<<endl; for(cnt3=0;cnt3<=0;cnt3++) { cout<<"Production "<<cnt3+1<<" is: "; cout<<prodn_new[cnt3].lf; cout<<"->"; cout<<prodn_new[cnt3].rt<<"'"; cout<<endl<<endl; } for(cnt3=1;cnt3<=b;cnt3++) { cout<<"Production "<<cnt3+1<<" is: "; cout<<prodn_new[cnt3].lf<<"'"; cout<<"->"; cout<<prodn_new[cnt3].rt; cout<<endl<<endl; } cnt3 = b+2; for(int cnt4=0;cnt4<n;cnt4++) {
Output: