Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Compiler design abca, Exercises of Compiler Design

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

2022/2023

Uploaded on 07/23/2023

arvasu-gupta
arvasu-gupta 🇮🇳

3 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT-6
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++)
{
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Compiler design abca and more Exercises Compiler Design in PDF only on Docsity!

ASSIGNMENT- 6

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 &prod, int n) { int cnt, cnt3; char lp; string d = "->"; //Input of Productions cout<<"Note: Here ^ denotes epsillon\n"; for(cnt=0;cnt<=n-1;cnt++) { int pos = prod[cnt].find(d); lp = prod[cnt][0]; prodn[cnt].lf = lp; for(int i=pos+2; i<=prod[cnt].length()-1; i++) { prodn[cnt].rt[i-(pos+2)] = prod[cnt][i]; } prodn[cnt].prod_rear=strlen(prodn[cnt].rt); prodn[cnt].fl=0; } //Condition for left factoring int cnt1 = 0, cnt2 = 0; for(cnt1=0;cnt1<n;cnt1++) { for(cnt2=cnt1+1;cnt2<n;cnt2++) { if(prodn[cnt1].lf==prodn[cnt2].lf) { cnt=0;

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:

  1. Write a C program to compute FIRST and FOLLOW sets for a given grammar, and check whether the grammar is LL(1). Solution: #include "ctype.h" #include "string.h" #include "stdio.h" char gram[10][10]; char vFirst[10]; char nonT[10]; char vFollow[10]; int m = 0; int p; int i = 0; int j = 0; int elem[10]; int size; int fPt; int k = 0; int getGram() { char ch; int i; int j; int k; printf("\nEnter Number of Rule : "); scanf("%d", &size); printf("\nEnter Grammar as E=E+B \n"); for(i = 0; i < size; i++){ scanf("%s%c", gram[i], &ch); elem[i] = strlen(gram[i]); }