/*#############################################################
Name : Abhijeet
Vijay Awade.
Roll No : 02
Aim
: Write a LEX program to remove comments from
c-program.
##############################################################*/
input:
%{
int m=0,s=0;
%}
%%
"//"[a-zA-z(""a-zA-Z
0-9)*;]* {s++;}
"/*"[a-zA-z(""a-zA-Z
0-9{})*;\n]*"*/" {m++;}
[a-zA-Z;0-9]+
{printf("%s",yytext);}
%%
main()
{
FILE *fp;
fp=fopen("pp.txt","r");
yyin=fp;
yylex();
printf("\n singled line
comment=%d",s);
printf("\n multiple line
comment=%d\n",m);
}
Input
text file:
#include<stdio.h>
void
main()
{
printf("\nWelcome...");
//printf("hi");
/*printf("\n\tabcd");*/
printf("\n\tBECOMP");
/*printf("\n\tNESGI");
printf("\n\tNES");
*/
//printf("\n\tvishal");
}
Output:
ubuntu@ubuntu:~$
lex cl4.l
ubuntu@ubuntu:~$
cc lex.yy.c -ll
ubuntu@ubuntu:~$
./a.out
#include<stdio.h>
void
main()
{
printf("\nWelcome...");
printf("\n\tBECOMP");
}
singled line comment=2
multiple line comment=2
/*##############################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim : Write a LEX program to count total no of words,lines,small &capital
letters, space & number
from given input. ################################################################*/
Lex
File:-
%{
int
num=0,small=0,nline=0,capital=0,word=0,space;
%}
%%
[a-z]
{small++;}
[A-Z]
{capital++;}
[a-zA-Z/
] {word++,space++;}
[a-zA-Z/\n]
{word++,nline++;}
[0-9/\n]+
{num++,nline++;}
[0-9/
]+ {num++,space++;}
%%
main()
{
yylex();
printf("\n\tSmall
letters:%d",small);
printf("\n\tCapital
letters:%d",capital);
printf("\n\twords :%d",word);
printf("\n\tLine :%d",nline);
printf("\n\tNumbers :%d",num);
printf("\n\tSpace :%d",space);
}
Output:
ubuntu@ubuntu-desktop:~$
cc lex.yy.c -ll
ubuntu@ubuntu-desktop:~$
./a.out
dsdd2343
dsfdf33
Small letters:9
Capital letters:0
words :1
Line :3
Numbers :2
Space :0ubuntu@ubuntu-desktop:~$ ^C
ubuntu@ubuntu-desktop:~$
/*#########################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim :
Write a LEX program to replace A-Z by a-z &
vice versa.
###########################################################*/
Lex
Flie:-
%{
%}
%%
[a-z]
{printf("%c",yytext[0]-32);}
[A-Z]
{printf("%c",yytext[0]+32);}
%%
main()
{
yylex();
}
Output:
ubuntu@ubuntu:~$
lex cl2.l
ubuntu@ubuntu:~$
cc lex.yy.c -ll
ubuntu@ubuntu:~$
./a.out
abcdPQrs
ABCDpqRS
/*#################################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim : To write a lex specification to identify different programming construct
of C program and print the same.
##################################################################*/
Lex
file:-
%{
#include<stdio.h>
FILE
*kf,*vf,*hf,*ff,*of;
int
cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0;
%}
%%
"int"|"float"|"void" {cnt1++;fprintf(kf,"%s \n",yytext);}
[a-zA-Z]+
{cnt2++;fprintf(vf,"%s
\n",yytext);}
"#include"[<"
[a-z]+".h"[>"]
{cnt3++;fprintf(hf,"%s \n",yytext);}
[a-zA-Z]+"("[^;]*")"
{cnt4++;fprintf(ff,"%s
\n",yytext);}
"+"|"=" {cnt5++;fprintf(of,"%s \n",yytext);}
";"|","|"{"|"}"|"\\"|"\*" {}
%%
main()
{
yyin=fopen("input","r");
kf=fopen("keyword","w+");
vf=fopen("variable","w+");
hf=fopen("header_file","w+");
ff=fopen("function","w+");
of=fopen("operator","w+");
yylex();
fprintf(kf,"
No.of Keywords:%d \n",cnt1);
fprintf(vf,"No.of
variables:%d \n",cnt2);
fprintf(hf,"No.of
header_files:%d \n",cnt3);
fprintf(ff,"No.of
functions:%d \n",cnt4);
fprintf(of,"No.of
operators:%d \n",cnt5);
}
Input
File
input.txt
// this is input file for program
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b;
float
z;
clrscr();
z=a+b;
printf("");
printf("");
}
Output:
ubuntu@ubuntu-desktop:~$
lex as2.l
ubuntu@ubuntu-desktop:~$
cc lex.yy.c -ll
ubuntu@ubuntu-desktop:~$
./a.out
Output
Files:
1)Function
file
main()
clrscr()
printf("")
printf("")
No.of
functions:4
2)header
file
#include<stdio.h>
#include<conio.h>
No.of
header_files:2
3)keyword
file
void
int
float
No.of Keywords:3
4)operator
=
+
No.of
operators:2
5)variable
file
a
b
z
z
a
b
No.of
variables:6
/*####################################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim : Writing a simple YACC programs for
desktop calculator.
####################################################################*/
Yacc
File:-
%{
#include<stdio.h>
%}
%start
S
%union
{
float a;
}
%token
num
%type
<a> S E T F num
%%
S
:E { printf("\n Result is
%f",$$);}
;
E
:E'+'T {$$=$1+$3;}
|E'-'T {$$=$1-$3;}
|T {$$=$1;}
;
T
:T'*'F {$$=$1*$3;}
|T'/'F {$$=$1/$3;}
|F {$$=$1;}
;
F
:'('E')' {$$=$2;}
|num {$$=$1;}
;
%%
main()
{
printf("\n
Enter expression to parse:");
yyparse();
}
int
yyerror(char *s)
{
printf("\n Error.....\n");
}
Lex
File:-
%{
#include"y.tab.h"
//extern
yylval;
%}
%%
[0-9]+[.]*[0-9]*
{ yylval.a=atof(yytext); return num;}
[-|+|*|/|(|)] {return yytext[0];}
[\n]
{}
%%
Output:-
uubuntu@ubuntu-desktop:~$
yacc -d calc1.y
uubuntu@ubuntu-desktop:~$
lex calc1.l
uubuntu@ubuntu-desktop:~$
cc lex.yy.c y.tab.c -ll
uubuntu@ubuntu-desktop:~$
./a.out
Enter expression to parse:10/5+3*4-5
Result is 9.000000student@ncc13:~$ ./a.out
Enter expression to parse:25/5+3*6-5
Result is 18.000000
/*###############################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim : Program
for valid variable declaration.
###############################################################*/
Yacc File:-
%{
//declaration
#include<stdio.h>
int count=0;
%}
%start D;
%token id INT FLOAT CHAR
%%
D : K V ';'
{printf("\n valid variable expression");}
;
K : INT
{count++;printf("\nK-> INT");}
| FLOAT {count++;printf("\nK->
flaot");}
| CHAR {count++;printf("\nK->
CHAR");}
;
V : V ',' V
{count++;printf("\nV -> V , V");}
| id {count++;printf("\nV ->
id");}
;
%%
main()
{
printf("\n Enter the
declaration to parse:");
yyparse();
}
void yyerror(char *s)
{
printf("\n Error....
%s",s);
}
Lex File:-
%{
//declaration
#include"y.tab.h"
%}
%%
int {return INT;}
float {return FLOAT;}
char {return CHAR;}
[a-zA-Z]+
{return id;}
[;] {return
';';}
[,] {return
',';}
[\n] { }
%%
Output:
uubuntu@ubuntu-desktop:~$ lex a33.l
uubuntu@ubuntu-desktop:~$ yacc -d a33.y
uubuntu@ubuntu-desktop:~$ cc lex.yy.c y.tab.c -ll
uubuntu@ubuntu-desktop:~$ ./a.out
Enter the
declaration to parse:int a,b;
K-> INT
V -> id
V -> id
V -> V , V
valid variable
expression
uubuntu@ubuntu-desktop:~$ ./a.out
Enter the
declaration to parse:long a,b;
error....syntax error
/*##########################################################################
Name : Abhijeet Vijay Awade.
Roll No : 02
Aim : A YACC program to identify a valid
expression for
Infix to postfix conversion.
##########################################################################*/
Yacc File:-
%{
#include<stdio.h>
%}
%start S
%union
{
char a[30];
}
%token num
%type <a> S E
num
%left '-','+'
%left '*','/'
%left '(',')'
%%
S :E'\n' { printf("\n The postfix expression is:\n");}
;
E :E'+'E {printf("+");}
|E'-'E
{printf("-");}
|E'*'E
{printf("*");}
|E'/'E
{printf("/");}
|'('E')' {;}
|num
{printf("%s",$1);}
;
%%
main()
{
printf("\n Enter infix
expression:");
yyparse();
}
int yyerror(char
*e)
{
printf("\n Error.....\n");
}
Lex File:-
%{
#include"y.tab.h"
%}
%%
[-|+|*|/|(|)] {return yytext[0];}
[\n] {return '\n';}
[a-zA-Z]+ {strcpy(yylval.a,yytext);return num;}
%%
Output:
ubuntu@ubuntu-desktop:~$
lex post.l
ubuntu@ubuntu-desktop:~$
yacc -d post.y
ubuntu@ubuntu-desktop:~$
cc lex.yy.c y.tab.c -ll
ubuntu@ubuntu-desktop:~$
./a.out
Enter infix
expression:a+b*c/d
The postfix
expression is:
abc*d/+
ubuntu@ubuntu-desktop:~$
./a.out
Enter infix expression:(a+b)*(c/d)
The postfix expression is:
ab+cd/*
/*##################################################################
Name : Abhijeet Vijay Awade.
Roll No : 02
Aim : A YACC program to identify a valid
expression
For infix to prefix conversion.
##################################################################*/
Yacc File:-
%{
#include<stdio.h>
#include<string.h>
%}
%start S
%union
{
char a[30];
}
%token num
%type <a> S E
num
%%
S :E'\n' {
printf("\n This is prefix expression\n"); printf("%s",$$);
}
;
E :E'+'E
{ strcpy($$,"+");
strcat($$,$1);
strcat($$,$3);}
|E'-'E
{ strcpy($$,"-");
strcat($$,$1);
strcat($$,$3);}
|E'*'E
{ strcpy($$,"*");
strcat($$,$1);
strcat($$,$3);}
|E'/'E
{ strcpy($$,"/");
strcat($$,$1);
strcat($$,$3);}
|'('E')' {
strcpy($$,"/");
strcat($$,$2); }
|num {
;}
;
%%
main()
{
printf("\n Enter infix
expression:");
yyparse();
}
int yyerror(char
*e)
{
printf("\n Error.....\n");
}
Lex File:-
%{
#include"y.tab.h"
%}
%%
[-|+|*|/|(|)] {return yytext[0];}
[\n] {return '\n';}
[a-zA-Z]+ {strcpy(yylval.a,yytext);return num;}
%%
Output:-
ubuntu@ubuntu-desktop:~$
lex pre.l
ubuntu@ubuntu-desktop:~$
yacc -d pre.y
ubuntu@ubuntu-desktop:~$
cc lex.yy.c y.tab.c -ll
ubuntu@ubuntu-desktop:~$
./a.out
Enter infix expression (a+b)*(c-d)
This is prefix
expression
*(+ab)(-cd)
/*###################################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim :
Write a program for intermediate code generation.
####################################################################*/
Lex
File:-
%{
#include"y.tab.h"
%}
%%
[a-zA-Z]+
{strcpy(yylval.string,yytext);return id;}
[0-9]+ {strcpy(yylval.string,yytext);return id;}
[-|*|+|=|/] {return yytext[0];}
[
\n]+ {;}
%%
Yacc
File:-
%{
#include<stdio.h>
#include<string.h>
int
iindex=0;
int
tindex=0;
struct
quad
{
char op[5];
char arg1[10];
char arg2[10];
char result[10];
}Q[20];
%}
%union
{
char string[10];
}
%token
<string> id digit
%type
<string> S E
%start
S
%left
'-','+'
%left
'*','/'
//%left
'(',')'
%%
S:id'='E
{strcpy(Q[iindex].op,"=");
strcpy(Q[iindex].arg1,$3);
strcpy(Q[iindex].arg2,"
");
strcpy(Q[iindex].result,$1);
strcpy($$,Q[iindex++].result);
}
;
E:E'+'E {addtoquad("+",$1,$3,$$);}
|E'*'E
{addtoquad("*",$1,$3,$$);}
|E'-'E
{addtoquad("-",$1,$3,$$);}
|E'/'E
{addtoquad("/",$1,$3,$$);}
|'-'E
{addtoquad("UMUS",$2,"",$$);}
|id
{strcpy($$,$1);}
|digit
{strcpy($$,$1);}
;
%%
main()
{
yyparse();
int
i;
for(i=0;i<iindex;i++)
{
printf("%d %s
%s %s %s ",i,Q[i].op,Q[i].arg1,Q[i].arg2,Q[i].result);
printf("\n");
}
}
yyerror(char
*s)
{
}
void
addtoquad(char op[10],char arg1[10],char arg2[10],char result[10])
{
strcpy(Q[iindex].op,op);
strcpy(Q[iindex].arg1,arg1);
strcpy(Q[iindex].arg2,arg2);
sprintf(Q[iindex].result,"t%d",tindex++);
strcpy(result,Q[iindex].result);
iindex++
}
Output:
ubuntu@ubuntu-desktop:~$
lex a4.l
ubuntu@ubuntu-desktop:~$
yacc -d a4.y
ubuntu@ubuntu-desktop:~$
cc lex.yy.c y.tab.c -ll
ubuntu@ubuntu-desktop:~$
./a.out
a=b+c*d/f
0 *
c d t0
1 /
t0 f t1
2 +
b t1 t2
3 =
t2 a
ubuntu@ubuntu-desktop:~$
/*###############################################################
Name : Abhijeet Vijay Awade.
Roll
No : 02
Aim : Write
a lex-yacc program for three address code generation.
#################################################################*/
Yacc
File:-
%{
#include<string.h>
#include<stdio.h>
#include<stdlib.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
{
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:
DECST ';'
| ASSIGNMENT ';'
| CONDST
| WHILEST
;
DECST:
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);
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++;
}
;
%%
main()
{
int
i;
printf("\n
Enter the Input C Program");
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");
}
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");
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);
}
yyerror()
{
}
Lex
File:-
%{
#include"y.tab.h"
#include<stdio.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];
%%
Output:
student@ncc5:~$
lex int.l
student@ncc5:~$
yacc -d int.y
student@ncc5:~$
cc lex.yy.c y.tab.c -ll
student@ncc5:~$
./a.out
Enter the Input C Program
main()
{
int a,b,c,d;
if(a<=b)
{
a=c+d;
}
else
{
b=c-d;
}
}
--------------------------------------------
Pos
Operator Arg1 Arg2
Result
---------------------------------------------
0 <= a b t0
1 == t0 FALSE 5
2 + c
d t1
3 = t1 a
4 GOTO 7
5 - c d t2
6 = t2 b
----------------------------------------------
/*##################################################################
Name : Abhijeet Vijay Awade.
Roll No : 02
Aim : To write a program to optimize entered three
address code.
##################################################################*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n,i,j,k,l,cnt,leader[15],index,block,blocks[15],set[10];
char
in[50][30],temp[10],temp2[10],temp3[10];
char*ptr,*ptr1,*ptr2;
FILE*fp;
clrscr();
fp=fopen("input.txt","r");
i=1;n=0;
while(!feof(fp))
{
fgets(in[i],25,fp);
ptr=strstr(in[i],"\n");
strcpy(ptr,NULL);
if(strlen(in[i])<1)
{
i--;
n--;
}
i++;
n++;
}
printf("\nEntered
Three Address Code:");
for(i=1;i<=n;i++)
printf("\n%s",in[i]);
getch();
clrscr();
for(i=1;i<=n;i++)
leader[i]=0;
leader[1]=1;
for(i=1;i<=n;i++)
{
if(strstr(in[i],"if")!=NULL ||
strstr(in[i],"goto")!=NULL)
{
leader[i+1]=1;
ptr=strstr(in[i],"goto");
ptr+=4;
index=atoi(ptr);
leader[index]=1;
}
}
leader[n+1]=1;
block=1;
i=1;
printf("\nThe basic blocks are as
follows:");
while(i<=n)
{
printf("\n\nBLOCK=%d",block);
do
{
printf("\n%s",in[i]);
blocks[i]=block;
i++;
}while(leader[i]!=1);
block++;
}
getch();
clrscr();
printf("\n\nAfter numeric
optimizasion");
i=1;
while(i<=n)
{
if(ptr=strstr(in[i],"="))
{
if(ptr=strstr(in[i],"*1"))
strcpy(ptr,NULL);
else
if(ptr=strstr(in[i],"/1"))
strcpy(ptr,NULL);
else
if(ptr=strstr(in[i],"+0"))
strcpy(ptr,NULL);
else
if(ptr=strstr(in[i],"-0"))
strcpy(ptr,NULL);
else
if(ptr=strstr(in[i],"*0"))
{
ptr=strstr(in[i],"=")+1;
strcpy(ptr,"0");
}
ptr=strstr(in[i],"=")+1;
for(j=0;in[i][j]!='=';j++)
temp[j]=in[i][j];
temp[j]='\0';
if(strcmp(ptr,temp)==0)
strcpy(in[i],"\0");
}
if(in[i][0]!='\0')
printf("\n%s",in[i]);
i++;
}
getch();
clrscr();
printf("\n\nAfter common expression
elimination:");
i=1;j=1;
while(i<block)
{
for(l=0;l<10;l++)
set[l]=0;
cnt=0;
while(blocks[j]==i)
{
for(l=0;l<cnt;l++)
if(j==set[l])
goto pr;
ptr=NULL;
if(in[j]!=NULL)
ptr=strstr(in[j],"=");
if(ptr!=NULL)
{
ptr+=1;
k=j+1;
while(blocks[k]==i)
{
for(l=0;l<cnt;l++)
if(k==set[l])
goto pr1;
ptr1=strstr(in[k],"=")+1;
if(ptr1!=NULL)
{
if(strcmp(ptr,ptr1)==0)
{
for(l=0;l<10;l++)
temp[l]=temp2[l]='\0';
for(l=0;in[j][l]!='=';l++)
temp[l]=in[j][l];
for(l=0;in[k][l]!='=';l++)
temp2[l]=in[k][l];
strcpy(in[k],NULL);
set[cnt]=j;
cnt++;
l=k+1;
while(blocks[l]==i)
{
if((ptr1=strstr(in[l],temp2))!=NULL)
{
ptr2=ptr1+strlen(temp2);
strcpy(temp3,ptr2);
strcpy(ptr1,temp);
strcat(ptr1,temp3);
set[cnt]=l;
cnt++;
}
l++;
}
}
}
pr1:k++;
}
}
pr:
if(strcmp(in[j],NULL)!=0)
printf("\n%s",in[j]);
j++;
}
i++;
}
getch();
}
Input Text:-
1 i=m-1
2 j=n
3 t1=4*n
4 v=a[t1]
5 i=i+1
6 t2=4*i
7 t3=a[t2]
8 if t3<v goto 5
9 j=j*1
10 t4=4*j
11 t5=a[t4]
12 if t5>v goto 9
13 if i>=j goto 23
14 t6=4*i
15 x=a[t6]
16 t7=4*I
17 t8=4*j
18 t9=a[t8]
19 a[t7]=9
20 t10=4*j
21 a[t10]=x
22 goto 5
23 t11=4*I
24 x=a[t11]
25 t12=4*i
26 t13=4*n
27 t14=a[t13]
28 a[t12]=t14
29 t15=4*n
30 a[t15]=x
Output:-
Entered Three Address Code:
i=m-1
j=n
t1=4*n
v=a[t1]
i=i+1
t2=4*i
t3=a[t2]
if t3<v goto 5
j=j*1
t4=4*j
t5=a[t4]
if t5>v goto 9
if i>=j goto 23
t6=4*i
x=a[t6]
t7=4*I
t8=4*j
t9=a[t8]
a[t7]=9
t10=4*j
a[t10]=x
goto 5
t11=4*I
x=a[t11]
t12=4*i
t13=4*n
t14=a[t13]
a[t12]=t14
t15=4*n
a[t15]=x
The basic blocks are as follows:
BLOCK=1
i=m-1
j=n
t1=4*n
v=a[t1]
BLOCK=2
i=i+1
t2=4*i
t3=a[t2]
if t3<v goto 5
BLOCK=3
j=j*1
t4=4*j
t5=a[t4]
if t5>v goto 9
BLOCK=4
if i>=j goto 23
BLOCK=5
t6=4*i
x=a[t6]
t7=4*I
t8=4*j
t9=a[t8]
a[t7]=9
t10=4*j
a[t10]=x
goto 5
BLOCK=6
t11=4*I
x=a[t11]
t12=4*i
t13=4*n
t14=a[t13]
a[t12]=t14
t15=4*n
a[t15]=x
After numeric optimization:
i=m-1
j=n
t1=4*n
v=a[t1]
i=i+1
t2=4*i
t3=a[t2]
if t3<v goto 5
t4=4*j
t5=a[t4]
if t5>v goto 9
if i>=j goto 23
t6=4*i
x=a[t6]
t7=4*I
t8=4*j
t9=a[t8]
a[t7]=9
t10=4*j
a[t10]=x
goto 5
t11=4*I
x=a[t11]
t12=4*i
t13=4*n
t14=a[t13]
a[t12]=t14
t15=4*n
a[t15]=x
After common expression elimination:
i=m-1
j=n
t1=4*n
v=a[t1]
i=i+1
t2=4*i
t3=a[t2]
if t3<v goto 5
t4=4*j
t5=a[t4]
if t5>v goto 9
if i>=j goto 23
t6=4*i
x=a[t6]
t7=4*I
t8=4*j
t9=a[t8]
a[t7]=9
a[t8]=x
goto 5
t11=4*I
x=a[t11]
t12=4*i
t13=4*n
t14=a[t13]
a[t12]=t14
a[t13]=x
No comments:
Post a Comment