Top Banner
Lab File COMPILER DESIGN LAB B. Tech. Computer Science & Engineering (3rd Year) 1
39

Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

May 11, 2018

Download

Documents

NguyenDat
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

Lab FileCOMPILER DESIGN LAB

B. Tech.Computer Science & Engineering (3rd Year)

1

Page 2: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

List of Experiments

S. No. Name of Experiment Page No. Remark

1 Write a program to design lexical analyser

2

Write a program to remove left recursion from a given grammar

3Write a program to make the grammar left factored.

4 Write a program to find the first of the given grammar.

5

Write a program to find the follow of the given grammar

6Write a lex program for simple calculator.

7Write a program to implement the top-down parsing.

8Write the program to implement the operator precedence parsing

9

Write a program to implement the parse shift reduce parser in c.

10Write a program to demonstrate the BOOTSTRAPPING process in c.

2

Page 3: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

11Implement the following expression into quadruple.(a+b+c*d/e+f).

12Implement constant folding technique of code optimization

13Implement Common sub-expression Elimination technique of code optimization.

14Implement Strength Reduction using Induction Variable technique of code.

3

Page 4: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

1.) Write a program in C to design lexical analyser.

#include<string.h>#include<file.h>#include<stdio.h>void keyword(char str[10]){

if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0|| strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0|| strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0|| strcmp("case",str)==0) printf("\n%s is a keyword",str); else printf("\n%s is an identifier",str);

}int main(){

FILE *f1,*f2,*f3; char c,str[10],st1[10]; int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0; printf("\nEnter the c program");/*gets(st1);*/ f1=fopen("input","w"); while((c=getchar())!=EOF) putc(c,f1); fclose(f1); f1=fopen("input","r"); f2=fopen("identifier","w"); f3=fopen("specialchar","w"); while((c=getc(f1))!=EOF) { if(isdigit(c)) { tokenvalue=c-'0'; c=getc(f1); while(isdigit(c)) { tokenvalue*=10+c-'0'; c=getc(f1); } num[i++]=tokenvalue; ungetc(c,f1); }

else if(isalpha(c)) { putc(c,f2);

4

Page 5: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

c=getc(f1); while(isdigit(c)||isalpha(c)||c=='_'||c=='$') { putc(c,f2); c=getc(f1); } putc(' ',f2); ungetc(c,f1); } else if(c==' '||c=='\t') printf(" "); else if(c=='\n') lineno++; else putc(c,f3); } fclose(f2); fclose(f3); fclose(f1); printf("\nThe no's in the program are"); for(j=0;j<i;j++) printf("%d",num[j]); printf("\n"); f2=fopen("identifier","r"); k=0; printf("The keywords and identifiersare:"); while((c=getc(f2))!=EOF) { if(c!=' ') str[k++]=c; else { str[k]='\0'; keyword(str); k=0; } } fclose(f2); f3=fopen("specialchar","r"); printf("\nSpecial characters are"); while((c=getc(f3))!=EOF) printf("%c",c); printf("\n"); fclose(f3); printf("Total no. of lines are:%d",lineno);

return 0;}

5

Page 6: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

OUTPUT:

Enter the c programa+b*c+3

The no’s in the program are 3The keywords and identifiers are:a is an identifierb is an identifierc is an identifierSpecial characters are +*+Total no. of lines are: 1

6

Page 7: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

2.) Write a program to remove left recursion from a given grammar.

#include<stdio.h>#include<conio.h>#include<string.h>void main(){char exp[10][100],a[10],b[10],al[10],be[10];int i=0,j=0,n,k=0,q,h;clrscr();printf("Enter the no.productions\n");scanf("%d",&n);printf("Enter the productions\n");for(i=0;i<n;i++)scanf("%s",exp[i]);for(i=0;i<n;i++){q=h=k=0;for(j=0;j<10;j++){a[j]=NULL; b[j]=NULL; al[j]=NULL; be[j]=NULL;}while(exp[i][q]!='|')q++;while(isalpha(exp[i][h])){a[k]=exp[i][h];h++; k++;}h+=2;k=0;while(isalpha(exp[i][h])){b[k]=exp[i][h];h++; k++; }if(strcmp(a,b)==0){k=0;while(q<strlen(exp[i])-1){q++;be[k]=exp[i][q];k++;}k=0;while(exp[i][h]!='|'){al[k]=exp[i][h];k++; h++;

