LABORATORY ACTIVITY MANUAL

53 downloads 187 Views 1MB Size Report
Jan 6, 2016 - Develop a lexical analyzer to recognize a few patterns in C. (Ex. identifiers, constants, ... Implement co
www.vidyarthiplus.com

P.S.R.RENGASAMY COLLEGE OF ENGINEERING FOR WOMEN

LABORATORY ACTIVITY MANUAL DEPT: COMPUTER SCIENCE AND ENGINEERING SUB: CS6612

COMPILER LABORATORY SEMESTER: VI

ABSTRACT The goal is to present overview of the design principles of a Compiler. The course has been designed to provide knowledge on series of compiler activities. At the end of the course the students will be able to:Be exposed to compiler writing tools. Learn to implement the different Phases of compiler Be familiar with control flow and "); strcpy(QUAD[Index].result,$1); strcpy($$,QUAD[Index++].result); } ; EXPR: EXPR '+' EXPR {AddQuadruple("+",$1,$3,$$);} |EXPR '-' EXPR {AddQuadruple("-",$1,$3,$$);} | EXPR '*' EXPR {AddQuadruple("*",$1,$3,$$);} | EXPR '/' EXPR {AddQuadruple("/",$1,$3,$$);} | '-' EXPR {AddQuadruple("UMIN",$2,"",$$);} | '(' EXPR ')' {strcpy($$,$2);} | VAR | NUM ; CONDST:IFST{ Ind=pop(); sprintf(QUAD[Ind].result,"%d",Index); Ind=pop(); sprintf(QUAD[Ind].result,"%d",Index);} | IFST ELSEST ; IFST: IF '(' CONDITION ')'{ strcpy(QUAD[Index].op,"=="); strcpy(QUAD[Index].arg1,$3); strcpy(QUAD[Index].arg2,"FALSE"); strcpy(QUAD[Index].result,"-1"); 29

www.Vidyarthiplus.com

Section No Page28 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –5 Issue Date Rev No

Rev Date

6.1.2016

push(Index); Index++; } BLOCK{ strcpy(QUAD[Index].op,"GOTO"); strcpy(QUAD[Index].arg1,""); strcpy(QUAD[Index].arg2,""); strcpy(QUAD[Index].result,"-1"); push(Index); Index++ ; } ;ELSEST: ELSE{ tInd=pop(); Ind=pop(); push(tInd); sprintf(QUAD[Ind].result,"%d",Index); } BLOCK{ Ind=pop(); sprintf(QUAD[Ind].result,"%d",Index); } ; CONDITION: VAR RELOP VAR {AddQuadruple($2,$1,$3,$$); StNo=Index-1; } | VAR | NUM ; WHILEST:WHILELOOP{ Ind=pop(); sprintf(QUAD[Ind].result,"%d",StNo); Ind=pop(); sprintf(QUAD[Ind].result,"%d",Index); } ; WHILELOOP: WHILE '(' CONDITION ')' { strcpy(QUAD[Index].op,"=="); strcpy(QUAD[Index].arg1,$3); strcpy(QUAD[Index].arg2,"FALSE"); 30

www.Vidyarthiplus.com

Section No Page29 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –5 Issue Date Rev No

Rev Date

6.1.2016

Section No Page 30 of 55

strcpy(QUAD[Index].result,"-1"); push(Index); Index++; } BLOCK { strcpy(QUAD[Index].op,"GOTO"); strcpy(QUAD[Index].arg1,""); strcpy(QUAD[Index].arg2,""); strcpy(QUAD[Index].result,"-1"); push(Index); Index++; }; %% extern FILE *yyin; int main(int argc,char *argv[]) { FILE *fp; int i; if(argc>1) { fp=fopen(argv[1],"r"); if(!fp) { printf("\n File not found"); exit(0); } yyin=fp; } yyparse(); printf("\n\n\t\t ----------------------------""\n\t\t Pos Operator Result" "\n\t\t--------------------"); for(i=0;inext->left)) flag+=1; if(!strcmp(store[4],temp->next->right)) flag+=2; if(flag!=0) break; temp=temp->next; } t=head; while(t->next!=null) t=t->next; if(flag==0) { ptr=(struct node*)malloc(sizeof(struct node)); t->next=ptr; if(s==5) strcpy(ptr->id,store[3]); else strcpy(ptr->id,strcat(store[3],store[5])); t=head; while(t->next!=null) { if(!strcmp(t->next->attach[0],store[2])) { f1=1; break; } if(strcmp(t->next->attach[1],””)) if(!strcmp(t->next->attach[1],store[2])) { f1 = 1; break; } t=t->next; } if(f1) 43

www.Vidyarthiplus.com

Section No Page 42 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –8 Issue Date Rev No

Rev Date

6.1.2016

strcpy(ptr->left,t->next->id); else strcpy(ptr->left,store[2]); f1=0; t=head; while(t->next!=null) { if(!strcmp(t->next->attach[0],store[4])) { f1 = 1; break; } if(strcmp(t->next->attach[1],””)) if(!strcmp(t->next->attach[1],store[4])) { f1= 1; break; } t=t->next; } if(f1) strcpy(ptr->right,t->next->id); else strcpy(ptr->right,store[0]); strcpy(ptr->attach[1],,””); ptr->next=null; } else if(flag==3) strcpy(temp->next->attack[1],store[0]; } if(s==3) { while(temp->next!=null) { if(!strcmp(store[2],temp->next->attach[0])) break; temp=temp->next; } strcpy(temp->next->attach[1],store[0]); } } int main() 44

