LAB MANUALCOMPILER LABVIII SEM BTECH (CSE)+DD(CS+SE AND CS+IC)
CP 452
COMPILER LAB
C(L,T,P) = 2 (0,0,2+2)
1,2 Write a Program to identify data storage statements in an 8086 assembly language program and estimate the size of data segment.1. Write a program to identify macro definitions in an assembly language program.
4,5. Extend the above program to implement simple and recursive macro expansion.
6. Write a program to process include and define macro in C language.
7, 8 Write a program to parse source code string of C-language and identify token in terms of keywords and identifiers.
9. Construct parse tree of arithmetic statements in C language program.
10. Write a program to optimize the source program for operator strength reduction, dead code elimination and frequency reduction transformation.
11, 12 Design a simple high level language containing arithmetic and logic operations pointers, branch and loop instructions. Write its lexical analyzer using lex.
PROGRAM1AIM: Write a program to identify data storage statements in an 8086 assembly language program.
// Source.C
#include
#include
#include
struct source
{
char label[10],instr[10],operand[10];
}src;
struct pseudo
{
char code[10];
}ps;
struct result
{
intaddr;
char label[10],instr[10],operand[10];
}rst;
int detect(char search[10])
{
FILE *p;
p=fopen("pseudo.txt","r");
while(!feof(p))
{
fscanf(p,"%s",ps.code);
if(strcmp(search,ps.code)==0)
{
fclose(p);
return 1;
}
}
fclose(p);
return 0;
}
void main()
{
FILE *s,*r;
int d=0,addr;
clrscr();
s=fopen("source.txt","r");
r=fopen("result.txt","w");
while(!feof(s))
{
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
d=detect(src.instr);
if(d==1)
{
if(strcmp(src.instr,"START")==0)
addr=atoi(src.operand);
else if(strcmp(src.instr,"WORD")==0)
addr=rst.addr+3;
else if(strcmp(src.instr,"RESW")==0)
addr=rst.addr+(atoi(src.operand)*3);
else if(strcmp(src.instr,"RESB")==0)
addr=rst.addr+atoi(src.operand);
else if(strcmp(src.instr,"BYTE")==0)
addr=rst.addr+(strlen(src.operand)-3);
else
addr=rst.addr+3;
strcpy(rst.label,src.label);
strcpy(rst.instr,src.instr);
strcpy(rst.operand,src.operand);
fprintf(r,"%d\t%s\t%s\t%s\n",rst.addr,rst.label,rst.instr,rst.operand);
rst.addr=addr;
}
else
{
printf("Error Encounted");
fclose(s);
fclose(r);
getch();
}
}
fclose(s);
fclose(r);
getch();
}
Output
Input File 1:Pseudo.txt
START
READ
WORD
BYTE
RESW
RESB
END
Input File 2:Source.txt
NULL
START1000
A
RESB
100
LOOP
READ
A
B
BYTE
C"LOOP"
C
WORD100
G
RESW
5
NULL
END
NULL
Output File:result.txt
0NULL
START1000
1000A
RESB
100
1100LOOP
READ
A
1103B
BYTE
C"LOOP"
1107C
WORD100
1110G
RESW
5
1125NULL
END
NULL
PROGRAM 2
AIM: Write a program to identify data storage statements in an 8086 assembly language program and estimate the size of data segment.
// Source.C
#include
#include
#include
struct source
{
char label[10],instr[10],operand[10];
}src;
struct pseudo
{
char code[10];
}ps;
struct result
{
intaddr;
char label[10],instr[10],operand[10];
}rst;
int detect(char search[10])
{
FILE *p;
p=fopen("pseudo.txt","r");
while(!feof(p))
{
fscanf(p,"%s",ps.code);
if(strcmp(search,ps.code)==0)
{
fclose(p);
return 1;
}
}
fclose(p);
return 0;
}
void main()
{
FILE *s,*r;
int d=0,addr;
clrscr();
s=fopen("source.txt","r");
r=fopen("result.txt","w");
while(!feof(s))
{
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
d=detect(src.instr);
if(d==1)
{
if(strcmp(src.instr,"START")==0){
addr=atoi(s.operand);
size=0;
}
else if(strcmp(src.instr,"WORD")==0){
addr=rst.addr+3;
size=addr-res.address;
}
else if(strcmp(src.instr,"RESW")==0){
addr=rst.addr+(atoi(src.operand)*3);
size=addr-res.address;
}
else if(strcmp(src.instr,"RESB")==0){
addr=rst.addr+atoi(src.operand);
size=addr-res.address;
}
else if(strcmp(src.instr,"BYTE")==0){
addr=rst.addr+(strlen(src.operand)-3);
size=addr-res.address;
}
else{
addr=rst.addr+3;
size=addr-res.address;
}
strcpy(rst.label,src.label);
strcpy(rst.instr,src.instr);
strcpy(rst.operand,src.operand);
fprintf(r,"%d\t%s\t%s\t%s\n",rst.addr,rst.label,rst.instr,rst.operand);
rst.addr=addr;
}
else
{
printf("Error Encounted");
fclose(s);
fclose(r);
getch();
}
}
fclose(s);
fclose(r);
getch();
}
Input File 1:Pseudo.txt
START
READ
WORD
BYTE
RESW
RESB
END
Input File 2: Source.txt
NULL
START1000
A
RESB
100
LOOP
READ
A
B
BYTE
C"LOOP"
C
WORD100
G
RESW
5
NULL
END
NULL
Output File :Result.txt
AddrLabel
OperandInstrSize
0NULL
START10000
1000A
RESB100100100
1100LOOP
READA00
1103B
BYTE
C"LOOP"4
1107CWORD1003
1110GRESW51515
1125NULLENDNULL00
PROGRAM 3
AIM: Write a program to identify macro definitions in an assembly language program.
//Macro.C
#include
#include
#include
struct source
{
char label[10], instr[10], operand[20];
}src;
struct result
{
char label[10],instr[10], operand[20];
}rst;
void main()
{
FILE *s,*r;
clrscr();
s=fopen("source1.txt","r");
r=fopen("resultmacro.txt","w");
while(!feof(s))
{
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
if(strcmp(src.instr,"MACRO")==0)
{
while(strcmp(src.instr,"MEND")!=0)
{
fprintf(r,"%s\t%s\t%s\n",rst.label,rst.instr,rst.operand);
strcpy(rst.label,src.label);
strcpy(rst.instr,src.instr);
strcpy(rst.operand,src.operand);
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
}
}
}
printf("MACRO Completed");
fclose(s);
fclose(r);
getch();
}
Input File: Source.txt
NULLMACRO INCR&A,&B,®2=BREG
NULLMOVER ®2,&A
NULLSUB ®2,&B
NULLMOVEM ®2,&A
NULLMEND NULL
NULLSTART100
NULLREADN1
NULLREADN2
NULLINCRN1,N2
NULLEND
Output File: Result.txt
NULLMACROINCR&A,&B,®2=BREGNULL
NULLMOVER®2,&A
NULLSUB®2,&B
PROGRAM 4 and 5AIM: Write a program to extend the third program to implement simple macro expansion.//Source.C
#include
#include
#include
#include
structmnt
{
char name[20];
intmdtindex;
intSkipFlag;
}mnt[10];
structmdt
{
intsrno;
charinst[20];
}mdt[10];
structala
{
int index;
chararg[20];
}ala[10];
intmntc,mdtc,alac;
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
{
returnstr;
}
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str),"%s%s",rep,p+strlen(orig));
if(!(p = strstr(buffer, orig))) // Is 'orig' even in 'buffer'?
return buffer;
else
replace_str(buffer,orig,rep);
}
voiddispALA()
{
int i;
printf("\nIndex\tArgument");
printf("\n------------------");
for(i=0;ilexptr,ptr->token);
}*/
int lexer()
{
int t;
int val, i = 0;
while (1)
{
t = getchar();
if (t == ' ' || t == '\t')
;
else if (t == '\n')
lineno = lineno + 1;
else if (isdigit(t))
{
ungetc(t, stdin);
scanf("%d", &tokenval);
return NUM;
}
else if (isalpha(t))
{
while (isalnum(t))
{
buffer[i] = t;
t = getchar();
i = i + 1;
if (i >= SIZE)
Error_Message("Compiler error");
}
buffer[i] = EOS;
if (t != EOF)
ungetc(t, stdin);
val = look_up(buffer);
if (val == 0)
val = insert(buffer, ID);
tokenval = val;
return symtable[val].token;
}
else if (t == EOF)
return DONE;
else
{
tokenval = NONE;
return t;
}
}
}
void Match(int t)
{
if (lookahead == t)
lookahead = lexer();
else
Error_Message("Syntax error");
}
void display(int t, int tval)
{
if (t == '+' || t == '-' || t == '*' || t == '/')
printf("\nArithmetic Operator: %c", t);
else if (t == NUM)
printf("\n Number: %d", tval);
else if (t == ID)
printf("\n Identifier: %s", symtable[tval].lexptr);
else
printf("\n Token %d tokenval %d", t, tokenval);
}
void F()
{
//void E();
switch (lookahead)
{
case '(':
Match('(');
E();
Match(')');
break;
case NUM:
display(NUM, tokenval);
Match(NUM);
break;
case ID:
display(ID, tokenval);
Match(ID);
break;
default:
Error_Message("Syntax error");
}
}
void T()
{
int t;
F();
while (1)
{
switch (lookahead)
{
case '*':
t = lookahead;
Match(lookahead);
F();
display(t, NONE);
continue;
case '/':
t = lookahead;
Match(lookahead);
display(t, NONE);
continue;
default:
return ;
}
}
}
E()
{
int t;
T();
while (1)
{
switch (lookahead)
{
case '+':
t = lookahead;
Match(lookahead);
T();
display(t, NONE);
continue;
case '-':
t = lookahead;
Match(lookahead);
T();
display(t, NONE);
continue;
default:
return ;
}
}
}
void parser()
{
lookahead = lexer();
while (lookahead != DONE)
{
E();
Match(';');
}
}
void main()
{
char ans[10];
printf("\n Program for recursive decent parsing ");
printf("\n Enter the expression ");
printf("And place ; at the end\n");
printf("Press Ctrl-Z to terminate\n");
parser();
getch();
}