7

Page 8: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

}printf("%s->%s%s'\n",a,be,a); printf("%s'->%s%s'|%c\n",a,al,a,238);} } getch(); }

3.) Write a program to make the grammar left factored.

#include<stdio.h>#include<conio.h>#include<string.h>void main(){char exp[50],a[50],g[50],al[50],be[50],ab[50];int i=0,j=0,n,k=0,q,h,z,c,r,p,m,l,u;clrscr();printf("Enter the no.productions\n");scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<50;j++)exp[j]=NULL; j=0;printf("\nEnter the productions and end with adelimiter#\n");do{scanf("%c",&exp[j]); j++;}while(exp[j-1]!='#');u=j-1; h=1;q=k=z=r=p=c=m=l=0;for(j=0;j<50;j++){a[j]=NULL; g[j]=NULL; al[j]=NULL; ab[j]=NULL; be[j]=NULL;}for(z=1;z<=u;z++){if(exp[z]=='|')c++;}while(isalpha(exp[h]))

{a[k]=exp[h];h++; k++;}h+=2;m=h;while(isalpha(exp[h])||isspace(exp[h]))

8

Page 9: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

{ab[r]=exp[h];h++; r++;}h+=1; r=0;while(exp[h]==ab[r]){al[p]=ab[r];h++; r++; p++;}r=0; h=1;while(r!=c) {while(exp[h]=='|'){h++; r++;}h++; }r=h; p=0; h--; l=h-1;while(r<=u){g[p]=exp[h];p++; h++; r++;}printf("%s->%s%s'|%s\n",a,al,a,g);printf("%s'->",a);h=m-1;while(h<l){m=0;while(m<strlen(al)){h++; m++;}h++;be[q]=exp[h];while(exp[h]!='|'){be[q]=exp[h];q++; h++;}if(be[q]=='|'){be[q]=238;

9

Page 10: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

q++;}be[q]='|';q++;}for(j=0;j<strlen(be)-1;j++)printf("%c",be[j]);}getch(); }

4.) Write a program to find the first of the given grammar.

#include<stdio.h>#include<conio.h>char array[10][20],temp[10];int c,n;void fun(int,int[]);int fun2(int i,int j,int p[],int key){int k;if(!key){for(k=0;k<n;k++)if(array[i][j]==array[k][0])break;p[0]=i;p[1]=j+1;fun(k,p);return 0;}Else{for(k=0;k<=c;k++){if(array[i][j]==temp[k])break;}if(k>c)return 1;

10

Page 11: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

else return 0;}}void fun(int i,int p[]){int j,k,key;for(j=2;array[i][j]!='\0';j++){if(array[i][j-1]=='/'){if(array[i][j]>='A'&&array[i][j]<='Z'){key=0;fun2(i,j,p,key);}else{key=1;if(fun2(i,j,p,key))temp[++c]=array[i][j];if(array[i][j]=='@'&&p[0]!=-1){ if(array[p[0]][p[1]]>='A'&&array[p[0]][p[1]]<='Z'){key=0;fun2(p[0],p[1],p,key);}elseif(array[p[0]][p[1]]!='/'&&array[p[0]][p[1]]!='\0'){if(fun2(p[0],p[1],p,key))temp[++c]=array[p[0]][p[1]];}}}}}}void main(){int p[2],i,j;clrscr();printf("Enter the no. of productions :");

11

Page 12: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

scanf("%d",&n);printf("Enter the productions :\n");for(i=0;i<n;i++)scanf("%s",array[i]);for(i=0;i<n;i++){c=-1,p[0]=-1,p[1]=-1;fun(i,p);printf("First(%c) : [ ",array[i][0]);for(j=0;j<=c;j++)printf("%c,",temp[j]);printf("\b ].\n");getch();}}

Output : Enter the no. of productions :6Enter the productions :S/aBDhB/cCC/bC/@D/E/FE/g/@F/f/@First(S) : [ a ].First(B) : [ c ].First(C) : [ b,@ ].First(D) : [ g,@,f ].First(E) : [ g,@ ].First(F) : [ f,@ ].

