0% found this document useful (0 votes)
28 views

Compiler-Design Correct Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Compiler-Design Correct Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

INDEX

Ex.No Date Name of Experiments Page. No Signature

3a

3b

3c

3d

7
Ex.No:1 Using theLEXtool,Developalexicalanalyzertorecognizeafew
patterns in C. (Ex. identifiers, constants, comments, operators
etc.). Create a symbol table, while recognizing identifiers.

AIM:
To develop a lexical analyzer to identify identifiers, constants comments ,operator set c using
Symbol Table.

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
symbolisalreadypresent,itdisplays“DuplicateSymbol”.Else,insertthesymbolandthe
corresponding address in the symbol table.

5. If the entered choiceis 2,the symbols present in the symbol table are displayed.

6. Iftheenteredchoiceis3,the symbol to be deleted is searched in the symbol table.

7. If it is not found in the symbol table it displays “Label Not found”. Else, the symbol is
deleted.

8. 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.
PROGRAM:
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
voidkeyword(charstr[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcmp("fl
oat",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("printf",str)==0||strcmp("switch
",str)==0||strcmp("case",str)==0)
printf("\n%sisakeyword",str);
else
printf("\n%sisanidentifier",str);
}
void main()
{
FILE *f1,*f2,*f3;
charc,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
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);
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("\ntheno'sintheprogramare:"); for(j=0;j<i;j+
+)
printf("\t%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("thekeywordsandidentifierare:");
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("\
nSpecialCharactersare");
while((c=getc(f3))!=EOF) printf("\t
%c",c);
printf("\n");
fclose(f3);
printf("Totalnooflinesare:%d",lineno);
}
OUTPUT:

RESULT:
Thus the program for developing a lexical analyzer to recognize a few patterns in C has been
executed successfully.
Ex.No:2
Implement a Lexical Analyzer using LEX Tool.

AIM:

To write a program for implementing a Lexical analyzer using LEX tool in Linux platform.

ALGORITHM:
1. Start the program

2. Lex program consists of three parts.

3. Declaration%%

4. Translation rules%%

5. Auxiliary procedure.

6. The declaration section includes declaration no f variables, main test, constants and regular

7. Definitions.

8. Translation rule of lex program a restatements of the form

9. P1{action}

10. P2{action}

11. Pn{action}

12. Write program in the vieditor andsaveitwith.1extension.

13. Compile the lex program with lex compiler to produce out put file as lex.yy.c.

14. Eg.$lex filename.1

15. $gcclex.yy.c-11

Compile that file with C compiler and verify the output.


PROGRAM:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>charvars[100][100];
int vcnt; char input[1000],c;
chartoken[50],tlen;

int state=0,pos=0,i=0,id;
char*getAddress(charstr[])
{

for(i=0;i<vcnt;i++)
if(strcmp(str,vars[i])==0)
return vars[i];
strcpy(vars[vcnt],str);
return vars[vcnt++];
}

intisrelop(charc)

if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^')return1;elsereturn0;

intmain(void)

clrscr();

printf("EntertheInputString:");gets(input); do
{c=input[pos];putchar(c);switch(state)

{case0:if(isspace(c))printf("\b");if(isalpha(c))

{token[0]=c;tlen=1;state=1;

}if(isdigit(c))state=2;if(isrelop(c))state=3;if(c==';')printf("\t<3,3>\n"); if(c=='=')
printf("\t<4,4>\n");
break;case1:if(!isalnum(c))

{token[tlen]='\o';printf("\b\t<1,%p>\n",getAddress(token));state=0;pos--;

}elsetoken[tlen++]=c;break;case2:

if(!isdigit(c))
{printf("\b\t<2,%p>\n",&input[pos]);state=0;pos--;
}break;case3:

id=input[pos-1];if(c=='=')printf("\t<%d,%d>\n",id*10,id*10);
else{ printf("\b\t<%d,%d>\n",id,id); pos--;
}

state=0
; break;
}

pos++;

}while(c!=0);getch();return 0;

}
OUTPUT:

RESULT:
Thus the program for the exercise on lexical analysis using lex has been successfully
executed and output is verified.
EX:NO:3A Generate YACC specification for a few syntactic categories
.Program to recognize a valid arithmetic expression that uses
operator +, -, * and /.

AIM:
To write a c program to do exercise on syntax analysis using YACC.

ALGORITHM:
1. Start the program.

2. Write the code for parser. L in the declaration port.

3. Write the code for the ‘y’ parser.

4. Also write the code for different arithmetical operations.

5. Write additional code to print the result of computation.

6. Execute and verify it.

7. Stop the program


PROGRAM:

#include<stdio.h>
#include<conio.h>
Void main()
{
char s[5];
clrscr();
printf("\nEnter anyoperator:");
gets(s);
switch(s[0])
{
case'>':

if(s[1]=='=')

printf("\n Greaterthan or equal");


else

printf("\nGreaterthan");
break;

case'<':if(s[1]=='=')

printf("\nLess thanor equal");


else
printf("\nLessthan");break;

case'=':if(s[1]=='=')printf("\nEqualto");elseprintf("\nAssignment");break; case'!':
if(s[1]=='=') printf("\nNot Equal"); else
printf("\nBitNot");break;

case'&':if(s[1]=='&')printf("\nLogicalAND");elseprintf("\nBitwiseAND");
break;
case'|':if(s[1]=='|')printf("\nLogicalOR");else
printf("\nBitwise OR"); break;
case'+': printf("\n Addition"); break;
case'-': printf("\nSubstraction"); break;
case'*': printf("\nMultiplication"); break;
case'/': printf("\nDivision"); break;
case'%': printf("Modulus"); break;
default: printf("\n Not a operator");}
getch(); }
OUTPUT:

RESULT:
Thus the program for the exercise on the syntax using YACC has been executed successfully
and Output is verified.
Ex.No:3b Program to recognize a valid variable which starts with a letter
Followed by any number of letters or digits.

AIM:
To write a yacc program to check valid variable followed by letter or digits

ALGORITHM:
1. Declare the variables which are used for C programs in the declaration section%{...%}.

2. Define the tokens let,digused in yacc.

3. Include the pattern (Context Free Grammar)in the transition rule section for validating
the variable between %%..%%

4. In main function get the variable from the user for validating it.

5. Call the yyparse()function to parse the given expression and it construct the LALR parsing
table using the grammar defined in transition rule .

6. Then call the yy lex() function, it get the current to kenands to reits value in yyl val variable
and it is repeated until the value for given variable.

7. Then it validates the variable with constructed LALR parser.

8. Print the variable is “accepted” if the variable given by user is derived by the grammar, else
print “rejected”.

9. Stop the program.


PROGRAM:
%{
#include<stdio.h>#include<ctype.h>
%}
%tokenletdig
%%

sad: letrecld '\n'{printf("accepted\n");return0;}


|let'\n'{printf("accepted\n");return0;}
|
|error{yyerror("rejected\n");return0;}
;
recld:let recld
|digrecld

|let
|dig
;
%%
yylex()
{
charch;while((ch=getchar())=='');if(isalpha(ch))
return let; if(isdigit(ch)) return dig;
returnch;
}
yyerror(char*s)
{

printf("%s",s);
}
main(){
printf("ENTERAvariable:");yyparse();}
OUTPUT:
C:\FlexWindows\EditPlusPortable>yacc-dvalid C:\

Flex Windows\EditPlusPortable> cc y.tab.c C:\

Flex Windows\EditPlusPortable> a

A1

Accepte

d 10a

Rejected

RESULT:
Thus the program for checking letter followed by letter or digits were done and executed
successfully.
Ex.No:3c Program to recognize a valid control structures syntax of C
language (For loop,while loop,if-else,if-else-if,switch-case,etc.).

AIM:
To create a calculate or using Lex and Yacc programs.

ALGORITHM:
Step1:Start the program.

Step2:Include the necessary header files.

Step3:Use a function for printing the error message.

Step4:Get the input from the user and parse it.

Step5:Check the input is a valid expression or not.

Step6:Write separate operations for addition,subtraction, multiplication and division using the expr
and matching it with the operators in the in the input.

Step7:Print the error messages for the invalid operators.

Step8:Print the output of the expression.

Step 9: Terminate the program.


PROGRAM:
4d.

%{

#include<stdlib.h>

#include<stdio.h>

#include"y.tab.h"voidyyerror(char*);externint yylval;
%}
%%
[\t]+;
[0-9]+{yylval=atoi(yytext);returnINTEGER;} [-
+*/] {return *yytext;}"("
{return*yytext;}")"{return*yytext;}

\n{return *yytext;}
. {char msg[25];
sprintf(msg,"%s<%s>","invalidcharacter",yytext);yyerror(msg);
}

4d.y
%{
#include<stdlib.h>#include<stdio.h>intyylex(void);#include"y.tab.h"
%}
%token INTEGER
%%

expr:
expr'\n'{printf("%d\n",$1);}
|
'n'ex
pr:
expr'+'mulex{ $$=$1+$3;}
| expr'-' mulex{ $$= $1-$3;}
| mulex { $$ = $1; } mulex:
mulex'*'term{$$=$1*$3;}
|mulex'/'term{$$=$1/$3;}

|term{$$=$1;} term:
'('expr')'{$$= $2;}
| INTEGER{$$=$1;}

%%
voidyyerror(char *s)
{
fprintf(stderr,"%s\n",s);return;
}
yywrap()
{
return(1);
}
intmain(void)

{
/*yydebug=1;*/ yyparse();return0;
}
OUTPUT:
[root@sys704d]# lex4d.l[root@sys704d]# yacc -d4d.y[root@sys704d]#cc y.tab.clex.yy.c - ll[root@sys70
4d]# ./a.out 10+20-50
-2012*10

120
1/1
1
100+100
200
10-10

RESULT:
Thus a calculator is for me using Yacc and the expression are checked and the output of the
valid expressions are obtained and verified successfully.
Ex.No:3d
Implementation of calculator using LEX and YACC

AIM:
To write a program for implementing a calculator for computing the given expression using
semantic rules of the YACC tool and LEX.

ALGORITHM:

Step1:AYaccs our c program has three parts as follows:


Declarations%%translation rules%% supporting C routines
Step2:Declarations Section:This section contain sentries that:
i. Include standard I/O header file.

ii. Define global variables.


iii. Define the list rule as the place to start processing.
iv. Define the token sused by the parser.v. Define the operator sand their precedence.
Step3:Rules Section:The rules section defines the rules that parse the input stream. Each rule of a
grammar production and the associated semantic action.
Step4:Programs Section:The programs section contains the following subroutines.Because these
subroutines are included in this file, it is not necessary to use the yacc library when processing this
file.
Step5:Main- The required main program that calls they parse subroutine to start the program.
Step6: yyerror(s)-This error-handling subroutine only prints a syntax error message.
Step 7:yywrap-The wrap-up subroutine that returns a value of 1when the end of input occurs.The
calc.lex file contains include statements for standard input and output, as programmar file
information if we use the-dflag with the yacc command.The y.tab.hfile contains definitions for the
tokens that the parser program uses.
Step8:calc.lex contains the rules to generate the tokens from the input stream.
PROGRAM:
%{
#include<stdio.h>
#include"y.tab.h"
extern int yylval;
%}
%%

[0-9]+{
yylval=atoi(yytext);
return NUMBER;
}
[\t];
[\n]return0;

.returnyytext[0];
%%
int yywrap()
{
return1;
}

YACCPART:
%{
#include<stdio.h>i
nt flag=0;
%}
%token NUMBER

%left '+''-'
%left'*''/''%'
%left'('')'
%%
ArithmeticExpression: E{
printf("\nResult=%d\n",$$);
return 0;
};

E:E'+'E{$$=$1+$3;}
|E'-'E{$$=$1-$3;}
|E'*'E{$$=$1*$3;}
|E'/'E{$$=$1/$3;}
|E'%'E{$$=$1%$3;}
|'('E')'{$$=$2;}

|NUMBER{$$=$1;}
%%
void main()
{
printf("\nEnterAnyArithmeticExpressionwhichcanhaveoperationsAddition,Subtraction,
Multiplication, Divison, Modulus and Round brackets:\n");
yyparse();
if(flag==0
)
printf("\nEnteredarithmeticexpressionisValid\n\n");
}
void yyerror()

{
printf("\nEnteredarithmeticexpressionisInvalid\n\n"); flag=1;
}
OUTPUT:

RESULT:

Thus the program to implement a calculator for computing the given expression using
semantic rules of the YACC tool and LEX was executed successfully.
Ex.No:4 Generate three address code for a simple program using LEX and
YACC

AIM:
To convert the given input of BNF rules intoYacc for to generate a Abstract Syntax
Tree.

ALGORITHM:
Step1:Start the program.

Step2:Include the necessary header files.

Step3:Separate the identifier and the numbers.

Step4:Use conditions to print the keywords using the lex file.

Step5:Create a structure and use the variables separately for operands, arguments and results.

Step6: Use a stack top push and pop all the contents of the input file to the output screen.

Step7:Use the file functions to read the input from a file.

Step8:Use string functions to operate on the input.

Step9:Terminate the program.


PROGRAM:
ex5.l
%{
#include"y.tab.h"#include<stdio.h>#include<string.h>intLineNo=1;
%}
identifier[a-zA-Z][_a-zA-Z0-9]*number[0-9]+|([0-9]*\.[0-9]+)
%%
main\(\)returnMAIN;ifreturnIF;
elsereturnELSE;whilereturnWHILE;int|char|floatreturnTYPE;
{identifier}{strcpy(yylval.var,yytext);returnVAR;}
{number}{strcpy(yylval.var,yytext);returnNUM;}
\<|\>|\>=|\<=|=={strcpy(yylval.var,yytext);returnRELOP;} [ \t] ;
\nLineNo++;
.returnyytext[0];
%%
ex5.y
%{
#include<string.h>
#include<stdio.h>structquad
{
charop[5];chararg1[10];chararg2[10];charresult[10];
}QUAD[30];
structstack
{
intitems[100];inttop;
}stk;
intIndex=0,tIndex=0,StNo,Ind,tInd;externintLineNo;
%}
%union
{
charvar[10];
}
%token<var>NUMVAR RELOP

%tokenMAINIFELSEWHILETYPE
%type<var>EXPRASSIGNMENTCONDITIONIFSTELSESTWHILELOOP
%left'-''+'
%left'*''/'
%%
PROGRAM:MAINBLOCK
;
BLOCK:'{'CODE'}'
;
CODE:BLOCK
|STATEMENTCODE
|STATEMENT
;
STATEMENT:DESCT';'
|ASSIGNMENT';'
|CONDST
|WHILEST
;
DESCT:TYPEVARLIST
;
VARLIST:VAR','VARLIST
|VAR
;

ASSIGNMENT:VAR'='EXPR{ strcpy(QUAD[Index].op,"=");strcpy(QUAD[Index].arg1,$3);strcp
y(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);
}
|IFSTELSEST
;
IFST:IF'('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++;
};
ELSEST:ELSE{
tInd=pop(); Ind=pop(); push(tInd); sprintf(QUAD[Ind].result,"%d",Index);
}
BLOCK
{
Ind=pop();sprintf(QUAD[Ind].result,"%d",Index);
};
CONDITION:VARRELOPVAR{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++;
}
;
%%
externFILE *yyin;
intmain(intargc,char*argv[])
{
FILE*fp;inti;if(argc>1)
{
fp=fopen(argv[1],"r");if(!fp)
{
printf("\nFile notfound");exit(0);
}
yyin=fp;
}
yyparse();
printf("\n\n\t\t----------------------------""\n\t\tPosOperatorArg1Arg2Result""\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");
return0;
}
voidpush(intdata)
{
stk.top++; if(stk.top==100)
{
printf("\nStackoverflow\n");exit(0);
}
stk.items[stk.top]=data;
}
intpop()
{
int data; if(stk.top==-1)
{
printf("\nStackunderflow\n");exit(0);
}

data=stk.items[stk.top--];returndata;
}
voidAddQuadruple(charop[5],chararg1[10],chararg2[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);
}
yyerror()
{
printf("\nErroronlineno:%d",LineNo);
}
input.c
main()
{
inta,b,c; if(a<b)
{
a=a+b;
}
while(a<b)
{
a=a+b;
}
if(a<=b)
{
c=a-b;
}
else
{
c=a+b;
}
}
OUTPUT:

[root@sys705]# lexex5.l[root@sys705]#yacc -dex5.y

[root@sys70 5]# cc y.tab.c lex.yy.c –ll

[root@sys705]#./a.outinput.c

RESULT:
Thus the three address code for a simple program is created and the output is verified
successfully.
Ex.No:5
Implement type checking using Lex and Yacc.
AIM:
To write a C program for implementing type checking for given expression.

ALGORITHM:
1. Start a program.

2. Include all the header files.

3. Initialize all the functions and variables.

4. Get the expression from the user and separate into the tokens.

5. After separation,specify the identifiers,operators and number.

6. Print the output.

7. Stop the program.

PROGRAM:

#include<stdio.h>charstr[50],opstr[75];
intf[2][9]={2,3,4,4,4,0,6,6,0,1,1,3,3,5,5,0,5,0};
intcol,col1,col2;charc; swt()
{
switch(c)
{
case'+':col=0;break;case'-':col=1;break;case'*':col=2;break;case'/':col=3;break;case'^':col=4;break;
case'(':col=5;break; case')':col=6;break; case'd':col=7;break; case'$':col=8;break;
default:printf("\nTERMINALMISSMATCH\n");exit(1);
}// return0;
}
main()
{
inti=0,j=0,col1,cn,k=0;
int t1=0,foundg=0;chartemp[20];clrscr();printf("\nEnterarithmeticexpression:");scanf("%s",&str);
while(str[i]!='\0')
i++;str[i]='$';str[++i]='\0';printf("%s\n",str);come:i=0;opstr[0]='$';j=1; c='$'; swt(); col1=col; c=str[i];
swt(); col2=col;
if(f[1][col1]>f[2][col2])
{
opstr[j]='>';j++;
}
else if(f[1][col1]<f[2][col2])
{
}
else
{
}
opstr[j]='<';j++;
opstr[j]='=';j++;
while(str[i]!='$')
{
c=str[i];
swt();col1=col;c=str[++i];swt();col2=col;opstr[j]=str[--i];j++;if(f[0][col1]>f[1][col2])
{
opstr[j]='>';
j++;
}
else if(f[0][col1]<f[1][col2])
{
opstr[j]='<';j++;
}
else
{
opstr[j]='=';j++;
}i++;
}
opstr[j]='$';
opstr[++j]='\0';
printf("\nPrecedenceInput:%s\n",opstr);i=0;j=0;while(opstr[i]!='\0')
{
foundg=0;while(foundg!=1)
{
if(opstr[i]=='\0')gotoredone;if(opstr[i]=='>')foundg=1;t1=i;i++;
}
if(foundg==1)
for(i=t1;i>0;i--)if(opstr[i]=='<')break;if(i==0){printf("\nERROR\n");exit(1);}cn=i;j=0;i=t1+1;
while(opstr[i]!='\0')
{
temp[j]=opstr[i];j++;i++;
}
temp[j]='\0';opstr[cn]='E';opstr[++cn]='\0';strcat(opstr,temp);printf("\n%s",opstr);i=1;
}
redone:k=0;while(opstr[k]!='\0')
{k++;
if(opstr[k]=='<')
{
Printf("\nError");exit(1);
}
}
if((opstr[0]=='$')&&(opstr[2]=='$'))gotosue;i=1while(opstr[i]!='\0')
{
c=opstr[i];if(c=='+'||c=='*'||c=='/'||c=='$')
{temp[j]=c;j++;}i++;
}
temp[j]='\0';strcpy(str,temp);gotocome;sue: printf("\n success"); return 0;
}
OUTPUT:

RESULT:
Thus the program has been executed successfully and Output is verified.
Ex.No:6 Implement simple code optimization techniques (Constant
folding,Strength reduction and Algebraic transformation)

AIM:
To write a C program to implement Code Optimization Techniques.

ALGORITHM:
Step1:Start the program.
Step2:Include the necessary header files.
Step3:Declare necessary character arrays for input and output and also a structure to include it.
Step4:Get the Input: Setof‘L’ values with corresponding‘R’values and
Step5:Implement the principles our of optimization techniques.
Step5:The Output should be of Intermediate code and Optimized code after eliminating common
expressions.
Step6:Terminate the program.

PROGRAM:
#include<stdio.h> #include<conio.h> #include<string.h>struct op
{
charl;charr[20];
}op[10],pr[10];
void main()
{
inta,i,k,j,n,z=0,m,q;char*p,*l; chartemp,t;char*tem;clrscr();
printf("EntertheNumberofValues:");scanf("%d",&n); for(i=0;i<n;i++)
{
printf("left:");
op[i].l=getche();
printf("\tright:");scanf("%s",op[i].r);
}
printf("IntermediateCode\n");for(i=0;i<n;i++)
{
printf("%c=",op[i].l);
printf("%s\n",op[i].r);
}
for(i=0;i<n-1;i++)
{
temp=op[i].l; for(j=0;j<n;j++)
{
p=strchr(op[j].r,temp);if(p)
{
pr[z].l=op[i].l;strcpy(pr[z].r,op[i].r);z++;
}
}
}
pr[z].l=op[n-1].l; strcpy(pr[z].r,op[n-1].r);z++;printf("\nAfterDeadCodeElimination\n");for(k=0;k<z;k++)
{
printf("%c\t=",pr[k].l);
printf("%s\n",pr[k].r);
}
for(m=0;m<z;m++)
{
tem=pr[m].r;for(j=m+1;j<z;j++)
{
p=strstr(tem,pr[j].r);if(p)
{
t=pr[j].l;pr[j].l=pr[m].l;for(i=0;i<z;i++)
{
l=strchr(pr[i].r,t);if(l)
{
a=l-pr[i].r;
printf("pos:%d",a);
pr[i].r[a]=pr[m].l;
}
}
}
}
}
printf("EliminateCommonExpression\n");for(i=0;i<z;i++)
{
printf("%c\t=",pr[i].l);
printf("%s\n",pr[i].r);
}
for(i=0;i<z;i++)

{
for(j=i+1;j<z;j++)
{
q=strcmp(pr[i].r,pr[j].r);
if((pr[i].l==pr[j].l)&&!q)
{
pr[i].l='\0' strcpy(pr[i].r,'\0');
}
}
}
printf("OptimizedCode\n");for(i=0;i<z;i++)
{
if(pr[i].l!='\0')
{
printf("%c=",pr[i].l);
printf("%s\n",pr[i].r);
}
}
getch();}
OUTPUT:
EntertheNumberofValues:5

Left: a right: 9

Left: b right: c+dLeft:

e right: c+dLeft: f

right: b+e Left:r

right: f

IntermediateCodea=9

b=c+d e=c+d

f=b+er=:fAfterDeadCodeEli

mination b=c+d

e =c+d

f=b+er=:f

EliminateCommonExpression

b=c+d

b =c+d

f=b+br=:f

OptimizedCode

b=c+df=b+br=:f

RESULT:
Thus the required C program for implementation of code optimization techniques is done
and the required output is obtained and verified.
Ex.No:7 Implement back-end of the compiler for which the three address
code is given as input and the 8086 assembly language code is
produced as output.

AIM:
Toimplementthebackendofacompilerwhichtakesthethreeaddresscodeand produces
the 8086 assembly language instructions using a C program.

ALGORITHM:
Step1:Start theprogram.

Step2:Includethenecessaryheaderfiles.

Step3:Declare necessarycharacterarraysforinputandoutputandalso astructureto include it. Step 4:


Get the Intermediate Code as the input.

Step5:Displaytheoptionstodothevariousoperationsanduseaswitchcasetoimplementthat operation.

Step6:Terminate the program.

PROGRAM:
#include<stdio.h>

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

charicode[10][30],str[20],opr[10];inti=0; clrscr();

printf("\nEnterthesetofintermediatecode(terminatedbyexit):\n");do
{

scanf("%s",icode[i]);

}while(strcmp(icode[i++],"exit")!=0);printf("\ntargetcodegeneration");

printf("\n************************");i=0;

do

strcpy(str,icode[i]);switch(str[3])

case'+':strcpy(opr,"ADD");break;case'-':strcpy(opr,"SUB");break; case

'*': strcpy(opr,"MUL"); break;

case'/':strcpy(opr,"DIV");

break;

printf("\n\tMov%c,R%d",str[2],i);

printf("\n\t%s%c,R%d",opr,str[4],i);

printf("\n\tMovR%d,%c",i,str[0]);

}while(strcmp(icode[++i],"exit")!=0);getch();

}
OUTPUT:

Enterthesetofintermediatecode(terminated byexit):

d=2/3c=4/5 a=2*e exit

target codegeneration

******************************************************************

MOV 2,R0

DIV 3,R0

MOV R0,d

MOV 4,R1

DIV5,R1

MOV R1.c

MOV2,,R2

MULe,R2

MOV R2,a

RESULT:
ThustherequiredCprogramtoimplementthebackendofthecompiler isdoneandthe required
output is obtained and verified.

You might also like