Sunteți pe pagina 1din 56

Page

S.NO DATE EXPERIMENT NAME Sign


No

1 Implementation of Symbol Table

2 Develop a lexical analyser to recognize a


few patterns in C
3 Lexical analyser using LEX tool

4 Recognizing a valid arithmetic expression


using LEX and YACC

5 Recognizing a valid identifier using LEX


and YACC

6 Implementation of calculator using LEX


and YACC

7 Convert the BNF rules into YACC form


and write code to generate abstract
syntax tree

8 Implementation of type checking

9 Implementation of static storage


allocation strategy

10 Implementation of assembly code


generation

11 DAG construction

12 Implementation of Simple Code


Optimization Techniques
Ex.No: 1
INDEX
Date: Implementation of Symbol Table

1
AIM:

ALGORITHM:

Program:
#include<stdio.h>

2
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
void insert(int m,int k);
void Display();
void Delete();
int Search(char lab[]);
void Modify();
struct SymbTab
{
char symbol[10];
int addr;
struct SymbTab *next;
};
struct SymbTab *first,*last;
char var[5][10];
int size=0;
void main()
{
FILE *fp;
int op,y,k,r,t=0,x;
char ch,la[10];
char *key[]={"char","float","int","double"};
char token[50];
fp=fopen("add.c","r");
if(fp==NULL)
{
printf("file not found");
}
exit(0);
while(!feof(fp))

3
{
fscanf(fp,"%s",token);
for(x=0;x<=3;x++)
{
if(strcmp(token,key[x])==0)
{
ch=fgetc(fp);
k=0;
r=0;
do
{
ch=fgetc(fp);
if(ch!=','&& ch!=';')
var[k][r++]=ch;
else
{
var[k][r]='\0';
k++;
r=0;
}
}
while(ch!=';');
break;
}
}
switch(x)
{
case 0:
insert(1,k);
break;
case 1:
insert(4,k);
break;

4
case 2:
insert(2,k);
break;
case 3:
insert(8,k);
break;
}
}
fclose(fp);
clrscr();
do
{
printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");

printf("\n\t1.DISPLAY\n\t2.DELETE\n\t3.SEARCH\n\t4.MODIFY\n\t5.END\n");
printf("\n\tEnter your option : ");
scanf("%d",&op);
switch(op)
{
case 1:
Display();
break;
case 2:
Delete();
break;
case 3:
printf("\n\tEnter the label to be searched : ");
scanf("%s",la);
y=Search(la);
printf("\n\tSearch Result:");
if(y==1)
printf("\n\tThe label is present in the symbol table\n");
else
printf("\n\tThe label is not present in the symbol table\n");

5
break;
case 4:
Modify();
break;
case 5:
exit(0);
}
}while(op<5);
getch();
}
void insert(int m,int k)
{
int n,i;
char l[10];
for(i=0;i<k;i++)
{
n=Search(var[i]);
if(n==1)
printf("\n\tThe label exists already in the symbol table\n\tDuplicate can't be
inserted");
else
{
struct SymbTab *p;
p=malloc(sizeof(struct SymbTab));
strcpy(p->symbol,var[i]);
p->next=NULL;
if(size==0)
{
first=p;
last=p;
last->addr=1000;
}
else
{

6
p->addr=last->addr+m;
last->next=p;
last=p;
}
}
size=size+1;
}
printf("\n\tsymbol inserted\n");
}
void Display()
{
int i;
struct SymbTab *p;
p=first;
printf("\n\tSYMBOL\t\tADDRESS\n");
for(i=0;i<size;i++)
{
printf("\t%s\t\t%d\n",p->symbol,p->addr);
p=p->next;
}
}
int Search(char sym[])
{
int i,flag=0;
struct SymbTab *p;
p=first;
for(i=0;i<size;i++)
{
if(strcmp(p->symbol,sym)==0)
flag=1;

p=p->next;
}

7
return flag;
}
void Modify()
{
char l[10],nl[10];
int i,s;
struct SymbTab *p;
p=first;
printf("\n\tEnter the old symbol : ");
scanf("%s",l);
s=Search(l);
if(s==0)
printf("\n\tsymbol not found\n");
else
{
printf("\n\tEnter the new symbol : ");
scanf("%s",nl);
for(i=0;i<size;i++)
{
if(strcmp(p->symbol,l)==0)
strcpy(p->symbol,nl);
p=p->next;
}
printf("\n\tAfter Modification:\n");
Display();
}
}
void Delete()
{
int a,v;
char l[10];
struct SymbTab *p,*q;
p=first;

8
printf("\n\tEnter the symbol to be deleted : ");
scanf("%s",l);
a=Search(l);
if(a==0)
printf("\n\tsymbol not found\n");
else
{
if(strcmp(first->symbol,l)==0)
{
first=first->next;
free(p);
}
else {
q=p->next;
while(q!=0)
{
if(strcmp(q->symbol,l)==0)
{
v=p->addr;
p->next=q->next;
if(q!=last)
q->next->addr=q->next->addr-q->addr+v;
free(q);
}
p=q;
q=q->next;
}
}
size--;
printf("\n\tAfter Deletion:\n");
Display();
} }

Input File

9
add.c
void main()
{
int a,b,c;
}

OUTPUT:

RESULT:

Ex.No: 2

Date: Develop a lexical analyzer to recognize a few patterns in C. 

10
AIM:

ALGORITHM:

Program:

11
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int search(char a[20][20],char bm[],int c);
void main()
{
FILE *fp,*f;
char ch,token[20],key[20][20],id[20][20],op[20][20],nemonic[20],te[2];
int t=0,x=0,k=0,i=0,o=0,kee=0,n,h;
clrscr();
for(h=0;h<20;h++)
{
strcpy(key[h]," ");
strcpy(op[h]," ");
strcpy(id[h]," ");
}
fp=fopen("add.c","r");
f=fopen("keyword.c","r");
if(fp==NULL)
{
printf("file not found");
exit(0);
}
while(!feof(fp))
{
kee=0;
ch=getc(fp);
if(isalpha(ch))
token[t++]=ch;
else

12
{
if(ispunct(ch))
{
te[0]=ch;
te[1]='\0';
n=search(op,te,o);
if(n==0)
strcpy(op[o++],te);
}
token[t]='\0';
t=0;
rewind(f);
while(fscanf(f,"%s",nemonic)!=EOF)
{
if(strcmp(token,nemonic)==0)
{
kee=1;
break;
}
}
if(kee==1)
{
n=search(key,token,k);
if(n==0)
strcpy(key[k],token);
k++;
}
else if(strcmp(token,'\0')!=0)
{
n=search(id,token,i);
if(n==0)
strcpy(id[i],token);

13
i++;
}
}
}
printf("\n keywords :\n");
for(x=0;x<k;x++)
printf("\t %s",key[x]);
printf("\n identifiers : \n");
for(x=0;x<i;x++)
printf("\t %s",id[x]);
printf("\n operators : \n");
for(x=0;x<o;x++)
printf("\t %s",op[x]);
fclose(fp);
fclose(f);
getch();
}
int search(char tok[20][20],char s[], int n)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(strcmp(tok[i],s)==0)
flag=1;

}
return flag;
}