12

Page 13: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

5.) Write a program to find the follow of the given grammar.

#include<stdio.h>#include<conio.h>#define max 10#define MAX 15char array[max][MAX],temp[max][MAX];int c,n,t;void fun(int,int[]);int fun2(int i,int j,int p[],int key){int k;if(!key){for(k=0;k<n;k++)if(array[i][j]==array[k][0])break;p[0]=i; p[1]=j+1;fun(k,p);return 0;}else{for(k=0;k<=c;k++){if(array[i][j]==temp[t][k])break;}if(k>c)return 1;else return 0;}}void fun(int i,int p[]){int j,k,key;for(j=2;array[i][j]!='\0';j++){if(array[i][j-1]=='/'){if(array[i][j]>='A'&&array[i][j]<='Z'){key=0;fun2(i,j,p,key);}else{key=1;if(fun2(i,j,p,key))temp[t][++c]=array[i][j];if(array[i][j]=='@'&&p[0]!=-1){

13

Page 14: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

if(array[p[0]][p[1]]>='A'&&array[p[0]][p[1]]<='Z'){key=0;fun2(p[0],p[1],p,key);}elseif(array[p[0]][p[1]]!='/'&&array[p[0]][p[1]]!='\0'){if(fun2(p[0],p[1],p,key))temp[t][++c]=array[p[0]][p[1]];} } } } } }char fol[max][MAX],ff[max];int f,l,ff0;void ffun(int,int);void follow(int i){int j,k;for(j=0;j<=ff0;j++)if(array[i][0]==ff[j])return 0;if(j>ff0)ff[++ff0]=array[i][0];if(i==0)fol[l][++f]='$';for(j=0;j<n;j++)for(k=2;array[j][k]!='\0';k++)if(array[j][k]==array[i][0])ffun(j,k);}void ffun(int j,int k){int ii,null=0,tt,cc;if(array[j][k+1]=='/'||array[j][k+1]=='\0')null=1;for(ii=k+1;array[j][ii]!='/'&&array[j][ii]!='\0';ii++){if(array[j][ii]<='Z'&&array[j][ii]>='A'){for(tt=0;tt<n;tt++)if(temp[tt][0]==array[j][ii])break;for(cc=1;temp[tt][cc]!='\0';cc++){if(temp[tt][cc]=='@')null=1;

14

Page 15: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

else fol[l][++f]=temp[tt][cc];} }else fol[l][++f]=array[j][ii];}if(null)follow(j);}void main(){int p[2],i,j;clrscr();printf("Enter the no. of productions :");scanf("%d",&n);printf("Enter the productions :\n");for(i=0;i<n;i++)scanf("%s",array[i]);for(i=0,t=0;i<n;i++,t++){c=0,p[0]=-1,p[1]=-1;temp[t][0]=array[i][0];fun(i,p);temp[t][++c]='\0';printf("First(%c) : [ ",temp[t][0]);for(j=1;j<c;j++)printf("%c,",temp[t][j]);printf("\b ].\n");getch();}for(i=0,l=0;i<n;i++,l++){f=-1;ff0=-1;fol[l][++f]=array[i][0];follow(i);fol[l][++f]='\0';}for(i=0;i<n;i++){printf("\nFollow[%c] : [ ",fol[i][0]);for(j=1;fol[i][j]!='\0';j++)printf("%c,",fol[i][j]);printf("\b ]");getch();

15

Page 16: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

}}Output : Enter the no. of productions :6Enter the productions :S/aBDhB/cCC/bC/@D/E/FE/g/@F/f/@First(S) : [ a ].First(B) : [ c ].First(C) : [ b,@ ].First(D) : [ g,@,f ].First(E) : [ g,@ ].First(F) : [ f,@ ].Follow[S] : [ $ ]Follow[B] : [ g,f,h,$ ]Follow[C] : [ g,f,h,$ ]Follow[D] : [ h ]Follow[E] : [ h ]Follow[F] : [ h ]

16

Page 17: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

7.) Write a program to implement the top-down parsing.