www.Vidyarthiplus.com

Section No Page 43 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –8 Issue Date Rev No

Rev Date

6.1.2016

{ struct node *temp; struct node *t; clrscr(); f=fopen(“input.text”,”r”); head=(struct node*)malloc(size of (struct node)); head->next=null; while(!feof(f)) { fscanf(f,”%s”,str); if(!strcmp(str,”;”)) { construct _tree(); s=0; }else strcpy(store[s++],str); }printf(“\n\nid\tleft\tright\tattached ids\n\n”); temp=head; while(temp->next!=null) { printf(“\n\n%s\t%s\t%s\t%s,”temp->next->id, temp->next->left,temp->next->right,temp->next->attach[0]); if(strcmp(temp->next->attach[1],””)) printf(“\t%s”,temp->next->attach[1]); temp=temp->next; } getch(); return 0;} 7.0 Criteria for evaluation: INPUT: t1 = 4 * I; t2 = a[t1]; t3 = 4 *I; t4= b [t3]; t5= t2 *t4; t6 = prod + t5; prod = 16; t7 = i+1; i=t7;

45

www.Vidyarthiplus.com

Section No Page 44 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01 OUTPUT: ID LEFT * 4 [] a [] b * [] + prod + I

Issue No 01

RIGHT I * * [] * |

EXERCISE –8 Issue Date Rev No

Rev Date

6.1.2016

ATTACHED t1 t2 t4 t5 t6 t7

IDs t3

prod i

8.0 Safety Precaution- NA 9.0 Records- NA 10.0 Result: Thus the above the program is executed and the required output is obtained.

46

www.Vidyarthiplus.com

Section No Page 45 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –9 Issue Date Rev No

Rev Date

6.1.2016

Section No Page 46 of 55

IMPLEMENTATION THE BACK END OF COMPILER 1.0 Aim: To write a C program to implement the Back end of the compiler. 2.0 Reference – NA 3.0 List of Apparatus: Hardware Requirements: Processsor: Pentium I RAM: 128MB Hard Disk 40 GB Floppy Drive 1.44MB Software Requirements: Language: C/C++ 4.0 Principle: NA 5.0 Steps: 1) St ar t t he pr o g r a m. 2)Get the three variables from statements and stored in the text file k.txt. 3)Compile the program and give the pat h of the source file. 4)Execute the program. 5)Target code for the given statement was produced. 6)Stop the program. 6.0 Program: #include #include #include #include void main() { int i=2,j=0,k=2,k1=0; char ip[10],kk[10]; FILE *fp;clrscr(); printf("\nEnter the filename of the intermediate code"); scanf("%s",&kk); fp=fopen(kk,"r"); if(fp==NULL) { printf("\nError in Opening the file"); getch(); }clrscr(); while(!feof(fp)) 47

www.Vidyarthiplus.com

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –9 Issue Date Rev No

Rev Date

6.1.2016

{ fscanf(fp,"%s\n",ip); printf("\t\t%s\n",ip); } rewind(fp); printf("\n------------------------------\n"); printf("\tStatement \t\t target code\n"); printf("\n------------------------------\n"); while(!feof(fp)) { fscanf(fp,"%s",ip); printf("\t%s",ip); printf("\t\tMOV %c,R%d\n\t",ip[i+k],j); if(ip[i+1]=='+') printf("\t\tADD"); else printf("\t\tSUB"); if(islower(ip[i])) printf("%c,R%d\n\n",ip[i+k1],j); else printf("%c,%c\n",ip[i],ip[i+2]); j++; k1=2; k=0;} printf("\n-------------------------------\n"); getch(); fclose(fp); }

48

www.Vidyarthiplus.com

Section No Page 47 of 55

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –9 Issue Date Rev No

Rev Date

6.1.2016

Section No Page 48 of 55

7.0 Criteria for evaluation:

8.0 Safety Precaution- NA 9.0 Records- NA 10.0 Result: Thus the above the program is executed and the required output is obtained.

49

www.Vidyarthiplus.com

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –10 Issue Date Rev No

Rev Date

6.1.2016

Section No Page 49 of 55

IMPLEMENTATION THE FRONT END OF COMPILER 1.0 Aim: To write a C program to implement the front end of the compiler. 2.0 Reference – NA 3.0 List of Apparatus: Hardware Requirements: Processsor: Pentium I RAM: 128MB Hard Disk 40 GB Floppy Drive 1.44MB Software Requirements: Language: C/C++ 4.0 Principle: NA 5.0 Steps: 1) Start the program. 2 ) G e t t he c o d i n g fr o m t he u s e r . 3) Find the operators, arguments and results from the coding. 4 ) D i s p l a y t h e va l u e i n t h e t a b l e . 5) Stop the program. 6.0 Program: #include #include #include void main() { char pg[100][100],str1[24]; int tem=-1,ct=0,i=-1,j=0,j1,pos=-1,t=-1,flag,flag1,tt=0,fg=0; clrscr(); printf("Enter the codings \n"); while(i>-2) { i++; lab1: t++; scanf("%s",&pg[i]); 50

www.Vidyarthiplus.com

www.vidyarthiplus.com LABORATORY ACTIVITY MANUAL

P.s.r rengasamy college of engineering for women Doc No PSRR/CL/01

Issue No 01

EXERCISE –10 Issue Date Rev No

Rev Date

6.1.2016

if((strcmp(pg[i],"getch();"))==0) { i=-2; goto lab1; } } printf("\n pos \t oper \t arg1 \t arg2 \tresult \n"); while(j=1 ; i--) fact = fact * i; cout