Cs6612-Compiler Laboratory
Cs6612-Compiler Laboratory
ANNE’S
COLLEGE OF ENGINEERING AND TECHNOLOGY
(Approved by AICTE New Delhi, Affiliated to Anna University, Chennai)
(An ISO 9001:2015 Certified Institution)
ANGUCHETTYPLAYAM, PANRUTI – 607 106.
LAB MANUAL
Regulation 2013
PREPARED BY
1
SYLLABUS
CS6612 COMPILER LABORATORY LTPC 0 0 3 2
OBJECTIVES:
The student should be made to:
✓ Be exposed to compiler writing tools.
✓ Learn to implement the different Phases of compiler
✓ Be familiar with control flow and data flow analysis
✓ Learn simple optimization techniques
LIST OF EXPERIMENTS:
1. Implementation of Symbol Table
2. Develop a lexical analyzer to recognize a few patterns in C.
(Ex. identifiers, constants, comments, operators etc.)
3. Implementation of Lexical Analyzer using Lex Tool
4. Generate YACC specification for a few syntactic categories.
a) Program to recognize a valid arithmetic expression that usesoperator +, - , * and /.
b) Program to recognize a valid variable which starts with a letter followed by any number of letters or
digits.
c)Implementation of Calculator using LEX and YACC
5. Convert the BNF rules into Yacc form and write code to generate Abstract Syntax Tree.
6. Implement type checking
7. Implement control flow analysis and Data flow Analysis
8. Implement any one storage allocation strategies(Heap,Stack,Static)
9. Construction of DAG
10. Implement the back end of the compiler which takes the three address code and produces the 8086
assembly language instructions that can be assembled and run using a 8086 assembler. The target
assembly instructions can be simple move, add, sub, jump. Also simple addressing modes are used.
11. Implementation of Simple Code Optimization Techniques (Constant Folding., etc.)
TOTAL: 45 PERIODS
OUTCOMES:
At the end of the course, the student should be able to
✓ Implement the different Phases of compiler using tools
✓ Analyze the control flow and data flow of a typical program
✓ Optimize a given program
✓ Generate an assembly language program equivalent to a source language program
2
TABLE OF CONTENTS
Staff
EXP Date Experiment Name Marks Sign
/NO.
1
Write a C program to implementation of Symbol Table.
3
Implementation of Lexical Analyzer using Lex Tool
5 Convert the BNF rules into Yacc form and write code to
generate Abstract Syntax Tree
7 Implementation of Stack
( storage allocation strategies)
8 Construction of DAG
9
Implementation of the back end of the compiler
3
Ex.No : 1
DATE:
AIM:
ALGORITHM:
1. Start the program for performing insert, display, delete, search and modify option in symbol table
2. Define the structure of the Symbol Table
3. Enter the choice for performing the operations in the symbol Table
4. If the entered choice is 1, search the symbol table for the symbol to be inserted. If the symbol is already
present, it displays “Duplicate Symbol”. Else, insert the symbol and the corresponding address in the symbol
table.
5. If the entered choice is 2, the symbols present in the symbol table are displayed.
6. If the entered choice is 3, the symbol to be deleted is searched in the symbol table. If it is not found in the
symbol table it displays “Label Not found”. Else, the symbol is deleted.
7. If the entered choice is 5, the symbol to be modified is searched in the symbol table. The label or address or
both can be modified.
4
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char var[10];
int value;
};
struct table tbl[20];
int i,j,n;
void create();
void modify();
int search(char variable[],int n);
void insert();
void display();
void main()
{
int ch,result=0;
char v[10];
clrscr();
do
{
printf("Enter your choice\n1.Create\n2.Insert\n3.Modify\n4.Search\n5.Display\n6.Exit:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
modify();
break;
case 4:
printf("Enter the variable to be searched for:\n");
scanf("%s",&v);
result=search(v,n);
if(result==0)
printf("The variable does not belong to the table\n");
else
5
printf("The location of the variable is %d\nThe value of %s is
%d\n",result,tbl[result].var,tbl[result].value);
break;
case 5:
display();
break;
case 6:
exit(1);
}
}
while(ch!=6);
getch();
}
void create()
{
printf("Enter the no. of entries:");
scanf("%d",&n);
printf("Enter the variable and the value:\n");
for(i=1;i<=n;i++)
{
scanf("%s%d",tbl[i].var,&tbl[i].value);
check:
if(tbl[i].var[0]>='0'&&tbl[i].var[0]<='9')
{
printf("The variable should start with an alphabet\nEnter correct variable name:\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<i;j++)
{
if(strcmp(tbl[i].var,tbl[j].var)==0)
{
printf("The variable already exists.Enter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
}
printf("The table after creation is:\n");
display();
}
void insert()
{
if(i>=20)
printf("Cannot insert.Table is full\n");
else
{
6
n++;
printf("Enter the value and variable\n");
scanf("%s%d",tbl[n].var,&tbl[n].value);
check:
if(tbl[i].var[0]>='0'&&tbl[i].var[0]<='9')
{
printf("The variable should start with an alphabet\nEnter correct variable name:\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<n;j++)
{
if(strcmp(tbl[j].var,tbl[i].var)==0)
{
printf("The variable already exist.Enter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
printf("The table after insertion is:\n");
display();
}
}
void modify()
{
char variable[10];
int result=0;
printf("Enter the variable to be modified\n");
scanf("%s",&variable);
result=search(variable,n);
if(result==0)
printf("%s does not belong to table\n",variable);
else
{
printf("The current value of the variable %s is %d\nEnter the new variable and its
value\n",tbl[result].var,tbl[result].value);
scanf("%s%d",tbl[result].var,&tbl[result].value);
check:
if(tbl[i].var[0]>='0'&&tbl[i].var[0]<='9')
{
printf("The variable should start with an alphabet\nEnter correct variable name:\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
}
printf("The table after modification is:\n");
display();
7
}
int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
{
if(strcmp(tbl[i].var,variable)==0)
{
flag=1;
break;
}
}
if(flag==1)
return i;
else
return 0;
}
void display()
{
printf("VARIABLE\t VALUE\n");
for(i=1;i<=n;i++)
printf("%s\t\t%d\n",tbl[i].var,tbl[i].value);
}
8
OUTPUT
9
Enter the new variable and its value
RIM 40
VARIABLE VALUE
AIM 45
RIM 40
BALL 56
SIM 25
Enter your choice
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit:6
RESULT:
Thus the C program to implement Symbol Table was executed and verified successfully.
10
Ex.No :2
DATE:
AIM:
To develop a lexical analyzer to recognize a few patterns in C.
ALGORITHM:
11
PROGRAM:
12
else //If Check succeeds
analyze();
printf(“\nEnd of file\n”);
getch();
} //End of Main function
int isdelim(char c) //Function to check if the character retrieved from the file is a delimiter.
{
int i;
for(i=0;i<14;i++)
{
if(c==delim[i])
return 1;
}
return 0;
}
int isop(char c) //Function to check if the character retrieved from the file is an operator.
15
{
int i,j;
char ch;
for(i=0;i<7;i++)
{
if(c==oper[i])
{
ch=getc(fp);
for(j=0;j<6;j++) //In case the operator is like ‘++’ or ‘--’, etc.
{
if(ch==oper[j])
{
fop=1;
sop=ch;
return 1;
}
}
ungetc(ch,fp);
return 1;
}
}
return 0;
}
void check(char t[]) //Function to check if the token is an identifier, keyword, header file name
{
int i;
if(numflag==1)
{
printf(“\nNumber\t\t %s”,t);
return;
}
for(i=0;i<2;i++)
{
if(strcmp(t,predirect[i])==0)
{
printf(“\nPreprocessor directive %s”,t);
return;
16
}
}
for(i=0;i<6;i++)
{
if(strcmp(t,header[i])==0)
{
printf(“\nHeader file\t %s”,t);
return;
}
}
for(i=0;i<21;i++)
{
if(strcmp(key[i],t)==0)
{
printf(“\nKeyword\t\t %s”,key[i]);
return;
}
}
printf(“\nIdentifier\t %s”,t);
}
void skipcomment() //Function to skip over the comment statements in the file.
{
ch=getc(fp);
if(ch==‘/’) //Checking single line comments
{
while((ch=getc(fp))!=‘\n’);
}
else if(ch==‘*’) //Checking multiple line comments.
{
while(f==0)
{
ch=getc(fp);
if(ch==‘*’)
{
c=getc(fp);
if(c==‘/’)
17
f=1;
}
}
f=0;
}
}
OUTPUT
Enter filename : iplex.c
Delimitter #
Preprocessor directive include
Delimitter <
Header file stdio.h
Delimitter >
Delimitter
Delimitter #
Preprocessor directive include
Delimitter <
Header file conio.h
Delimitter >
Delimitter
Keyword void
Delimitter
Identifier main
18
Delimitter (
Delimitter )
Delimitter
Delimitter {
Delimitter
Identifier clrscr
Delimitter (
Delimitter )
Delimitter ;
Delimitter
Identifier printf
Delimitter (
Delimitter )
Delimitter ;
Delimitter
Identifier getch
Delimitter (
Delimitter )
Delimitter ;
Delimitter
Delimitter }
End of file
RESULT
Thus the C program for implementation of a lexical analyzer to recognize a few patterns was executed
and verified successfully.
19
Ex. No: 3
DATE:
AIM:
To write a ‘C’ program to implement a lexical analyzer for separation of tokens using LEX Tool.
ALGORITHM:
Step 3: Check for the given list of keywords and print them as keyword if it is encountered.
Step 5: For a function, print the beginning and ending of the function block.
Step 6: Similarly print the corresponding statements for numbers, identifiers and assignment operators.
Step 7: In the main function get the input file as argument and open the file in read mode.
Step 8: Then read the file and print the corresponding lex statement given above.
20
PROGRAM 1:
%{
#include<stdio.h>
%}
%%
if|else|while|int|switch|for {printf("%s is a keyword",yytext);}
[a-z|A-Z]([a-z|A-Z]|[0-9])* {printf("%s is an identifier",yytext);}
[0-9]* {printf("%s is a number",yytext);}
%%
int main()
{
yylex();
return 0;
}
int yywrap()
{
}
OUTPUT
21
PROGRAM 2:
%{
%}
identifier [a-z|A-Z]|[a-z|A-Z|0-9]*
%%
#.* {printf("\n%s is a preprocessor dir",yytext);}
int {printf("\n\t%s is a keyword",yytext);}
{identifier}\( {printf("\n\nFUNCTION\n\t%s",yytext);}
\{ {printf("\nBLOCK BEGINS");}
\} {printf("\nBLOCK ENDS");}
{identifier} {printf("\n%s is an IDENTIFIER",yytext);}
. | \n
%%
int yywrap()
{
return 0;
}
Input ( in.c )
#include<stdio.h>
main()
{
int a ;
}
22
OUTPUT:
RESULT:
Thus the C program for the implementation of lexical analyzer using LEX Tool was executed successfully.
23
Ex. No: 4 (a)
DATE:
GENERARATION OF YACC SPECIFICATION
RECOGNIZING A VALID ARITHMETIC EXPRESSION
AIM:
To write a program to recognize a valid arithmetic expression that uses operator +, - , * and / using YACC tool.
ALGORITHM:
LEX
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions to identify valid arithmetic expression token of lexemes.
3. LEX call yywrap() function after input is over. It should return 1 when work is done or should return 0
when more processing is required.
YACC
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. Define tokens in the first section and also define the associativity of the operations
3. Mention the grammar productions and the action for each production.
4. $$ refer to the top of the stack position while $1 for the first value, $2 for the second value in the stack.
5. Call yyparse() to initiate the parsing process.
6. yyerror() function is called when all productions in the grammar in second section doesn't match to the
input statement.
24
PROGRAM:
//art_expr.l
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[a-zA-Z][0-9a-zA-Z]* {return ID;}
[0-9]+ {return DIG;}
[ \t]+ {;}
. {return yytext[0];}
\n {return 0;}
%%
int yywrap()
{
return 1;
}
//art_expr.y
%{
#include<stdio.h>
%}
%token ID DIG
%left '+''-'
%left '*''/'
%right UMINUS
%%
stmt:expn ;
expn:expn'+'expn
|expn'-'expn
|expn'*'expn
|expn'/'expn
|'-'expn %prec UMINUS
|'('expn')'
|DIG
|ID
25
;
%%
int main()
{
printf("Enter the Expression \n");
yyparse();
printf("valid Expression \n");
return 0;
}
int yyerror()
{
printf("Invalid Expression");
exit(0);
}
OUTPUT
RESULT:
Thus the program to recognize a valid arithmetic expression that uses operator +, - , * and / using YACC tool was
executed and verified successfully.
26
Ex. No: 4 (b)
DATE:
RECOGNIZING A VALID VARIABLE
AIM:
To write a program to recognize a valid variable which starts with a letter followed by any number of letters or
digits using YACC tool.
ALGORITHM:
LEX
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions or patterns to identify token of lexemes for recognize a valid variable.
3. Lex call yywrap() function after input is over. It should return 1 when work is done or should return 0
when more processing is required.
YACC
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. Define tokens in the first section and also define the associativity of the operations
3. Mention the grammar productions and the action for each production.
4. $$ refer to the top of the stack position while $1 for the first value, $2 for the second value in the stack.
5. Call yyparse() to initiate the parsing process.
6. yyerror() function is called when all productions in the grammar in second section doesn't match to the
input statement.
27
PROGRAM:
//valvar.l
%{
#include "y.tab.h"
%}
%%
[a-zA-Z] {return LET;}
[0-9] {return DIG;}
. {return yytext[0];}
\n {return 0;}
%%
int yywrap()
{
return 1;
}
//valvar.y
%{
#include<stdio.h>
%}
%token LET DIG
%%
variable:var
;
var:var DIG
|var LET
|LET
;
%%
int main()
{
printf("Enter the variable:\n");
yyparse();
printf("Valid variable \n");
28
return 0;
}
int yyerror()
{
printf("Invalid variable \n");
exit(0);
}
OUTPUT:
RESULT:
Thus the program to recognize a valid variable which starts with a letter followed by any number of letters or
digits using YACC tool was executed and verified successfully.
29
Ex.NO: 4(c)
DATE:
AIM:
To write a program to implement Calculator using LEX and YACC.
ALGORITHM:
30
PROGRAM:
cal.l
DIGIT [0-9]+
%option noyywrap
%%
%%
cal.y
%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUM
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%right UMINUS
%%
31
OUTPUT:
RESULT:
Thus the program for implementing calculator using LEX and YACC is executed and verified.
32
Ex.No:5
DATE:
CONVERSION OF THE BNF RULES INTO YACC FORM AND GENERATION ABSTRACT SYNTAX
TREE
AIM:
To write the program to convert the BNF rules into YACC form and write code to generate abstract syntax
tree.
ALGORITHM:
33
PROGRAM:
<int.l>
%{
#include"y.tab.h"
#include<stdio.h>
#include<string.h>
int LineNo=1;
%}
identifier [a-zA-Z][_a-zA-Z0-9]*
number [0-9]+|([0-9]*\.[0-9]+)
%%
main\(\) return MAIN;
if return IF;
else return ELSE;
while return WHILE;
int |
char |
float return TYPE;
{identifier} {strcpy(yylval.var,yytext);
return VAR;}
{number} {strcpy(yylval.var,yytext);
return NUM;}
\< |
\> |
\>= |
\<= |
== {strcpy(yylval.var,yytext);
return RELOP;}
[ \t] ;
\n LineNo++;
. return yytext[0];
%%
<int.y>
%{
#include<string.h>
#include<stdio.h>
struct quad
{
char op[5];
char arg1[10];
char arg2[10];
char result[10];
}QUAD[30];
struct stack
{
int items[100];
int top;
}stk;
int Index=0,tIndex=0,StNo,Ind,tInd;
extern int LineNo;
%}
%union
34
{
char var[10];
}
%token <var> NUM VAR RELOP
%token MAIN IF ELSE WHILE TYPE
%type <var> EXPR ASSIGNMENT CONDITION IFST ELSEST WHILELOOP
%left '-' '+'
%left '*' '/'
%%
PROGRAM : MAIN BLOCK
;
BLOCK : '{' CODE '}'
;
CODE: BLOCK
| STATEMENT CODE
| STATEMENT
;
STATEMENT: DESCT ';'
| ASSIGNMENT ';'
| CONDST
| WHILEST
;
DESCT: TYPE VARLIST
;
VARLIST: VAR ',' VARLIST
| VAR
;
ASSIGNMENT: VAR '=' EXPR{
strcpy(QUAD[Index].op,"=");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"");
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);
35
strcpy(QUAD[Index].arg2,"FALSE");
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++;
};
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");
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;
36
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 Arg1 Arg2 Result" "\n\t\t
--------------------");
for(i=0;i<Index;i++)
{
printf("\n\t\t %d\t %s\t %s\t %s\t
%s",i,QUAD[i].op,QUAD[i].arg1,QUAD[i].arg2,QUAD[i].result);
}
printf("\n\t\t -----------------------");
printf("\n\n");
return 0;
}
void push(int data)
{
stk.top++;
if(stk.top==100)
{
printf("\n Stack overflow\n");
exit(0);
}
stk.items[stk.top]=data;
}
int pop()
{
int data;
if(stk.top==-1)
{
printf("\n Stack underflow\n");
exit(0);
}
data=stk.items[stk.top--];
return data;
}
void AddQuadruple(char op[5],char arg1[10],char arg2[10],char result[10])
{
strcpy(QUAD[Index].op,op);
strcpy(QUAD[Index].arg1,arg1);
strcpy(QUAD[Index].arg2,arg2);
sprintf(QUAD[Index].result,"t%d",tIndex++);
strcpy(result,QUAD[Index++].result);
}
37
yyerror()
{
printf("\n Error on line no:%d",LineNo);
}
Input
$vi test.c
main()
{
int a,b,c;
if(a<b)
{
a=a+b;
}
while(a<b)
{
a=a+b;
}
if(a<=b)
{
c=a-b;
}
else
{
c=a+b;
}
}
38
OUTPUT:
RESULT:
Thus the program to convert the BNF rules into YACC forms and writes code to generate abstract syntax tree was
executed successfully.
39
Ex.No: 6
DATE:
AIM:
ALGORITHM:
40
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int count=1,i=0,j=0,l=0,findval=0,k=0,kflag=0;
char key[4][12]= {"int","float","char","double"};
char dstr[100][100],estr[100][100];
char token[100],resultvardt[100],arg1dt[100],arg2dt[100];
void entry();
int check(char[]);
int search(char[]);
void typecheck();
struct table
{
char var[10];
char dt[10];
};
struct table tbl[20];
void main()
{
clrscr();
printf("\n IMPLEMENTATION OF TYPE CHECKING \n");
i=0;
printf("\n SEMANTIC ANALYZER(TYPE CHECKING): \n");
while(strcmp(dstr[i],"END"))
{
entry();
printf("\n");
i++;
}
l=0;
while(strcmp(estr[l],"END"))
{
typecheck();
printf("\n");
41
l++;
}
void typecheck()
{
memset(token,0,strlen(token));
j=0;
k=0;
while(estr[l][j]!='=')
42
{
token[k]=estr[l][j];
k++;
j++;
}
findval=search(token);
if(findval>0)
{
strcpy(resultvardt,tbl[findval].dt);
findval=0;
}
else
{
printf("Undefined Variable\n");
}
k=0;
memset(token,0,strlen(token));
j++;
while(((estr[l][j]!='+')&&(estr[l][j]!='-')&&(estr[l][j]!='*')&&(estr[l][j]!='/')))
{
token[k]=estr[l][j];
k++;
j++;
}
findval=search(token);
if(findval>0)
{
strcpy(arg1dt,tbl[findval].dt);
findval=0;
}
else
{
printf("Undefined Variable\n");
}
k=0;
memset(token,0,strlen(token));
j++;
while(estr[l][j]!=';')
{
token[k]=estr[l][j];
k++;
j++;
}
findval=search(token);
if(findval>0)
{
strcpy(arg2dt,tbl[findval].dt);
findval=0;
}
else
{
printf("Undefined Variable\n");
}
if(!strcmp(arg1dt,arg2dt))
{
43
if(!strcmp(resultvardt,arg1dt))
{
printf("\tThere is no type mismatch in the expression %s ",estr[l]);
}
else
{
printf("\tLvalue and Rvalue should be same\n");
}
}
else
{
printf("\tType Mismatch\n");
}
}
44
OUTPUT:
RESULT:
Thus the program for type checking is executed and verified.
45
Ex.No: 7
DATE:
CONSTRUCTION OF DAG
AIM:
ALGORITHM:
46
PROGRAM:
#include<stdio.h>
#include <conio.h>
struct node
char id[10];
char left[10];
char right[10];
char attach[3][10];
};
FILE *f;
int i,s=0;
char str[25],store[10][25];
void construct_tree()
int flag=0,f1=0;
temp=head;
if(s==5||s==6)
while (temp->next!=NULL)
if(!strcmp(store[2],temp->next->left))
47
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)
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]))
48
{
f1=1;
break;
t=t->next;
if(f1)
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)
49
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->attach[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()
clrscr();
f=fopen("C:\\DAG.txt","r");
50
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\t",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;
51
INPUT:
t1=4*i;
t2=a[t1];
t3=4*i;
t4=b[t3];
t5=t2*t4;
t6=prod+t5;
prod=t6;
t7=i+1;
i=t7;
OUTPUT:
ID LEFT RIGHT ATTACHED IDs
* 4 I t1 t3
[] a * t2
[] b * t4
* [] [] t5
+ prod * t6 prod
+ I t7 i
RESULT:
52
Ex.No: 8
DATE:
IMPLEMENTATION OF STACK
AIM:
ALGORITHM:
53
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack
{
int s[size];
int top;
} st;
int stfull()
{
if(st.top>=size-1)
return 1;
else
return 0;
}
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int pop()
{
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display()
{
int i;
if(stempty())
printf("\nStack Is Empty!");
else
54
{
for(i=st.top;i>=0;i--)
printf("\n%d",st.s[i]);
}
}
int main()
{
int item, choice;
char ans;
st.top = -1;
printf("\n\tImplementation Of Stack");
do
{
printf("\nMain Menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
printf("\nEnter Your Choice");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf("\nStack is Full!");
else
push(item);
break;
case 2:
if(stempty())
printf("\nEmpty stack!Underflow !!");
else
{
item = pop();
printf("\nThe popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf("\nDo You want To Continue?");
ans=getche();
}
while(ans=='Y'||ans =='y');
55
return 0;
}
OUTPUT:
RESULT:
56
Ex.No: 9
DATE:
IMPLEMENTATION OF BACKEND
AIM:
To write a ‘C’ program to generate the machine code for the given intermediate code.
ALGORITHM:
Step3: Display the assembly code according to the operators present in the given expression.
Step4: Use the temporary registers (R0, R1) while storing the values in assembly code programs.
57
PROGRAM:
/* CODE GENERATOR */
#include<stdio.h>
#include<string.h>
int count=0,i=0,l=0;
char str[100][100];
void gen();
void main()
{
clrscr();
printf("\n CODE GENERATOR \n");
printf("\n ENTER THREE ADDRESS CODE \n\n");
do
{
printf("\t");
gets(str[i]);
i++;
} while(strcmp(str[i-1],"QUIT"));
i=0;
printf("\n ASSEMBLY LANGUAGE CODE: \n");
while(strcmp(str[i-1],"QUIT"))
{
gen();
printf("\n");
i++;
}
void gen()
{
int j;
printf("\n");
for(j=strlen(str[i])-1;j>=0;j--)
{
char reg='R';
if(isdigit(str[i][j])||(isalpha(str[i][j]))|| str[i][j]=='+'||str[i][j]=='-'||str[i][j]=='*'||str[i][j]=='/'||str[i][j]=='
'||str[i][j]=='|'||str[i][j]=='&'||str[i][j]==':'||str[i][j]=='=')
{
switch(str[i][j])
{
case '+':
printf("\n\t MOV\t%c,%c%d",str[i][j-1],reg,count);
58
printf("\n\t ADD\t%c,%c%d",str[i][j+1],reg,count);
break;
case '-':
printf("\n\t MOV\t%c,%c%d",str[i][j-1],reg,count);
printf("\n\t SUB\t%c,%c%d",str[i][j+1],reg,count);
break;
case '*':
printf("\n\t MOV\t%c,%c%d",str[i][j-1],reg,count);
printf("\n\t MUL\t%c,%c%d",str[i][j+1],reg,count);
break;
case '/':
printf("\n\t MOV\t%c,%c%d",str[i][j-1],reg,count);
printf("\n\t DIV\t%c,%c%d",str[i][j+1],reg,count);
break;
case '|':
printf("\n\t MOV\t%c,%c%d",str[i][j-1],reg,count);
printf("\n\t OR\t%c,%c%d",str[i][j+1],reg,count);
break;
case '&':
printf("\n\t MOV\t%c,%c%d",str[i][j-1],reg,count);
printf("\n\t AND\t%c,%c%d",str[i][j+1],reg,count);
break;
case ':':
if(str[i][j+1]=='=')
{
printf("\n\t MOV\t%c%d,%c",reg,count,str[i][j-1]);
count++;
}
else
{
printf("\n syntax error...\n");
}
break;
default:
break;
}
}
else printf("\n Error\n");
}
}
59
OUTPUT:
CODE GENERATOR
A:=B+C
D:=E/F
QUIT
MOV B,R0
ADD C,R0
MOV R0,A
MOV E,R1
DIV F,R1
MOV R1,D
RESULT:
Thus the program for generation of Machine Code for the given intermediate code is executed and verified.
60
Ex.No:10
DATE:
AIM:
ALGORITHM:
61
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char a[25][25],u,op1='*',op2='+',op3='/',op4='-';
int p,q,r,l,o,ch,i=1,c,k,j,count=0;
FILE *fi,*fo;
// clrscr();
printf("Enter three address code");
printf("\nEnter the ctrl-z to complete:\n");
fi=fopen("infile.txt","w");
while((c=getchar())!=EOF)
fputc(c,fi);
fclose(fi);
printf("\n Unoptimized input block\n");
fi=fopen("infile.txt","r");
while((c=fgetc(fi))!=EOF)
{
k=1;
while(c!=';'&&c!=EOF)
{
a[i][k]=c;
printf("%c",a[i][k]);
k++;
c=fgetc(fi);
}
printf("\n");
i++;
}
count=i;
fclose(fi);
i=1;
printf("\n Optimized three address code");
while(i<count)
62
{
if(strcmp(a[i][4],op1)==0&&strcmp(a[i][5],op1)==0)
{
printf("\n type 1 reduction in strength");
if(strcmp(a[i][6],'2')==0)
{
for(j=1;j<=4;j++)
printf("%c",a[i][j]);
printf("%c",a[i][3]);
}
}
else if(isdigit(a[i][3])&&isdigit(a[i][5]))
{
printf("\n type2 constant floding");
p=a[i][3];
q=a[i][5];
if(strcmp(a[i][4],op1)==0)
r=p*q;
if(strcmp(a[i][4],op2)==0)
r=p+q;
if(strcmp(a[i][4],op3)==0)
r=p/q;
if(strcmp(a[i][4],op4)==0)
r=p-q;
for(j=1;j<=2;j++)
printf("%c",a[i][j]);
printf("%d",r);
printf("\n");
}
else if(strcmp(a[i][5],'0')==0||strcmp(a[i][5],'1')==0)
{
cprintf("\n type3 algebraic expression elimation");
if((strcmp(a[i][4],op1)==0&&strcmp(a[i][5],'1')==0)||(strcmp(a[i][4],op3)==0&&strcmp(a[i][5],'
1')==0))
{
for(j=1;j<=3;j++)
63
printf("%c",a[i][j]);
printf("\n");
}
else
printf("\n sorry cannot optimize\n");
}
else
{
printf("\n Error input");
}
i++;
}
getch();
}
64
infile.txt
OUTPUT
RESULT
Thus the C program for implementation of Code optimization was executed successfully.
65