#include<stdio.h>#include<conio.h>#include<string.h>char out[20][20][20],nt[10],te[10],ip[20][20],first[20][20],dup[20];char ip3[20][20],ip2[20][20],follow[10][10],temp[5];int i,flag,ind1=0,n,k1=0,j;int k=0,teind,q,ct=0,dump,count,count1,test,slen=0;void getdata();void followcall(int);void cal();void topdown();void duplicate(int);void main(){int cnt=0;clrscr();getdata();cal();printf("NO OF ROW IS %d \t COLUMN IS %d:\n",ind1,k1);cnt=0;for(i=0;i<k1;i++)printf("\t%c",te[i]);for(i=0;i<ind1;i++){printf("\n\n%c ",nt[i]);for(j=0;j<k1;j++){if(strlen(out[i][j])>slen)cnt++;printf("%s\t ",out[i][j]);}printf("\n");}if(cnt>0)printf("It is not a LL(1) grammar\n");else{printf("It is a LL(1) grammar\n");topdown();}getch();

17

Page 18: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

}void getdata(){for(i=0;i<10;i++){for(j=0;j<10;j++)ip[i][j]=ip2[i][j]=ip3[i][j]=NULL;}printf("enter the no production\n");scanf("%d",&n);printf("enter the productionds\n");count=0,slen=0;for(i=0;i<n;i++){scanf("%s",&ip[i]);if(strlen(ip[i])>slen)slen=strlen(ip[i]);}q=0;for(i=0;i<n;i++){flag=0;for(j=0;j<n;j++){if(nt[j]==ip[i][0]){dup[q]=ip[i][0];q++;flag=1;}}if(flag==0){nt[ind1] = ip[i][0];ind1++;}}for(i=0;i<n;i++)if(ip[i][3]=='#'){for(j=i;j<n;j++)strcpy(ip[j],ip[j+1]);n--;}printf("enter the first values\n");k1=0;

18

Page 19: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

for(i=0;i<ind1;i++){scanf("%s",&first[i]);for(j=0;j<strlen(first[i]);j++){flag=0;for(k=0;k<=k1;k++)if((te[k]==first[i][j])||(first[i][j]=='#'))flag=1;if(flag==0){te[k1]=first[i][j];k1++;}}}printf("enter the follow values\n");for(i=0;i<ind1;i++){scanf("%s",&follow[i]);for(j=0;j<strlen(follow[i]);j++){flag=0;for(k=0;k<=k1;k++)if(te[k]==follow[i][j])flag=1;if(flag==0){te[k1]=follow[i][j];k1++;}}}count=0;for(i=0;i<ind1;i++){test=0;for(j=0;j<n;j++){if(ip[j][0]==nt[i])test++;if((test>1)&&(ip[j][0]==nt[i])){strcpy(ip2[count],ip[j]);count++;}

19

Page 20: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

}}count1=0;i=0;for(i=0;i<n;i++){test=0;for(j=0;j<count;j++)if(strcmp(ip2[j],ip[i])==0)test=1;if(test==0){strcpy(ip3[count1],ip[i]);count1++;}}}void cal(){int z,teindex,ntindex;for(i=0;i<count1;i++){for(j=0;j<strlen(first[i]);j++){test=0;for(k=0;k<count;k++) if(ip3[i][0]==ip2[k][0])test=1;if(test==1){duplicate(i);break;}Else{if(first[i][j]=='#')followcall(i);else{for(z=0;z<k1;z++)if(first[i][j]==te[z])teindex=z;for(z=0;z<count1;z++)if(ip3[i][0]==nt[z])ntindex=z;strcat(out[ntindex][teindex],ip3[i]);}

20

Page 21: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

}}}}void followcall( int a){int z,y;for(y=0;y<strlen(follow[a]);y++)for(z=0;z<=k1;z++)if(follow[a][y]==te[z]){temp[0]=nt[a];temp[1]='-';temp[2]='>';temp[3]='#';temp[4]='\0';strcat(out[a][z],temp);}}void duplicate( int a){int dup=0,z,teindex,ntindex,ct=0;for(z=0;z<k1;z++)if(first[a][0]==te[z]){teindex=z;ct++;}for(z=0;z<count1;z++)if(ip3[a][0]==nt[z])ntindex=z;strcat(out[ntindex][teindex],ip3[a]);for(z=0;z<count;z++)if(ip3[a][0]==ip2[z][0]){dup=z;break;}while(ip3[a][0]==ip2[dup][0]){for(z=0;z<count1;z++)if(ip2[dup][0]==nt[z])ntindex=z;strcat(out[ntindex][ct],ip2[dup]);dup++;ct++;

21

Page 22: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

}}void topdown(){int ct1=0,ct2,i,j,stindex=0,ipindex=0,ct3,c=0;char stack[10],iptd[10],temp1[10];for(i=0;i<10;i++)stack[i]='\0';stack[0]='$';stack[1]=ip3[0][0];printf("Enter ip string to parse!"); scanf("%s",&iptd);while(strlen(stack)>=1){if(strcmp(iptd,"$")!=0){if(stack[strlen(stack)-1]=='#')stack[strlen(stack)-1]='\0';c=0;for(j=0;j<ind1;j++){if(stack[strlen(stack)-1]==nt[j])stindex = j;elsec++;}if(c==ind1)break;c=0;for(j=0;j<k1;j++){if(iptd[0]==te[j])ipindex=j;elsec++;}if(c==k1)break;ct2=0;printf("\nStack is %s \t\t IP is %s \t\t Outis%s\n",stack,iptd,out[stindex][ipindex]);for(j=3;j<strlen(out[stindex][ipindex]);j++,ct2++)temp1[ct2]= out[stindex][ipindex][j];temp1[ct2]='\0';ct3=strlen(stack)-1;for(j=strlen(temp1)-1;j>=0;j--){

22

Page 23: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

stack[ct3] = temp1[j];ct3++;}c=0; if(stack[strlen(stack)-1]==iptd[0]){stack[strlen(stack)-1] = '\0';for(j=0;j<strlen(iptd)-1;j++){iptd[j]=iptd[j+1];c++;}if(c!=0)iptd[j]='\0';}}Elsebreak;}if(strcmp(iptd,"$")==0){while(strlen(stack)>1){printf("\nStack is %s\t\t IP is %s \t\t Out is%c->#\n",stack,iptd,stack[strlen(stack)-1]);stack[strlen(stack)-1]=NULL;}for(i=0;i<10;i++)stack[i]='\0';stack[0]='$';printf("\nStack is %s\t\t IP is %s\n",stack,iptd);printf("The string is accepted\n");}elseprintf("The string is not accepted\n");}