InputFiles:

14
Keyword.c
int
char
double
float
void
main
printf
scanf
getch

add.c
void main()
{
int a,b,c;
printf("hello c program");
a=b+c;
getch();
}

Output:

keywords :
void main int printf getch
identifiers :
a b c hello program
operators :
( ) { , ; " = + }

RESULT:

Ex.No: 3

Date: LEXICAL ANALYSER USING LEX TOOL

15
AIM:

ALGORITHM:

Program:
digits[0-9]

16
letters[a-zA-Z]
delimiters[\,\:\;]
arithmetic[\+\-\*\/\%]
relational[\==\!=\<\>\<=\>=]
%{
int line=0;
%}
%%
#.* {printf("\n %s is a preprocessor directive",yytext);}
"/*".*"*/" {printf("\n %s is a comment statement",yytext);}
\".*\" {printf("\n %s is a control string",yytext);}
int|float|char|double|void|long|if|else|while|do|for|goto|break|continue|
switch|case|default|return |main {printf("\n %s is a keyword",yytext);}
[letters]([letters]|[digits])* {printf("\n %s is an identifier",yytext);}
[digits]+ {printf("\n %s ius a number",yytext);}
[delimiters]? {printf("\n %s is a special symbol - delimiters",yytext);}
\{ {printf("\n %s is a opening brace",yytext);}
\} {printf("\n %s is a closing brace",yytext);}
\( {printf("\n %s is a left parenthesis",yytext);}
\) {printf("\n %s is a right parnthesis",yytext);}
[arithmetic]? {printf("\n %s is an arithmatic operator",yytext);}
[relational]? {printf("\n %s is a relational operator",yytext);}
\n {line++;}
%%
int main(int argc,char **argv)
{
yyin=fopen(argv[1],"r");
yylex();
printf("\n\n\n no of lines scanned : %d",line);
fclose(yyin);
return 0;
}
int yyerror (char *error)
{ fprintf(stderr,“%s”,error); }

17
int yywrap(void)
{ return 1;}

Input File:
input.c
void main()
{
int a,b;
if(a<b)
}
Output:

[exam24@localhost ~]$ lex lault3.l


[exam24@localhost ~]$ cc lex.yy.c -ll
[exam24@localhost ~]$ ./a.out input.c
void is a keyword main is an identifier ( is a left parenthesis
) is a right parenthesis { is a opening brace int is a keyword
a is an identifier , is a special symbol - delimiters b is an identifier
; is a special symbol – delimiters if is a keyword ( is a left parenthesis
a is an identifier < is a relational operator b is an
identifier ) is a right parenthesis } is a closing brace

RESULT:

Ex.No: 4

Date: Recognizing a valid arithmetic expression using LEX and YACC

18
AIM:

ALGORITHM:

Program:
Lex Part

19
%{
#include "y.tab.h"
%}
%%
[0-9]+ {
return NUM;}
[a-zA-Z] return ID;
[\t];
\n return 0;
. return yytext[0];
%%

Yacc Part
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token NUM
%token ID
%left '+''-'
%left '*''/'
%left '('')'
%%
expr: e
;
e:e '+' e
|e '-' e
|e '*' e
|e '/' e
|'(' e ')'
|NUM
|ID
;

20
%%
main()
{
printf("\nEnter the arithmetic expresssion \n");
yyparse();
printf("\n valid expression\n");
}
int yyerror(char *error)
{
fprintf(stderr,"%s",error);
printf("\ninvalid expression\n");
exit(0);
}
int yywrap(void)
{
return 1;
}
Output
$ lex s.l
$ yacc -d s1.y
$ cc y.tab.c lex.yy.c -ll -lm
$ ./a.out

Enter the arithmetic Expression:


a=b+c
valid expression.

Result:

Ex.No: 5

Date: Identifier using LEX Recognizing a valid and YACC

21
AIM:

ALGORITHM:

Program:
Lex part

22
%{
#include "y.tab.h"
%}
%%
[0-9]+ {return DIGIT;}
[a-zA-Z]+ {return LETTER;}
[ \t] {;}
\n { return 0;}
. {return yytext[0];}
%%
Yacc part
%{
#include<stdio.h>
%}
%token DIGIT LETTER
%%
stmt:A
;
A: LETTER | LETTER B
;
B: LETTER
| DIGIT
| DIGIT B
;
%%
main(){
printf("enter string \n");
yyparse();
printf("valid");
exit(0);
}
void yyerror()
{

23
printf("invalid");
exit(0);
}
int yywrap(void)
{
return 1;
}

Output:
$ lex s.l
$ yacc -d s1.y
$ cc y.tab.c lex.yy.c -ll -lm
$ ./a.out
enter string
ab234dfg
valid

Result:

Ex.No: 6

IMPLEMENTATION OF CALCULATOR USING LEX AND YACC


Date:

24
AIM:

ALGORITHM:

Program:
FCAL.L

25
%{
#include"y.tab.h"
#include<math.h>
%}
%%
[0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)? {yylval.dval=atof(yytext); return NUMBER;}
log |
LOG {return LOG;}
ln |
LN {return nLOG;}
sin |
SIN {return SINE;}
cos |
COS {return COS;}
tan |
TAN {return TAN;}
mem {return MEM;}
[\t];
\$ {return 0;}
\n|. return yytext[0];
%%
FCAL.Y
%{
#include<math.h>
#include<stdio.h>
double memvar;
%}
%union
{
double dval;
}
%token <dval> NUMBER
%token <dval> MEM

26
%token LOG SINE nLOG COS TAN
%left '-' '+'
%left '*' '/'
%right '^'
%left LOG SINE nLOG COS TAN
%nonassoc UMINUS
%type <dval> expression
%%
start: statement '\n'
| start statement '\n'
statement: MEM '=' expression {memvar=$3;}
|expression {printf("Answer=%g\n",$1);}
;
expression: expression '+' expression {$$=$1+$3;}
|expression '-' expression {$$=$1-$3;}
|expression '*' expression {$$=$1*$3;}
|expression '/' expression
{
if($3==0)
printf("Divide by zero");
else
$$=$1/$3;
}
|expression '^' expression {$$=pow($1,$3);}
;
expression: '-' expression %prec UMINUS {$$=-$2;}
|'('expression')' {$$=$2;}
|LOG expression {$$=log($2)/log(10);}
|nLOG expression {$$=log($2);}
|SINE expression {$$=sin($2*3.141592654/180);}
|COS expression {$$=cos($2*3.141592654/180);}
|TAN expression {$$=tan($2*3.141592654/180);}
|NUMBER {$$=$1;}

27
|MEM {$$=memvar;}
;
%%
main()
{
printf("Enter the expression:");
yyparse();
}
int yyerror(char *error)
{
fprintf(stderr,"%s\n",error);
}
Output:
$ lex fcal2.l
$ yacc -d fcal.y
$ cc y.tab.c lex.yy.c -ll -lm
$ ./a.out
Enter the expression:2*9
Answer=18
2+90
Answer=92
mem=cos45
sin45/mem
Answer=1
ln10
Answer=2.30259

Result:

Ex.No: 7

Date: Convert the BNF rules into YACC form and write code
to generate abstract syntax tree

28
AIM:

ALGORITHM:

Program:
int.l
%{ #include"y.tab.h"

29
#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
{
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;

30
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,"");

31
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 Arg1 Arg2 Result"
"\n\t\t--------------------");
for(i=0;i<Index;i++)
{printf("\n\t\t %d\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);
}
yyerror()
{printf("\n Error on line no:%d",LineNo);}

Input:
test.c
main()
{

32
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;
}}

Output:
$lex int.l
$yacc –d int.y
$gcc lex.yy.c y.tab.c –ll –lm
$./a.out test.c

Result:

Ex.No: 8

Date: IMPLEMENTATION OF TYPE CHECKING

33
AIM:

ALGORITHM:

Program:

#include<stdio.h>
34
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
void insert(int m,int k,char t[]);
char* chck(char a[],char op[],char b[]);
char* lookup(char d[]);
void Display();
int Search(char lab[]);
int te=1000,h=0;
struct SymbTab
{
char type[10];
char symbol[10];
int addr;

struct SymbTab *next;};


struct SymbTab *first,*last;
char var[5][10];
int size=0;
void main()
{
FILE *fp;
int y,k,r,t=0,x,b=0;
char ch,la[10];
char ope1[10],ope2[10],op[5],asop[5];
char *o[]={"+","-","/","*","%"};
char *key[]={"char","float","int","double"};
char token[50];
fp=fopen("add.c","r");
clrscr();
if(fp==NULL)
{
printf("file not found");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s",token);
for(x=0;x<=3;x++)
{
if(strcmp(token,key[x])==0)
{
ch=fgetc(fp);
k=0;

35
r=0;
do
{
ch=fgetc(fp);
if(ch!=','&& ch!=';')
var[k][r++]=ch;
else
{
var[k][r]='\0';
k++;
r=0;
}
}
while(ch!=';');
break;
}
}
switch(x)
{
case 0:
insert(1,k,"char");
break;
case 1:
insert(4,k,"float");
break;
case 2:
insert(2,k,"int");
break;
case 3:
insert(8,k,"double");
break;
}
ch=fgetc(fp);
if(k>0)
{
k=0;
continue;
}
if(strcmp(token,"if")==0)
{
fscanf(fp,"%s%s%s",ope1,op,ope2);
printf("%s if statement",chck(ope1,op,ope2));
}
else if(strcmp(token,"while")==0)
{
fscanf(fp,"%s%s%s",ope1,op,ope2);

36
printf("\n%s while statement",chck(ope1,op,ope2));
}
else
{
fscanf(fp,"%s%s%s%s",asop,ope1,op,ope2);

for(k=0;k<6;k++)
{
if(strcmp(op,o[k])==0)
{
b=1;
break;
}
}
if((strcmp(asop,"=")==0) && (b==1) && (strcmp(lookup(ope1),lookup(ope2))==0)
&& (strcmp(lookup(ope1),lookup(token))==0))
printf("\nvalid expression");
else
printf("\ninvalid expression");

}
}
printf(“symbol is inserted”);
Display();
fclose(fp);
getch();
}
void insert(int m,int k,char va[])
{
int n,i;

char l[10];
for(i=0;i<k;i++)
{
n=Search(var[i]);
if(n==1)
printf("\n\tThe label exists already in the symbol table\n\tDuplicate can't be
inserted");
else
{
struct SymbTab *p;
p=malloc(sizeof(struct SymbTab));
strcpy(p->symbol,var[i]);
strcpy(p->type,va);
p->next=NULL;
if(size==0)

37
{
first=p;
last=p;
last->addr=te; }

else if(h==1)
{
p->addr=te;
last->next=p;
last=p;
h=0;
}
else
{
p->addr=last->addr+m;
last->next=p;
last=p;
}
}
size=size+1;
}
h=1;
te=last->addr+m;
}
void Display()
{
int i;
struct SymbTab *p;
p=first;
printf("\n\tSYMBOL\t\tADDRESS\t\tTPE\n");
for(i=0;i<size;i++)
{
printf("\t%s\t\t%d\t\t%s\n",p->symbol,p->addr,p->type);
p=p->next;
}
}
int Search(char sym[])
{
int i,flag=0;
struct SymbTab *p;
p=first;
for(i=0;i<size;i++)
{
if(strcmp(p->symbol,sym)==0)
flag=1;

38
p=p->next;
}
return flag;
}
char* lookup(char a[])
{
int i,flag=0;
struct SymbTab *p;
p=first;
for(i=0;i<size;i++)
{
if(strcmp(p->symbol,a)==0)
return p->type;
p=p->next;
}
if(i==size)
return "error";
}
char* chck(char a[],char op[],char b[])
{
int x,y=0;
char *re[]={"<",">","<=",">=","==","!="};
for(x=0;x<7;x++)
{
if(strcmp(op,re[x])==0)
{ y=1;
break; }
}
if((strcmp(lookup(a),lookup(b))==0) && (y==1))
return "valid";
return "invalid";
}

Input File
add.c
int a,b,c;

39
if a > b
while b > c
c=a+b

Output

symbol inserted

SYMBOL ADDRESS TYPE


a 1000 int
b 1002 int
c 1004 int
valid if statement
valid while statement
valid expression
valid expression

Result:

Ex.No: 9

Date: IMPLEMENTATION OF STATIC STORAGE


ALLOCATION STRATEGY

40
AIM:

ALGORITHM:

Program:

41
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
void insert(int m,int k,char t[]);
void Display();
int Search(char lab[]);
int *te=1000,h=0;
struct SymbTab
{
char type[10];
char symbol[10];
int addr;
struct SymbTab *next;
};
struct SymbTab *first,*last;
char var[5][10];
int size=0;
void main()
{
FILE *fp;
int y,k,r,t=0,x,b=0;
char ch,la[10];
char ope1[10],ope2[10],op[5],asop[5];
char *key[]={"char","float","int","double"};
char token[50];
fp=fopen("add.c","r");
clrscr();
if(fp==NULL)
{
printf("file not found");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s",token);
for(x=0;x<=3;x++)
{
if(strcmp(token,key[x])==0)
{
ch=fgetc(fp);
//ch=fgetc(fp);
k=0;
r=0;

42
do
{
ch=fgetc(fp);
if((ch!=',') && (ch!=';'))
var[k][r++]=ch;
else
{
var[k][r]='\0';
k++;
r=0;
}
}
while(ch!=';');
break;
}
}
switch(x)
{
case 0:
insert(1,k,"char");
break;
case 1:
insert(4,k,"float");
break;
case 2:
insert(2,k,"int");
break;
case 3:
insert(8,k,"double");
break;
}
ch=fgetc(fp);
if(k>0)
{
k=0;
continue;
}
}
printf("\n symbol inserted");
Display();
fclose(fp);
getch();
}
void insert(int m,int k,char va[])
{
int n,i;

43
char l[10];
for(i=0;i<k;i++)
{
n=Search(var[i]);
if(n==1)
printf("\n\tThe label exists already in the symbol table\n\tDuplicate can't be
inserted");
else
{
struct SymbTab *p;
p=malloc(sizeof(struct SymbTab));
strcpy(p->symbol,var[i]);
strcpy(p->type,va);
p->next=NULL;
if(size==0)
{
first=p;
last=p;
last->addr=te;
}
else if(h==1)
{
p->addr=te;
last->next=p;
last=p;
h=0;
}
else
{
p->addr=last->addr+m;
last->next=p;
last=p;
}
}
size=size+1;
}
te=last->addr+m;
h=1;
}
void Display()
{
int i;
struct SymbTab *p;
p=first;
printf("\n\tSYMBOL\t\tADDRESS\t\tTPE\n");
for(i=0;i<size;i++)

44
{
printf("\t%s\t\t%d\t\t%s\n",p->symbol,p->addr,p->type);
p=p->next;
}
}
int Search(char sym[])
{
int i,flag=0;
struct SymbTab *p;
p=first;
for(i=0;i<size;i++)
{
if(strcmp(p->symbol,sym)==0)
flag=1;
p=p->next;
}
return flag;
}

Input File
add.c
int a,b,c;
char r,t,f;

Output:
symbol inserted
SYMBOL ADDRESS TYPE
a 1000 int
b 1002 int
c 1004 int
r 1006 char
t 1007 char
f 1008 char

Result:

Ex.No: 10

Date: IMPLEMENTATION OF ASSEMBLY CODE GENERATION

45
AIM:

ALGORITHM:

Program:
#include<stdio.h>
#include<conio.h>
46
#include<string.h>
void main()
{
int n,i,j;
char a[50][50];
clrscr();
printf("Enter no. of intermediate code:");
scanf("%d",&n);
for(i=0;i<n;i++)
for(i=0;i<n;i++)
{
printf("Enter the three address code:");
for(j=0;j<6;j++)
{
scanf("%c",&a[i][j]);
}
}
printf("The generated code is:");
for(i=0;i<n;i++)
{
printf("\n mov %c,R%d",a[i][3],i);
if(a[i][4]=='-')
{
printf("\n sub %c,R%d",a[i][5],i);
}
if(a[i][4]=='+')
{
printf("\n add %c,R%d",a[i][5],i);
}
if(a[i][4]=='*')
{
printf("\n mul %c,R%d",a[i][5],i);
}
if(a[i][4]=='/')
{
printf("\n div %c,R%d",a[i][5],i);
}
printf("\n mov R%d,%c",i,a[i][1]);
}
getch();
}

OUTPUT:

Enter no. of intermediate code:4

47
Enter the three address code:t=a-b
Enter the three address code:u=a-c
Enter the three address code:v=t*u
Enter the three address code:w=v/u
The generated code is:
mov a,R0
sub b,R0
mov R0,t
mov a,R1
sub c,R1
mov R1,u
mov t,R2
mul u,R2
mov R2,v
mov v,R3
div u,R3
mov R3,w

Result:

Ex.No: 11

Date: 11. DAG construction

AIM:

48
ALGORITHM:

Program:

#include<stdio.h>
#include<conio.h>

49
#include<string.h>
typedef struct bst
{
char element[5];
struct bst *left;
struct bst *right;
}node;
void view(node*);
void main()
{
FILE *f;
node *s,*r,*p,*q,*t,*u,*v;
char a[3],b[3],c[3],d[3],e[3],m[5],l[5];
clrscr();
f=fopen("input.txt","r");
fscanf(f,"%s%s%s%s%s",a,b,c,d,e);
u=(node*)malloc(sizeof(node));
strcpy(u->element,c);
u->left=u->right=NULL;
v=(node*)malloc(sizeof(node));
strcpy(v->element,e);
v->left=v->right=NULL;
t=(node*)malloc(sizeof(node));
strcpy(t->element,d);
t->left=u;
t->right=v;
strcpy(m,a);
while(!feof(f))
{
fscanf(f,"%s%s%s%s%s",a,b,c,d,e);
if((strcmp(m,c)==0) && (strcmp(l,e)==0))
{
p=(node*)malloc(sizeof(node));
strcpy(s->element,d);
p->left=t;
p->right=s;
t=p;
}
else if((strcmp(l,c)==0)&& (strcmp(m,e)==0))
{
p=(node*)malloc(sizeof(node));
strcpy(s->element,d);
p->left=s;
p->right=t;
t=p;
}

50
else if (strcmp(m,c)==0)
{
r=(node*)malloc(sizeof(node));
strcpy(r->element,e);
r->left=r->right=NULL;
s=(node*)malloc(sizeof(node));
strcpy(s->element,d);
s->left=t;
s->right=r;
strcpy(m,a);
t=s;
}
else if (strcmp(m,e)==0)
{
r=(node*)malloc(sizeof(node));
strcpy(r->element,c);
r->left=r->right=NULL;
s=(node*)malloc(sizeof(node));
strcpy(s->element,d);
s->left=r;
s->right=t;
strcpy(m,a);
t=s;
}
else
{
p=(node*)malloc(sizeof(node));
strcpy(p->element,c);
p->left=p->right=NULL;
r=(node*)malloc(sizeof(node));
strcpy(r->element,e);
r->left=r->right=NULL;
s=(node*)malloc(sizeof(node));
strcpy(s->element,d);
s->left=p;
s->right=r;
strcpy(l,a);
}
}
view(t);
fclose(f);
getch();
}
void view(node* t)
{
if(t!=NULL)

51
{
view(t->left);
printf("%3s",t->element);
view(t->right);
}
}

Input: input.txt

t1 = a + b
t2 = c + d
t3 = t1 + t2

Output:

a + b + c + d

Result:

Ex.No: 12

Date: Implementation of Simple Code Optimization Techniques

52
AIM:

ALGORITHM:

Program:

#include<stdio.h>

53
#include<string.h
#include<conio.h>
void main()
{
int ch,n,l,i,j,g,k,a,c,r;
char name[5],s[10],b,ne[10],val[10][5];
clrscr();
do
{
printf("enter your choice 1.constant folding 2.strength reduction\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("enter the no. of variable");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the variable name\n");
getc(stdin);
name[i]=getc(stdin);
printf("enter the constant value\n");
scanf("%s",val[i]);
}
printf("enter the expression \n");
scanf("%s",s);
l=strlen(s);
strcpy(ne,'\0');
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
if(name[j]==s[i])
{
strcat(ne,val[j]);
break;
}
}
if(j==n)
{
r=strlen(ne);
ne[r]=s[i];
ne[r+1]='\0';
}
}
printf("\n the optimized string is %s\n",ne);
break;
}
case 2:
{
printf("\n enter the operation 1.multification 2.power \n");
scanf("%d",&c);
if(c==1)

54
{
printf("\n enter the numeric argument");
scanf("%d",&a);
printf("\n enter the other argument");
getc(stdin);
b=getc(stdin);
for(k=0;k<a-1;k++)
printf("%c + ",b);
printf("%c",b);
}
else
{
printf("\n enter the numeric argument");
scanf("%d",&a);
printf("\n enter the other argument");
getc(stdin);
b=getc(stdin);
for(k=0;k<a-1;k++)
printf("%c * ",b);
printf("%c ",b);
}
break;
}
default:
printf("\n try again");
}
printf("\n do u want to continue give 1otherwise 0 \n");
scanf("%d",&g);
}
while(g==1);getch();
}

55
Output:

Output 1: Output 2:
1. Constant Folding 2. Strength Reduction 1.Constant Folding 2. Strength Reduction

Enter Your Choice 1 Enter Your Choice 2

Enter no of variables 2 1.multification 2.power

Enter the variable name a Enter the option 1

Enter the constant value 4 Enter the numeric argument 3

Enter the variable name b Enter other argument a


a + a +a
Enter the constant value 2
do you want to continue 1
Enter the expression a+b
1.Constant Folding 2. Strength Reduction
The optimized string is 4+2
Enter Your Choice 2
1.multification 2.power
Enter the option 2
Enter the numeric argument 3
Enter other argument a
a * a *a

Result:

56

S-ar putea să vă placă și