23

Page 24: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

8.) Write the program to implement the operator precedence parsing.

#include<stdio.h>#include<conio.h>int find(char a){switch(a){case 'a':return 0;case '+':return 1;case '*':return 2;case '$':return 3;}}void display(char a[],int top1,char b[],int top2){int i;for(i=0;i<=top1;i++)printf("%c",a[i]);printf("\t");for(i=top2;i<strlen(b);i++)printf("%c",b[i]);printf("\n");}int main(){char table[][4]= {' ','>','>','>','<','<','<','>','<','>','<','>','<','<','<',' '};char input[10];char stack[10]={'$'};int top_stack=0,top_input=0,i,j;clrscr();printf("operator precedence parsing for E->E+E/E*E/a\n");printf("enter the string\n");scanf("%s",input);strcat(input,"$");

24

Page 25: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

printf("stack\tinput\n");display(stack,top_stack,input,top_input);while(1){if((stack[top_stack]=='$')&&(input[top_input]=='$'))

{printf("string accepted");break;}if(table[find(stack[top_stack])][find(input[top_input])]==' '){printf("parse error");getch();exit(0);}if(table[find(stack[top_stack])][find(input[top_input])]=='<'){stack[++top_stack]='<';stack[++top_stack]=input[top_input];top_input++;display(stack,top_stack,input,top_input);continue;}if(table[find(stack[top_stack])][find(input[top_input])]=='>'){stack[++top_stack]='>';display(stack,top_stack,input,top_input);top_stack-=3;display(stack,top_stack,input,top_input);}}getch();}

25

Page 26: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

OUTPUT:Operator precedence parsing for E->E+E/E*E/a Enter the stringa+a*a

stack input$ a+a*a$$<a +a*a$$<a> +a*a$$ +a*a$$<+ a*a$$<+<a *a$$<+<a> *a$$<+ *a$$<+<* a$$<+<*<a $$<+<*<a> $$<+<* $$<+<*> $$<+ $$<+> $$ $ String accepted

26

Page 27: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

9.) Write a program to implement the parse shift reduce parser in c.

#include<stdio.h>#include<conio.h>void push(char,char[],int);void pop(int);void main(){char a[4][10]={{'E','=','E','+','E'},{'E','=','E','*','E'},{'E','=','(','E',')'},{'E','=','id'}};char st[20],item;int top=0;st[top]='$';char str[10]={'id','+','(','id','*','id',')'};clrscr();for(int j=0;j<=7;j++){push(str[j],st,top);if(st[top]==a[3][2]){pop(top);push('E',st,top);}else{for(int i=0;i<4;i++){if(st[top]==a[i][2]){if(st[top--]==a[i][3])if(st[top--]==a[i][4]){pop(top);push(a[i][1],st,top);} } } } }if(st[top]=='$')printf("accept");getch();}void push(char item,char st[],int top)

27

Page 28: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

{top=top+1;st[top]=item;}void pop(int top){ top=top-1;}

OUTPUT:Enter the String : id+id*id

Stack String Action…………………………………………..$id +id*id$ Shift$E +id*id$ Reduce$E+ id*id$ Shift$E+id *id$ Shift$E+E *id$ Reduce$E+E* id$ Shift$E+E*id $ Shift$E+E*E $ Reduce$E+E $ Reduce$E $ Reduce

The Given String is Valid

28

Page 29: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

11.)

Implement the following expression into quadruple.(a+b+c*d/e+f).

29

Op arg1 arg2 Result(0) + f T1(1) / d e T2(2) * c T2 T3(3) + T3 T1 T4(4) + b T4 T5(5) + a T5 T6

Page 30: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

12.) Implement constant folding technique of code optimization.

#include <stdio.h>#include<conio.h>int main(){int x, y, z;x = 10;y = x + 45;z = y + 4;printf("The value of z = %d", z);return 0;}

Output / Conclusion :Constant folding, the compiler evaluates the various expressions in the program only once and plugs the final value into the generated code. One more interesting thing to observe is that after constant folding, there is no need of the variables x, y and z. Therefore no space for them is allocated on the stack, thus reducing the memory requirement of the program. This brings out the fact that one optimization may lead to another one. In the above case constant folding lead to a decrease in run time (since all the computations are done at compile time) and also to a decrease in the space requirements. Optimization is done.

30

Page 31: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

13.) Implement Common sub-expression Elimination technique of code optimization.

#include <stdio.h>#include<conio.h>int main(){int a, b;int x, y, z;scanf("%d %d", &a, &b);x = a * b;if(b >= 4){y = a * b;z = 0;}Else{z = a * b * 4;y = 0;}printf("x = %d, y = %d, z = %d\n", x, y, z);return 0;}

Output / Conclusion :This program has an example of a common sub expression. The expression a * b is evaluated the first time, and then again later. The last two evaluations of a * bare redundant since the value of neither a nor b changes after the first evaluation. Thus these two evaluations are common sub expressions that can be eliminated. The first thing to notice is that now only variables a and b are stored on the stack, hence a

31

Page 32: Compiler Design Lab File€¦ · Web viewCompiler Design Lab File

stack of just 8 bytes is required as opposed to a 20 byte stack for the not optimized version. You may wonder what's so special about variables a and b. The specialty is that the address of variables a and b is used in the program (for scanf()) and variables that reside in the registers cannot have a memory address.

14.) Implement Strength Reduction using Induction Variable technique of code.

#include<stdio.h>#include<conio.h>int main(){int i, j;for(i = 0; i < 10; i++){j = i * 7;printf("i = %d, j = %d", i, j);}return 0;}Output / Conclusion :Here i is the loop variable whereas j is the induction variable as j always changes in lock step with I . Here, the compiler has adjusted the order of the statements in the loop so as to enable it to use strength reduction. After analyzing the program, the compiler sees that inside the loop, the value of i is always going to increase by 1 and since j is an induction variable, its value is always going to increase by 7. Therefore, instead of multiplying i by 7 (which is costly), it adds 7 to the old value of j before going on to the next iteration. Thus a costly operation of multiplication has been replaced by addition which is cheaper (in terms of time).

32